From f135d77a27447169fdade12070c354916d6fcbf3 Mon Sep 17 00:00:00 2001 From: schmyd Date: Wed, 22 Dec 2021 09:29:54 +0100 Subject: [PATCH] Fix deconz light service parameter handling (#62128) * Only check presence of values, not their content * Add tests * Revert "Only check presence of values, not their content" This reverts commit 046f0ed5fd631cbac0d26e4d3869ad2c6254c0f9. * Validate existence of keys, not their values * Properly handle cases of missing keys --- homeassistant/components/deconz/light.py | 14 +++++------ tests/components/deconz/test_light.py | 30 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index e287d574633..5330fdb3226 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -199,7 +199,7 @@ class DeconzBaseLight(DeconzDevice, LightEntity): """Turn on light.""" data: dict[str, bool | float | int | str | tuple[float, float]] = {"on": True} - if attr_brightness := kwargs.get(ATTR_BRIGHTNESS): + if (attr_brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None: data["brightness"] = attr_brightness if attr_color_temp := kwargs.get(ATTR_COLOR_TEMP): @@ -215,16 +215,16 @@ class DeconzBaseLight(DeconzDevice, LightEntity): if ATTR_XY_COLOR in kwargs: data["xy"] = kwargs[ATTR_XY_COLOR] - if attr_transition := kwargs.get(ATTR_TRANSITION): + if (attr_transition := kwargs.get(ATTR_TRANSITION)) is not None: data["transition_time"] = int(attr_transition * 10) elif "IKEA" in self._device.manufacturer: data["transition_time"] = 0 - if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH, ""))) is not None: + if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH))) is not None: data["alert"] = alert del data["on"] - if (effect := EFFECT_TO_DECONZ.get(kwargs.get(ATTR_EFFECT, ""))) is not None: + if (effect := EFFECT_TO_DECONZ.get(kwargs.get(ATTR_EFFECT))) is not None: data["effect"] = effect await self._device.set_state(**data) @@ -236,11 +236,11 @@ class DeconzBaseLight(DeconzDevice, LightEntity): data: dict[str, bool | int | str] = {"on": False} - if ATTR_TRANSITION in kwargs: + if (attr_transition := kwargs.get(ATTR_TRANSITION)) is not None: data["brightness"] = 0 - data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10) + data["transition_time"] = int(attr_transition * 10) - if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH, ""))) is not None: + if (alert := FLASH_TO_DECONZ.get(kwargs.get(ATTR_FLASH))) is not None: data["alert"] = alert del data["on"] diff --git a/tests/components/deconz/test_light.py b/tests/components/deconz/test_light.py index c2b12651fc0..2405ed159e6 100644 --- a/tests/components/deconz/test_light.py +++ b/tests/components/deconz/test_light.py @@ -399,6 +399,20 @@ async def test_light_state_change(hass, aioclient_mock, mock_deconz_websocket): "xy": (0.411, 0.351), }, ), + ( # Turn on light without transition time + { + "light_on": True, + "service": SERVICE_TURN_ON, + "call": { + ATTR_ENTITY_ID: "light.hue_go", + ATTR_TRANSITION: 0, + }, + }, + { + "on": True, + "transitiontime": 0, + }, + ), ( # Turn on light with short color loop { "light_on": False, @@ -453,6 +467,22 @@ async def test_light_state_change(hass, aioclient_mock, mock_deconz_websocket): "alert": "select", }, ), + ( # Turn off light without transition time + { + "light_on": True, + "service": SERVICE_TURN_OFF, + "call": { + ATTR_ENTITY_ID: "light.hue_go", + ATTR_TRANSITION: 0, + ATTR_FLASH: FLASH_SHORT, + }, + }, + { + "bri": 0, + "transitiontime": 0, + "alert": "select", + }, + ), ( # Turn off light with long flashing { "light_on": True,