From d7247c2acea6858cbfb3b5cb49624acaff5c403b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 28 Apr 2021 10:12:20 +0200 Subject: [PATCH] Correct conversion of RGB and XY colors to RGBW (#49802) --- homeassistant/components/light/__init__.py | 5 +- tests/components/light/test_init.py | 66 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 0328da7c1bc..ec94c24c6a7 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -331,7 +331,7 @@ async def async_setup(hass, config): # noqa: C901 elif ATTR_RGB_COLOR in params and COLOR_MODE_RGB not in supported_color_modes: rgb_color = params.pop(ATTR_RGB_COLOR) if COLOR_MODE_RGBW in supported_color_modes: - params[ATTR_RGBW_COLOR] = (*rgb_color, 0) + params[ATTR_RGBW_COLOR] = color_util.color_rgb_to_rgbw(*rgb_color) if COLOR_MODE_RGBWW in supported_color_modes: params[ATTR_RGBWW_COLOR] = (*rgb_color, 0, 0) elif COLOR_MODE_HS in supported_color_modes: @@ -345,7 +345,8 @@ async def async_setup(hass, config): # noqa: C901 elif COLOR_MODE_RGB in supported_color_modes: params[ATTR_RGB_COLOR] = color_util.color_xy_to_RGB(*xy_color) elif COLOR_MODE_RGBW in supported_color_modes: - params[ATTR_RGBW_COLOR] = (*color_util.color_xy_to_RGB(*xy_color), 0) + rgb_color = color_util.color_xy_to_RGB(*xy_color) + params[ATTR_RGBW_COLOR] = color_util.color_rgb_to_rgbw(*rgb_color) elif COLOR_MODE_RGBWW in supported_color_modes: params[ATTR_RGBWW_COLOR] = ( *color_util.color_xy_to_RGB(*xy_color), diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index e52192c62ee..0f2a8a73e9e 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -1367,6 +1367,39 @@ async def test_light_service_call_color_conversion(hass): _, data = entity6.last_call("turn_on") assert data == {"brightness": 128, "rgbww_color": (128, 0, 0, 0, 0)} + await hass.services.async_call( + "light", + "turn_on", + { + "entity_id": [ + entity0.entity_id, + entity1.entity_id, + entity2.entity_id, + entity3.entity_id, + entity4.entity_id, + entity5.entity_id, + entity6.entity_id, + ], + "brightness_pct": 50, + "rgb_color": (255, 255, 255), + }, + blocking=True, + ) + _, data = entity0.last_call("turn_on") + assert data == {"brightness": 128, "hs_color": (0.0, 0.0)} + _, data = entity1.last_call("turn_on") + assert data == {"brightness": 128, "rgb_color": (255, 255, 255)} + _, data = entity2.last_call("turn_on") + assert data == {"brightness": 128, "xy_color": (0.323, 0.329)} + _, data = entity3.last_call("turn_on") + assert data == {"brightness": 128, "rgb_color": (255, 255, 255)} + _, data = entity4.last_call("turn_on") + assert data == {"brightness": 128, "hs_color": (0.0, 0.0)} + _, data = entity5.last_call("turn_on") + assert data == {"brightness": 128, "rgbw_color": (0, 0, 0, 255)} + _, data = entity6.last_call("turn_on") + assert data == {"brightness": 128, "rgbww_color": (255, 255, 255, 0, 0)} + await hass.services.async_call( "light", "turn_on", @@ -1400,6 +1433,39 @@ async def test_light_service_call_color_conversion(hass): _, data = entity6.last_call("turn_on") assert data == {"brightness": 128, "rgbww_color": (0, 255, 22, 0, 0)} + await hass.services.async_call( + "light", + "turn_on", + { + "entity_id": [ + entity0.entity_id, + entity1.entity_id, + entity2.entity_id, + entity3.entity_id, + entity4.entity_id, + entity5.entity_id, + entity6.entity_id, + ], + "brightness_pct": 50, + "xy_color": (0.323, 0.329), + }, + blocking=True, + ) + _, data = entity0.last_call("turn_on") + assert data == {"brightness": 128, "hs_color": (0.0, 0.392)} + _, data = entity1.last_call("turn_on") + assert data == {"brightness": 128, "rgb_color": (255, 254, 254)} + _, data = entity2.last_call("turn_on") + assert data == {"brightness": 128, "xy_color": (0.323, 0.329)} + _, data = entity3.last_call("turn_on") + assert data == {"brightness": 128, "xy_color": (0.323, 0.329)} + _, data = entity4.last_call("turn_on") + assert data == {"brightness": 128, "hs_color": (0.0, 0.392)} + _, data = entity5.last_call("turn_on") + assert data == {"brightness": 128, "rgbw_color": (1, 0, 0, 255)} + _, data = entity6.last_call("turn_on") + assert data == {"brightness": 128, "rgbww_color": (255, 254, 254, 0, 0)} + async def test_light_state_color_conversion(hass): """Test color conversion in state updates."""