mirror of
https://github.com/Andre0512/hon.git
synced 2024-12-18 02:16:04 +01:00
Add light entity for lights
This commit is contained in:
parent
1d83162f7d
commit
97637ef244
@ -18,6 +18,7 @@ PLATFORMS = [
|
||||
"binary_sensor",
|
||||
"climate",
|
||||
"fan",
|
||||
"light",
|
||||
]
|
||||
|
||||
HON_HVAC_MODE = {
|
||||
|
@ -44,8 +44,9 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non
|
||||
for device in hass.data[DOMAIN][entry.unique_id].appliances:
|
||||
for description in FANS.get(device.appliance_type, []):
|
||||
if isinstance(description, HonFanEntityDescription):
|
||||
if description.key not in device.available_settings or device.get(
|
||||
description.key.split(".")[-1] is None
|
||||
if (
|
||||
description.key not in device.available_settings
|
||||
or device.get(description.key.split(".")[-1]) is None
|
||||
):
|
||||
continue
|
||||
entity = HonFanEntity(hass, entry, device, description)
|
||||
|
109
custom_components/hon/light.py
Normal file
109
custom_components/hon/light.py
Normal file
@ -0,0 +1,109 @@
|
||||
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
|
||||
from pyhon.appliance import HonAppliance
|
||||
from pyhon.parameter.range import HonParameterRange
|
||||
|
||||
from .const import DOMAIN
|
||||
from .hon import HonEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
FANS = {
|
||||
"WC": (LightEntityDescription(key="settings.lightStatus", name="Light"),),
|
||||
"HO": (
|
||||
LightEntityDescription(
|
||||
key="startProgram.lightStatus",
|
||||
name="Light status",
|
||||
),
|
||||
),
|
||||
"AP": (LightEntityDescription(key="settings.lightStatus", name="Light status"),),
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
|
||||
entities = []
|
||||
for device in hass.data[DOMAIN][entry.unique_id].appliances:
|
||||
for description in FANS.get(device.appliance_type, []):
|
||||
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
|
||||
|
||||
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
|
||||
light: HonParameterRange = device.settings.get(description.key)
|
||||
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."""
|
||||
light = self._device.settings.get(self.entity_description.key)
|
||||
return light.value != light.min
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on or control the light."""
|
||||
light: HonParameterRange = self._device.settings.get(
|
||||
self.entity_description.key
|
||||
)
|
||||
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."""
|
||||
light: HonParameterRange = self._device.settings.get(
|
||||
self.entity_description.key
|
||||
)
|
||||
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."""
|
||||
light: HonParameterRange = self._device.settings.get(
|
||||
self.entity_description.key
|
||||
)
|
||||
if light.value == light.min:
|
||||
return None
|
||||
return int(255 / light.max * light.value)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self, update=True) -> None:
|
||||
self._attr_is_on = self.is_on
|
||||
self._attr_brightness = self.brightness
|
||||
if update:
|
||||
self.async_write_ha_state()
|
@ -162,13 +162,6 @@ NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
|
||||
translation_key="freezer_temp_sel",
|
||||
),
|
||||
),
|
||||
"HO": (
|
||||
HonNumberEntityDescription(
|
||||
key="startProgram.lightStatus",
|
||||
name="Light status",
|
||||
icon="mdi:lightbulb",
|
||||
),
|
||||
),
|
||||
"AP": (
|
||||
HonNumberEntityDescription(
|
||||
key="settings.aromaTimeOn",
|
||||
@ -182,11 +175,6 @@ NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
|
||||
icon="mdi:thermometer",
|
||||
native_unit_of_measurement=UnitOfTime.SECONDS,
|
||||
),
|
||||
HonNumberEntityDescription(
|
||||
key="settings.lightStatus",
|
||||
name="Light status",
|
||||
icon="mdi:lightbulb",
|
||||
),
|
||||
HonNumberEntityDescription(
|
||||
key="settings.pollenLevel",
|
||||
name="Pollen Level",
|
||||
|
@ -329,9 +329,6 @@ SWITCHES: dict[str, tuple[HonSwitchEntityDescription, ...]] = {
|
||||
icon="mdi:palm-tree",
|
||||
translation_key="holiday_mode",
|
||||
),
|
||||
HonSwitchEntityDescription(
|
||||
key="lightStatus", name="Light", icon="mdi:lightbulb"
|
||||
),
|
||||
),
|
||||
"AP": (
|
||||
HonSwitchEntityDescription(
|
||||
|
Loading…
Reference in New Issue
Block a user