From ae4b1967a31decc87a71301d93d3d65bc6ef9300 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 16 Jul 2022 20:56:48 +0200 Subject: [PATCH] Use pydeconz interface controls for lights (#75261) --- homeassistant/components/deconz/gateway.py | 8 ++-- homeassistant/components/deconz/light.py | 44 ++++++++++--------- homeassistant/components/deconz/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/deconz/gateway.py b/homeassistant/components/deconz/gateway.py index 0ee967f88d9..f8e4548cf91 100644 --- a/homeassistant/components/deconz/gateway.py +++ b/homeassistant/components/deconz/gateway.py @@ -10,8 +10,8 @@ from typing import TYPE_CHECKING, Any, cast import async_timeout from pydeconz import DeconzSession, errors from pydeconz.interfaces import sensors -from pydeconz.interfaces.api import APIItems, GroupedAPIItems -from pydeconz.interfaces.groups import Groups +from pydeconz.interfaces.api_handlers import APIHandler, GroupedAPIHandler +from pydeconz.interfaces.groups import GroupHandler from pydeconz.models.event import EventType from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry @@ -127,7 +127,7 @@ class DeconzGateway: def register_platform_add_device_callback( self, add_device_callback: Callable[[EventType, str], None], - deconz_device_interface: APIItems | GroupedAPIItems, + deconz_device_interface: APIHandler | GroupedAPIHandler, always_ignore_clip_sensors: bool = False, ) -> None: """Wrap add_device_callback to check allow_new_devices option.""" @@ -148,7 +148,7 @@ class DeconzGateway: self.ignored_devices.add((async_add_device, device_id)) return - if isinstance(deconz_device_interface, Groups): + if isinstance(deconz_device_interface, GroupHandler): self.deconz_groups.add((async_add_device, device_id)) if not self.option_allow_deconz_groups: return diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index 7be6551cccc..7c6f1a0e362 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -3,16 +3,12 @@ from __future__ import annotations from typing import Any, Generic, TypedDict, TypeVar +from pydeconz.interfaces.groups import GroupHandler +from pydeconz.interfaces.lights import LightHandler from pydeconz.models import ResourceType from pydeconz.models.event import EventType from pydeconz.models.group import Group -from pydeconz.models.light import ( - ALERT_LONG, - ALERT_SHORT, - EFFECT_COLOR_LOOP, - EFFECT_NONE, -) -from pydeconz.models.light.light import Light +from pydeconz.models.light.light import Light, LightAlert, LightColorMode, LightEffect from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -42,8 +38,14 @@ from .deconz_device import DeconzDevice from .gateway import DeconzGateway, get_gateway_from_config_entry DECONZ_GROUP = "is_deconz_group" -EFFECT_TO_DECONZ = {EFFECT_COLORLOOP: EFFECT_COLOR_LOOP, "None": EFFECT_NONE} -FLASH_TO_DECONZ = {FLASH_SHORT: ALERT_SHORT, FLASH_LONG: ALERT_LONG} +EFFECT_TO_DECONZ = {EFFECT_COLORLOOP: LightEffect.COLOR_LOOP, "None": LightEffect.NONE} +FLASH_TO_DECONZ = {FLASH_SHORT: LightAlert.SHORT, FLASH_LONG: LightAlert.LONG} + +DECONZ_TO_COLOR_MODE = { + LightColorMode.CT: ColorMode.COLOR_TEMP, + LightColorMode.HS: ColorMode.HS, + LightColorMode.XY: ColorMode.XY, +} _L = TypeVar("_L", Group, Light) @@ -51,10 +53,10 @@ _L = TypeVar("_L", Group, Light) class SetStateAttributes(TypedDict, total=False): """Attributes available with set state call.""" - alert: str + alert: LightAlert brightness: int color_temperature: int - effect: str + effect: LightEffect hue: int on: bool saturation: int @@ -130,7 +132,13 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity): """Set up light.""" super().__init__(device, gateway) - self._attr_supported_color_modes: set[str] = set() + self.api: GroupHandler | LightHandler + if isinstance(self._device, Light): + self.api = self.gateway.api.lights.lights + elif isinstance(self._device, Group): + self.api = self.gateway.api.groups + + self._attr_supported_color_modes: set[ColorMode] = set() if device.color_temp is not None: self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP) @@ -158,12 +166,8 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity): @property def color_mode(self) -> str | None: """Return the color mode of the light.""" - if self._device.color_mode == "ct": - color_mode = ColorMode.COLOR_TEMP - elif self._device.color_mode == "hs": - color_mode = ColorMode.HS - elif self._device.color_mode == "xy": - color_mode = ColorMode.XY + if self._device.color_mode in DECONZ_TO_COLOR_MODE: + color_mode = DECONZ_TO_COLOR_MODE[self._device.color_mode] elif self._device.brightness is not None: color_mode = ColorMode.BRIGHTNESS else: @@ -229,7 +233,7 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity): if ATTR_EFFECT in kwargs and kwargs[ATTR_EFFECT] in EFFECT_TO_DECONZ: data["effect"] = EFFECT_TO_DECONZ[kwargs[ATTR_EFFECT]] - await self._device.set_state(**data) + await self.api.set_state(id=self._device.resource_id, **data) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off light.""" @@ -246,7 +250,7 @@ class DeconzBaseLight(Generic[_L], DeconzDevice, LightEntity): data["alert"] = FLASH_TO_DECONZ[kwargs[ATTR_FLASH]] del data["on"] - await self._device.set_state(**data) + await self.api.set_state(id=self._device.resource_id, **data) @property def extra_state_attributes(self) -> dict[str, bool]: diff --git a/homeassistant/components/deconz/manifest.json b/homeassistant/components/deconz/manifest.json index c1b0b07de02..8019d0df2df 100644 --- a/homeassistant/components/deconz/manifest.json +++ b/homeassistant/components/deconz/manifest.json @@ -3,7 +3,7 @@ "name": "deCONZ", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/deconz", - "requirements": ["pydeconz==99"], + "requirements": ["pydeconz==100"], "ssdp": [ { "manufacturer": "Royal Philips Electronics", diff --git a/requirements_all.txt b/requirements_all.txt index 4fc5e180e95..ff2640568d8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1447,7 +1447,7 @@ pydaikin==2.7.0 pydanfossair==0.1.0 # homeassistant.components.deconz -pydeconz==99 +pydeconz==100 # homeassistant.components.delijn pydelijn==1.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ff34193cee8..fc576e28612 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -980,7 +980,7 @@ pycoolmasternet-async==0.1.2 pydaikin==2.7.0 # homeassistant.components.deconz -pydeconz==99 +pydeconz==100 # homeassistant.components.dexcom pydexcom==0.2.3