From 1d643d6da7a6c280fc014b2e1923fc30869f94f5 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sun, 3 Oct 2021 22:14:28 +0200 Subject: [PATCH] Minor improvements to deCONZ light platform (#56953) Use library constnats for flash and effect Use attr_effect_list to specify supported effects Use isinstance to identify if it is light or group --- homeassistant/components/deconz/light.py | 46 +++++++++++------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index 2202bdbe58f..1a3cca6df05 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -2,7 +2,14 @@ from __future__ import annotations -from pydeconz.light import Light +from pydeconz.group import DeconzGroup as Group +from pydeconz.light import ( + ALERT_LONG, + ALERT_SHORT, + EFFECT_COLOR_LOOP, + EFFECT_NONE, + Light, +) from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -35,6 +42,8 @@ from .deconz_device import DeconzDevice from .gateway import 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} async def async_setup_entry(hass, config_entry, async_add_entities): @@ -126,6 +135,7 @@ class DeconzBaseLight(DeconzDevice, LightEntity): if device.effect is not None: self._attr_supported_features |= SUPPORT_EFFECT + self._attr_effect_list = [EFFECT_COLORLOOP] @property def color_mode(self) -> str: @@ -147,11 +157,6 @@ class DeconzBaseLight(DeconzDevice, LightEntity): """Return the brightness of this light between 0..255.""" return self._device.brightness - @property - def effect_list(self): - """Return the list of supported effects.""" - return [EFFECT_COLORLOOP] - @property def color_temp(self): """Return the CT color value.""" @@ -197,19 +202,12 @@ class DeconzBaseLight(DeconzDevice, LightEntity): elif "IKEA" in self._device.manufacturer: data["transition_time"] = 0 - if ATTR_FLASH in kwargs: - if kwargs[ATTR_FLASH] == FLASH_SHORT: - data["alert"] = "select" - del data["on"] - elif kwargs[ATTR_FLASH] == FLASH_LONG: - data["alert"] = "lselect" - del data["on"] + if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH))) is not None: + data["alert"] = alert + del data["on"] - if ATTR_EFFECT in kwargs: - if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP: - data["effect"] = "colorloop" - else: - data["effect"] = "none" + if (effect := EFFECT_TO_DECONZ.get(kwargs.get(ATTR_EFFECT))) is not None: + data["effect"] = effect await self._device.set_state(**data) @@ -224,20 +222,16 @@ class DeconzBaseLight(DeconzDevice, LightEntity): data["brightness"] = 0 data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10) - if ATTR_FLASH in kwargs: - if kwargs[ATTR_FLASH] == FLASH_SHORT: - data["alert"] = "select" - del data["on"] - elif kwargs[ATTR_FLASH] == FLASH_LONG: - data["alert"] = "lselect" - del data["on"] + if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH))) is not None: + data["alert"] = alert + del data["on"] await self._device.set_state(**data) @property def extra_state_attributes(self): """Return the device state attributes.""" - return {DECONZ_GROUP: self._device.type == "LightGroup"} + return {DECONZ_GROUP: isinstance(self._device, Group)} class DeconzLight(DeconzBaseLight):