From fd6b5ed072db2c1ca17a3e16a49f3ec4a4a4e47c Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 12 Jul 2021 08:17:50 +0200 Subject: [PATCH] Prefer using xy over hs when supported by light (#52883) --- homeassistant/components/deconz/light.py | 8 ++++-- tests/components/deconz/test_light.py | 34 +++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index 90d5e82af71..058147189e6 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -26,6 +26,7 @@ from homeassistant.components.light import ( ) from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.util.color import color_hs_to_xy from .const import ( COVER_TYPES, @@ -189,8 +190,11 @@ class DeconzBaseLight(DeconzDevice, LightEntity): data["ct"] = kwargs[ATTR_COLOR_TEMP] if ATTR_HS_COLOR in kwargs: - data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535) - data["sat"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255) + if COLOR_MODE_XY in self._attr_supported_color_modes: + data["xy"] = color_hs_to_xy(*kwargs[ATTR_HS_COLOR]) + else: + data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535) + data["sat"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255) if ATTR_XY_COLOR in kwargs: data["xy"] = kwargs[ATTR_XY_COLOR] diff --git a/tests/components/deconz/test_light.py b/tests/components/deconz/test_light.py index 2beb339dd4f..42dc04fc7ae 100644 --- a/tests/components/deconz/test_light.py +++ b/tests/components/deconz/test_light.py @@ -371,6 +371,34 @@ async def test_light_state_change(hass, aioclient_mock, mock_deconz_websocket): @pytest.mark.parametrize( "input,expected", [ + ( # Turn on light with hue and sat + { + "light_on": True, + "service": SERVICE_TURN_ON, + "call": { + ATTR_ENTITY_ID: "light.hue_go", + ATTR_HS_COLOR: (20, 30), + }, + }, + { + "on": True, + "xy": (0.411, 0.351), + }, + ), + ( # Turn on light with XY color + { + "light_on": True, + "service": SERVICE_TURN_ON, + "call": { + ATTR_ENTITY_ID: "light.hue_go", + ATTR_XY_COLOR: (0.411, 0.351), + }, + }, + { + "on": True, + "xy": (0.411, 0.351), + }, + ), ( # Turn on light with short color loop { "light_on": False, @@ -811,9 +839,8 @@ async def test_groups(hass, aioclient_mock, input, expected): }, }, { - "hue": 45510, "on": True, - "sat": 127, + "xy": (0.235, 0.164), }, ), ( # Turn on group with short color loop @@ -827,9 +854,8 @@ async def test_groups(hass, aioclient_mock, input, expected): }, }, { - "hue": 45510, "on": True, - "sat": 127, + "xy": (0.235, 0.164), }, ), ],