Improvements, add switch
parent
136a1810fc
commit
16eafff169
@ -0,0 +1,113 @@
|
||||
from collections.abc import Callable, Coroutine
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from pyhon import HonConnection
|
||||
from pyhon.device import HonDevice
|
||||
|
||||
from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
|
||||
from .const import DOMAIN
|
||||
from .hon import HonCoordinator, HonEntity
|
||||
|
||||
|
||||
@dataclass
|
||||
class HonSwitchEntityDescriptionMixin:
|
||||
turn_on_key: str = ""
|
||||
turn_off_key: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class HonSwitchEntityDescription(HonSwitchEntityDescriptionMixin,
|
||||
SwitchEntityDescription
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
SWITCHES: dict[str, tuple[HonSwitchEntityDescription, ...]] = {
|
||||
"WM": (
|
||||
HonSwitchEntityDescription(
|
||||
key="startProgram",
|
||||
name="Start Program",
|
||||
icon="mdi:play",
|
||||
turn_on_key="startProgram",
|
||||
turn_off_key="stopProgram",
|
||||
),
|
||||
HonSwitchEntityDescription(
|
||||
key="startProgram.delayStatus",
|
||||
name="Delay Status",
|
||||
entity_category=EntityCategory.CONFIG
|
||||
),
|
||||
HonSwitchEntityDescription(
|
||||
key="startProgram.haier_SoakPrewashSelection",
|
||||
name="Soak Prewash Selection",
|
||||
entity_category=EntityCategory.CONFIG
|
||||
),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
|
||||
hon: HonConnection = hass.data[DOMAIN][entry.unique_id]
|
||||
coordinators = hass.data[DOMAIN]["coordinators"]
|
||||
appliances = []
|
||||
for device in hon.devices:
|
||||
if device.mac_address in coordinators:
|
||||
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
|
||||
else:
|
||||
coordinator = HonCoordinator(hass, device)
|
||||
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
if descriptions := SWITCHES.get(device.appliance_type_name):
|
||||
for description in descriptions:
|
||||
appliances.extend([
|
||||
HonSwitchEntity(hass, coordinator, entry, device, description)]
|
||||
)
|
||||
|
||||
async_add_entities(appliances)
|
||||
|
||||
|
||||
class HonSwitchEntity(HonEntity, SwitchEntity):
|
||||
entity_description: HonSwitchEntityDescription
|
||||
|
||||
def __init__(self, hass, coordinator, entry, device: HonDevice, description: HonSwitchEntityDescription) -> None:
|
||||
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:
|
||||
return self._device.settings[self.entity_description.key].typology == "fixed"
|
||||
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]
|
||||
return setting.value == "1" or hasattr(setting, "min") and setting.value != setting.min
|
||||
return self._device.data.get(self.entity_description.key, "")
|
||||
|
||||
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()
|
||||
|
||||
|
@ -1,14 +1,32 @@
|
||||
{
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "hOn",
|
||||
"description": "Please enters your hOn credentials",
|
||||
"data": {
|
||||
"email": "Email Address",
|
||||
"password": "Password"
|
||||
}
|
||||
}
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "hOn",
|
||||
"description": "Please enters your hOn credentials",
|
||||
"data": {
|
||||
"email": "Email Address",
|
||||
"password": "Password"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"mode": {
|
||||
"state": {
|
||||
"1": "Ready",
|
||||
"2": "Running",
|
||||
"5": "Scheduled",
|
||||
"6": "Error",
|
||||
"7": "Finished"
|
||||
}
|
||||
},
|
||||
"errors": {
|
||||
"00": "No error",
|
||||
"100000000000": "E2: Check if the door is closed",
|
||||
"8000000000000": "E4: Check the water supply"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue