2023-06-30 19:36:36 +02:00
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
LightEntityDescription,
|
|
|
|
LightEntity,
|
|
|
|
ColorMode,
|
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import callback
|
2023-07-23 21:52:42 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2023-06-30 19:36:36 +02:00
|
|
|
from pyhon.appliance import HonAppliance
|
|
|
|
from pyhon.parameter.range import HonParameterRange
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .hon import HonEntity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-07-23 21:52:42 +02:00
|
|
|
LIGHTS: dict[str, tuple[LightEntityDescription, ...]] = {
|
2023-07-01 01:45:32 +02:00
|
|
|
"WC": (
|
|
|
|
LightEntityDescription(
|
|
|
|
key="settings.lightStatus",
|
|
|
|
name="Light",
|
|
|
|
translation_key="light",
|
|
|
|
),
|
|
|
|
),
|
2023-06-30 19:36:36 +02:00
|
|
|
"HO": (
|
|
|
|
LightEntityDescription(
|
2023-07-12 00:20:35 +02:00
|
|
|
key="settings.lightStatus",
|
2023-06-30 19:36:36 +02:00
|
|
|
name="Light status",
|
2023-07-01 01:45:32 +02:00
|
|
|
translation_key="light",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
"AP": (
|
|
|
|
LightEntityDescription(
|
|
|
|
key="settings.lightStatus",
|
|
|
|
name="Light status",
|
|
|
|
translation_key="light",
|
2023-06-30 19:36:36 +02:00
|
|
|
),
|
|
|
|
),
|
2024-03-18 01:11:23 +01:00
|
|
|
"DW": (
|
|
|
|
LightEntityDescription(
|
|
|
|
key="settings.lightStatus",
|
|
|
|
name="Light status",
|
|
|
|
translation_key="light",
|
|
|
|
),
|
|
|
|
),
|
2023-06-30 19:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-23 21:52:42 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2023-06-30 19:36:36 +02:00
|
|
|
entities = []
|
|
|
|
for device in hass.data[DOMAIN][entry.unique_id].appliances:
|
2023-06-30 19:40:30 +02:00
|
|
|
for description in LIGHTS.get(device.appliance_type, []):
|
2023-06-30 19:36:36 +02:00
|
|
|
if (
|
|
|
|
description.key not in device.available_settings
|
|
|
|
or device.get(description.key.split(".")[-1]) is None
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
entity = HonLightEntity(hass, entry, device, description)
|
|
|
|
await entity.coordinator.async_config_entry_first_refresh()
|
|
|
|
entities.append(entity)
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class HonLightEntity(HonEntity, LightEntity):
|
|
|
|
entity_description: LightEntityDescription
|
|
|
|
|
2023-07-23 21:52:42 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
device: HonAppliance,
|
|
|
|
description: LightEntityDescription,
|
|
|
|
) -> None:
|
2023-07-24 21:37:48 +02:00
|
|
|
light = device.settings.get(description.key)
|
2023-07-23 21:52:42 +02:00
|
|
|
if not isinstance(light, HonParameterRange):
|
|
|
|
raise ValueError()
|
2023-06-30 19:36:36 +02:00
|
|
|
self._light_range = (light.min, light.max)
|
|
|
|
self._attr_supported_color_modes: set[ColorMode] = set()
|
|
|
|
if len(light.values) == 2:
|
|
|
|
self._attr_supported_color_modes.add(ColorMode.ONOFF)
|
|
|
|
else:
|
|
|
|
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
|
|
|
|
self._command, self._parameter = description.key.split(".")
|
|
|
|
super().__init__(hass, entry, device, description)
|
|
|
|
self._handle_coordinator_update(update=False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if light is on."""
|
2023-07-23 21:52:42 +02:00
|
|
|
return bool(self._device.get(self.entity_description.key.split(".")[-1]) > 0)
|
2023-06-30 19:36:36 +02:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn on or control the light."""
|
2023-07-23 21:52:42 +02:00
|
|
|
light = self._device.settings.get(self.entity_description.key)
|
|
|
|
if not isinstance(light, HonParameterRange):
|
|
|
|
raise ValueError()
|
2023-06-30 19:36:36 +02:00
|
|
|
if ColorMode.BRIGHTNESS in self._attr_supported_color_modes:
|
|
|
|
percent = int(100 / 255 * kwargs.get(ATTR_BRIGHTNESS, 128))
|
|
|
|
light.value = round(light.max / 100 * percent)
|
|
|
|
if light.value == light.min:
|
|
|
|
self._attr_is_on = False
|
|
|
|
self._attr_brightness = self.brightness
|
|
|
|
else:
|
|
|
|
light.value = light.max
|
|
|
|
await self._device.commands[self._command].send()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Instruct the light to turn off."""
|
2023-07-23 21:52:42 +02:00
|
|
|
light = self._device.settings.get(self.entity_description.key)
|
|
|
|
if not isinstance(light, HonParameterRange):
|
|
|
|
raise ValueError()
|
2023-06-30 19:36:36 +02:00
|
|
|
light.value = light.min
|
|
|
|
await self._device.commands[self._command].send()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self) -> int | None:
|
|
|
|
"""Return the brightness of the light."""
|
2023-07-23 21:52:42 +02:00
|
|
|
light = self._device.settings.get(self.entity_description.key)
|
|
|
|
if not isinstance(light, HonParameterRange):
|
|
|
|
raise ValueError()
|
2023-06-30 19:36:36 +02:00
|
|
|
if light.value == light.min:
|
|
|
|
return None
|
2023-07-23 21:52:42 +02:00
|
|
|
return int(255 / light.max * float(light.value))
|
2023-06-30 19:36:36 +02:00
|
|
|
|
|
|
|
@callback
|
2023-07-23 21:52:42 +02:00
|
|
|
def _handle_coordinator_update(self, update: bool = True) -> None:
|
2023-06-30 19:36:36 +02:00
|
|
|
self._attr_is_on = self.is_on
|
|
|
|
self._attr_brightness = self.brightness
|
|
|
|
if update:
|
|
|
|
self.async_write_ha_state()
|
2023-07-12 00:20:35 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
2023-07-23 21:52:42 +02:00
|
|
|
if (entity := self._device.settings.get(self.entity_description.key)) is None:
|
|
|
|
return False
|
|
|
|
return super().available and len(entity.values) > 1
|