hOn/custom_components/hon/switch.py

186 lines
5.8 KiB
Python
Raw Normal View History

2023-04-08 04:44:47 +02:00
import logging
2023-03-05 00:54:57 +01:00
from dataclasses import dataclass
from typing import Any
from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
2023-04-10 07:09:54 +02:00
from pyhon import Hon
from pyhon.appliance import HonAppliance
2023-03-05 00:54:57 +01:00
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity
2023-04-08 04:44:47 +02:00
_LOGGER = logging.getLogger(__name__)
2023-03-05 00:54:57 +01:00
2023-04-08 06:15:33 +02:00
2023-03-05 00:54:57 +01:00
@dataclass
class HonSwitchEntityDescriptionMixin:
turn_on_key: str = ""
turn_off_key: str = ""
@dataclass
2023-04-10 19:51:16 +02:00
class HonSwitchEntityDescription(
HonSwitchEntityDescriptionMixin, SwitchEntityDescription
):
2023-03-05 00:54:57 +01:00
pass
SWITCHES: dict[str, tuple[HonSwitchEntityDescription, ...]] = {
"WM": (
HonSwitchEntityDescription(
2023-03-08 23:00:55 +01:00
key="active",
name="Washing Machine",
icon="mdi:washing-machine",
2023-03-05 00:54:57 +01:00
turn_on_key="startProgram",
turn_off_key="stopProgram",
),
2023-03-08 23:00:55 +01:00
HonSwitchEntityDescription(
key="pause",
name="Pause Washing Machine",
icon="mdi:pause",
turn_on_key="pauseProgram",
turn_off_key="resumeProgram",
),
2023-03-05 00:54:57 +01:00
HonSwitchEntityDescription(
key="startProgram.delayStatus",
name="Delay Status",
2023-03-08 23:00:55 +01:00
icon="mdi:timer-check",
2023-04-10 19:51:16 +02:00
entity_category=EntityCategory.CONFIG,
2023-03-05 00:54:57 +01:00
),
HonSwitchEntityDescription(
key="startProgram.haier_SoakPrewashSelection",
name="Soak Prewash Selection",
2023-03-08 23:00:55 +01:00
icon="mdi:tshirt-crew",
2023-04-10 19:51:16 +02:00
entity_category=EntityCategory.CONFIG,
2023-03-05 00:54:57 +01:00
),
),
2023-03-21 13:01:41 +01:00
"TD": (
HonSwitchEntityDescription(
key="active",
name="Tumble Dryer",
icon="mdi:tumble-dryer",
turn_on_key="startProgram",
turn_off_key="stopProgram",
),
HonSwitchEntityDescription(
key="pause",
name="Pause Tumble Dryer",
icon="mdi:pause",
turn_on_key="pauseProgram",
turn_off_key="resumeProgram",
),
),
2023-04-16 16:59:37 +02:00
"OV": (
HonSwitchEntityDescription(
key="active",
name="Oven",
icon="mdi:toaster-oven",
turn_on_key="startProgram",
turn_off_key="stopProgram",
),
),
2023-04-08 04:44:47 +02:00
"WD": (
HonSwitchEntityDescription(
key="active",
name="Washing Machine",
icon="mdi:washing-machine",
turn_on_key="startProgram",
turn_off_key="stopProgram",
),
HonSwitchEntityDescription(
key="pause",
name="Pause Washing Machine",
icon="mdi:pause",
turn_on_key="pauseProgram",
turn_off_key="resumeProgram",
),
),
2023-03-05 00:54:57 +01:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 07:09:54 +02:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-03-05 00:54:57 +01:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 07:09:54 +02:00
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
2023-03-05 00:54:57 +01:00
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
2023-03-05 00:54:57 +01:00
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := SWITCHES.get(device.appliance_type):
2023-03-05 00:54:57 +01:00
for description in descriptions:
2023-04-10 19:51:16 +02:00
if (
device.get(description.key) is not None
or device.commands.get(description.key) is not None
):
appliances.extend(
[HonSwitchEntity(hass, coordinator, entry, device, description)]
2023-03-05 19:19:52 +01:00
)
2023-04-08 04:44:47 +02:00
else:
2023-04-10 19:51:16 +02:00
_LOGGER.warning(
"[%s] Can't setup %s", device.appliance_type, description.key
)
2023-03-05 00:54:57 +01:00
async_add_entities(appliances)
class HonSwitchEntity(HonEntity, SwitchEntity):
entity_description: HonSwitchEntityDescription
2023-04-10 19:51:16 +02:00
def __init__(
self,
hass,
coordinator,
entry,
device: HonAppliance,
description: HonSwitchEntityDescription,
) -> None:
2023-03-05 00:54:57 +01:00
super().__init__(hass, entry, coordinator, device)
self._coordinator = coordinator
self._device = device
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
def available(self) -> bool:
if self.entity_category == EntityCategory.CONFIG:
2023-04-10 19:51:16 +02:00
return (
self._device.settings[self.entity_description.key].typology != "fixed"
)
2023-03-05 00:54:57 +01:00
return True
@property
def is_on(self) -> bool | None:
"""Return True if entity is on."""
if self.entity_category == EntityCategory.CONFIG:
setting = self._device.settings[self.entity_description.key]
2023-04-10 19:51:16 +02:00
return (
setting.value == "1"
or hasattr(setting, "min")
and setting.value != setting.min
)
2023-03-08 23:00:55 +01:00
return self._device.get(self.entity_description.key, False)
2023-03-05 00:54:57 +01:00
async def async_turn_on(self, **kwargs: Any) -> None:
if self.entity_category == EntityCategory.CONFIG:
setting = self._device.settings[self.entity_description.key]
setting.value = setting.max
self.async_write_ha_state()
else:
await self._device.commands[self.entity_description.turn_on_key].send()
async def async_turn_off(self, **kwargs: Any) -> None:
if self.entity_category == EntityCategory.CONFIG:
setting = self._device.settings[self.entity_description.key]
setting.value = setting.min
self.async_write_ha_state()
else:
await self._device.commands[self.entity_description.turn_off_key].send()