hOn/custom_components/hon/switch.py

401 lines
14 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-04-16 13:55:08 +02:00
from pyhon.parameter.range import HonParameterRange
2023-03-05 00:54:57 +01:00
from .const import DOMAIN
2023-05-07 16:39:45 +02:00
from .hon import HonCoordinator, HonEntity, unique_entities
2023-03-05 00:54:57 +01:00
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-04-23 02:01:14 +02:00
translation_key="washing_machine",
2023-03-05 00:54:57 +01:00
),
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-04-23 02:01:14 +02:00
translation_key="pause",
2023-03-08 23:00:55 +01:00
),
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-04-24 21:38:05 +02:00
translation_key="delay_time",
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-04-23 02:01:14 +02:00
translation_key="prewash",
2023-03-05 00:54:57 +01:00
),
HonSwitchEntityDescription(
key="startProgram.autoSoftenerStatus",
name="Keep Fresh",
entity_category=EntityCategory.CONFIG,
icon="mdi:refresh-circle",
translation_key="keep_fresh",
),
HonSwitchEntityDescription(
key="startProgram.autoDetergentStatus",
name="Auto Dose",
entity_category=EntityCategory.CONFIG,
icon="mdi:cup",
translation_key="auto_dose",
),
HonSwitchEntityDescription(
key="startProgram.acquaplus",
name="Acqua Plus",
entity_category=EntityCategory.CONFIG,
icon="mdi:water-plus",
translation_key="acqua_plus",
),
HonSwitchEntityDescription(
key="extraRinse1",
name="Extra Rinse 1",
entity_category=EntityCategory.CONFIG,
icon="mdi:numeric-1-box-multiple-outline",
translation_key="extra_rinse_1",
),
HonSwitchEntityDescription(
key="extraRinse2",
name="Extra Rinse 2",
entity_category=EntityCategory.CONFIG,
icon="mdi:numeric-2-box-multiple-outline",
translation_key="extra_rinse_2",
),
HonSwitchEntityDescription(
key="extraRinse3",
name="Extra Rinse 3",
entity_category=EntityCategory.CONFIG,
icon="mdi:numeric-3-box-multiple-outline",
translation_key="extra_rinse_3",
),
HonSwitchEntityDescription(
key="goodNight",
name="Good Night",
icon="mdi:weather-night",
entity_category=EntityCategory.CONFIG,
translation_key="good_night",
),
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",
2023-04-23 02:01:14 +02:00
translation_key="tumble_dryer",
2023-03-21 13:01:41 +01:00
),
HonSwitchEntityDescription(
key="pause",
name="Pause Tumble Dryer",
icon="mdi:pause",
turn_on_key="pauseProgram",
turn_off_key="resumeProgram",
2023-04-23 02:01:14 +02:00
translation_key="pause",
2023-03-21 13:01:41 +01:00
),
HonSwitchEntityDescription(
key="startProgram.sterilizationStatus",
name="Sterilization",
icon="mdi:clock-start",
entity_category=EntityCategory.CONFIG,
),
HonSwitchEntityDescription(
key="startProgram.antiCreaseTime",
name="Anti-Crease",
entity_category=EntityCategory.CONFIG,
icon="mdi:timer",
translation_key="anti_crease",
),
HonSwitchEntityDescription(
key="startProgram.anticrease",
name="Anti-Crease",
entity_category=EntityCategory.CONFIG,
icon="mdi:timer",
2023-04-23 02:01:14 +02:00
translation_key="anti_crease",
),
2023-03-21 13:01:41 +01:00
),
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-23 02:01:14 +02:00
translation_key="oven",
2023-04-16 16:59:37 +02:00
),
2023-04-19 19:31:45 +02:00
HonSwitchEntityDescription(
key="startProgram.preheatStatus",
name="Preheat",
icon="mdi:thermometer-chevron-up",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="preheat",
2023-04-19 19:31:45 +02:00
),
2023-04-16 16:59:37 +02:00
),
2023-04-08 04:44:47 +02:00
"WD": (
HonSwitchEntityDescription(
key="active",
2023-05-07 16:39:45 +02:00
name="Washer Dryer",
2023-04-08 04:44:47 +02:00
icon="mdi:washing-machine",
turn_on_key="startProgram",
turn_off_key="stopProgram",
2023-04-23 02:01:14 +02:00
translation_key="washer_dryer",
2023-04-08 04:44:47 +02:00
),
HonSwitchEntityDescription(
key="pause",
2023-05-07 16:39:45 +02:00
name="Pause Washer Dryer",
2023-04-08 04:44:47 +02:00
icon="mdi:pause",
turn_on_key="pauseProgram",
turn_off_key="resumeProgram",
2023-04-23 02:01:14 +02:00
translation_key="pause",
2023-04-08 04:44:47 +02:00
),
),
2023-04-16 21:46:17 +02:00
"DW": (
HonSwitchEntityDescription(
key="active",
name="Dish Washer",
icon="mdi:dishwasher",
turn_on_key="startProgram",
turn_off_key="stopProgram",
2023-04-23 02:01:14 +02:00
translation_key="dish_washer",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.extraDry",
name="Extra Dry",
icon="mdi:hair-dryer",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="extra_dry",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.halfLoad",
name="Half Load",
icon="mdi:fraction-one-half",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="half_load",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.openDoor",
name="Open Door",
icon="mdi:door-open",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="open_door",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.threeInOne",
name="Three in One",
icon="mdi:numeric-3-box-outline",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="three_in_one",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.ecoExpress",
name="Eco Express",
icon="mdi:sprout",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="eco",
2023-04-16 21:46:17 +02:00
),
HonSwitchEntityDescription(
key="startProgram.addDish",
name="Add Dish",
icon="mdi:silverware-fork-knife",
entity_category=EntityCategory.CONFIG,
2023-04-23 02:01:14 +02:00
translation_key="add_dish",
2023-04-16 21:46:17 +02:00
),
),
2023-04-26 23:57:44 +02:00
"AC": (
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.10degreeHeatingStatus",
2023-04-26 23:57:44 +02:00
name="10° Heating",
2023-05-08 02:05:04 +02:00
icon="mdi:heat-wave",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="10_degree_heating",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.echoStatus",
2023-04-26 23:57:44 +02:00
name="Echo",
2023-05-08 02:05:04 +02:00
icon="mdi:account-voice",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.ecoMode",
2023-04-26 23:57:44 +02:00
name="Eco Mode",
entity_category=EntityCategory.CONFIG,
translation_key="eco_mode",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.healthMode",
2023-04-26 23:57:44 +02:00
name="Health Mode",
2023-05-08 02:05:04 +02:00
icon="mdi:medication-outline",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.muteStatus",
2023-04-26 23:57:44 +02:00
name="Mute",
2023-05-08 02:05:04 +02:00
icon="mdi:volume-off",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="mute_mode",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.rapidMode",
2023-04-26 23:57:44 +02:00
name="Rapid Mode",
2023-05-08 02:05:04 +02:00
icon="mdi:run-fast",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="rapid_mode",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.screenDisplayStatus",
2023-04-26 23:57:44 +02:00
name="Screen Display",
2023-05-08 02:05:04 +02:00
icon="mdi:monitor-small",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.selfCleaning56Status",
2023-04-26 23:57:44 +02:00
name="Self Cleaning 56",
2023-05-08 02:05:04 +02:00
icon="mdi:air-filter",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="self_clean_56",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.selfCleaningStatus",
2023-04-26 23:57:44 +02:00
name="Self Cleaning",
2023-05-08 02:05:04 +02:00
icon="mdi:air-filter",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="self_clean",
),
HonSwitchEntityDescription(
2023-05-08 02:05:04 +02:00
key="settings.silentSleepStatus",
2023-04-26 23:57:44 +02:00
name="Silent Sleep",
2023-05-08 02:05:04 +02:00
icon="mdi:bed",
2023-04-26 23:57:44 +02:00
entity_category=EntityCategory.CONFIG,
translation_key="silent_mode",
),
),
2023-03-05 00:54:57 +01:00
}
2023-05-07 16:39:45 +02:00
SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["WM"])
SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"])
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
2023-05-07 00:52:54 +02:00
or description.key in device.available_settings
2023-05-08 02:35:54 +02:00
or description.turn_on_key in list(device.commands)
or description.turn_off_key in list(device.commands)
2023-04-10 19:51:16 +02:00
):
appliances.extend(
[HonSwitchEntity(hass, coordinator, entry, device, description)]
2023-03-05 19:19:52 +01:00
)
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}"
@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]
2023-04-16 21:46:17 +02:00
setting.value = (
setting.max if isinstance(setting, HonParameterRange) else "1"
)
2023-03-05 00:54:57 +01:00
self.async_write_ha_state()
2023-04-26 23:57:44 +02:00
if self._device.appliance_type in ["AC"]:
self._device.commands["startProgram"].send()
2023-04-23 20:04:19 +02:00
await self.coordinator.async_refresh()
2023-03-05 00:54:57 +01:00
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]
2023-04-16 21:46:17 +02:00
setting.value = (
setting.min if isinstance(setting, HonParameterRange) else "0"
)
2023-03-05 00:54:57 +01:00
self.async_write_ha_state()
2023-04-26 23:57:44 +02:00
if self._device.appliance_type in ["AC"]:
self._device.commands["startProgram"].send()
2023-04-23 20:04:19 +02:00
await self.coordinator.async_refresh()
2023-03-05 00:54:57 +01:00
else:
await self._device.commands[self.entity_description.turn_off_key].send()
@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.entity_category == EntityCategory.CONFIG:
return super().available
else:
return super().available and self._device.get("remoteCtrValid") == "1"