From 65705173305df2e169b6bd70712eb472558878bc Mon Sep 17 00:00:00 2001 From: Maikel Punie Date: Sat, 5 Aug 2023 18:29:46 +0200 Subject: [PATCH] Add lightplatform to Duotecno (#97582) * add light platform * Add light to coveragerc * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/duotecno/light.py Co-authored-by: Joost Lekkerkerker * comments * revert * re-implement comments --------- Co-authored-by: Joost Lekkerkerker --- .coveragerc | 1 + homeassistant/components/duotecno/__init__.py | 2 +- homeassistant/components/duotecno/light.py | 69 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/duotecno/light.py diff --git a/.coveragerc b/.coveragerc index 9c6e7a1a223..564b3203ac9 100644 --- a/.coveragerc +++ b/.coveragerc @@ -234,6 +234,7 @@ omit = homeassistant/components/duotecno/entity.py homeassistant/components/duotecno/switch.py homeassistant/components/duotecno/cover.py + homeassistant/components/duotecno/light.py homeassistant/components/dwd_weather_warnings/const.py homeassistant/components/dwd_weather_warnings/coordinator.py homeassistant/components/dwd_weather_warnings/sensor.py diff --git a/homeassistant/components/duotecno/__init__.py b/homeassistant/components/duotecno/__init__.py index 98003c3e8c4..4c8060b468d 100644 --- a/homeassistant/components/duotecno/__init__.py +++ b/homeassistant/components/duotecno/__init__.py @@ -11,7 +11,7 @@ from homeassistant.exceptions import ConfigEntryNotReady from .const import DOMAIN -PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.COVER] +PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.COVER, Platform.LIGHT] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/duotecno/light.py b/homeassistant/components/duotecno/light.py new file mode 100644 index 00000000000..01d3bf488f1 --- /dev/null +++ b/homeassistant/components/duotecno/light.py @@ -0,0 +1,69 @@ +"""Support for Duotecno lights.""" +from typing import Any + +from duotecno.unit import DimUnit + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ColorMode, + LightEntity, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .entity import DuotecnoEntity + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Duotecno light based on config_entry.""" + cntrl = hass.data[DOMAIN][entry.entry_id] + async_add_entities(DuotecnoLight(channel) for channel in cntrl.get_units("DimUnit")) + + +class DuotecnoLight(DuotecnoEntity, LightEntity): + """Representation of a light.""" + + _unit: DimUnit + _attr_color_mode = ColorMode.BRIGHTNESS + _attr_supported_color_modes = {ColorMode.BRIGHTNESS} + + @property + def is_on(self) -> bool: + """Return true if the light is on.""" + return self._unit.is_on() + + @property + def brightness(self) -> int: + """Return the brightness of the light.""" + return int((self._unit.get_dimmer_state() * 255) / 100) + + async def async_turn_on(self, **kwargs: Any) -> None: + """Instruct the light to turn on.""" + if (val := kwargs.get(ATTR_BRIGHTNESS)) is not None: + # set to a value + val = max(int((val * 100) / 255), 1) + else: + # restore state + val = None + try: + await self._unit.set_dimmer_state(val) + except OSError as err: + raise HomeAssistantError( + "Transmit for the set_dimmer_state packet failed" + ) from err + + async def async_turn_off(self, **kwargs: Any) -> None: + """Instruct the light to turn off.""" + try: + await self._unit.set_dimmer_state(0) + except OSError as err: + raise HomeAssistantError( + "Transmit for the set_dimmer_state packet failed" + ) from err