diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 835c4fd2fa9..0b78ed3672e 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -317,11 +317,23 @@ async def async_setup(hass, config): hs_color = params.pop(ATTR_HS_COLOR) if COLOR_MODE_RGB in supported_color_modes: params[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color) + elif COLOR_MODE_RGBW in supported_color_modes: + params[ATTR_RGBW_COLOR] = (*color_util.color_hs_to_RGB(*hs_color), 0) + elif COLOR_MODE_RGBWW in supported_color_modes: + params[ATTR_RGBWW_COLOR] = ( + *color_util.color_hs_to_RGB(*hs_color), + 0, + 0, + ) elif COLOR_MODE_XY in supported_color_modes: params[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color) 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_HS in supported_color_modes: + if COLOR_MODE_RGBW in supported_color_modes: + params[ATTR_RGBW_COLOR] = (*rgb_color, 0) + if COLOR_MODE_RGBWW in supported_color_modes: + params[ATTR_RGBWW_COLOR] = (*rgb_color, 0, 0) + elif COLOR_MODE_HS in supported_color_modes: params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color) elif COLOR_MODE_XY in supported_color_modes: params[ATTR_XY_COLOR] = color_util.color_RGB_to_xy(*rgb_color) @@ -331,6 +343,14 @@ async def async_setup(hass, config): params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color) 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) + elif COLOR_MODE_RGBWW in supported_color_modes: + params[ATTR_RGBWW_COLOR] = ( + *color_util.color_xy_to_RGB(*xy_color), + 0, + 0, + ) # Remove deprecated white value if the light supports color mode if supported_color_modes: diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 3adb146a225..f0cca89892c 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -1209,6 +1209,8 @@ async def test_light_service_call_color_conversion(hass): platform.ENTITIES.append(platform.MockLight("Test_xy", STATE_ON)) platform.ENTITIES.append(platform.MockLight("Test_all", STATE_ON)) platform.ENTITIES.append(platform.MockLight("Test_legacy", STATE_ON)) + platform.ENTITIES.append(platform.MockLight("Test_rgbw", STATE_ON)) + platform.ENTITIES.append(platform.MockLight("Test_rgbww", STATE_ON)) entity0 = platform.ENTITIES[0] entity0.supported_color_modes = {light.COLOR_MODE_HS} @@ -1229,6 +1231,12 @@ async def test_light_service_call_color_conversion(hass): entity4 = platform.ENTITIES[4] entity4.supported_features = light.SUPPORT_COLOR + entity5 = platform.ENTITIES[5] + entity5.supported_color_modes = {light.COLOR_MODE_RGBW} + + entity6 = platform.ENTITIES[6] + entity6.supported_color_modes = {light.COLOR_MODE_RGBWW} + assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) await hass.async_block_till_done() @@ -1251,6 +1259,12 @@ async def test_light_service_call_color_conversion(hass): state = hass.states.get(entity4.entity_id) assert state.attributes["supported_color_modes"] == [light.COLOR_MODE_HS] + state = hass.states.get(entity5.entity_id) + assert state.attributes["supported_color_modes"] == [light.COLOR_MODE_RGBW] + + state = hass.states.get(entity6.entity_id) + assert state.attributes["supported_color_modes"] == [light.COLOR_MODE_RGBWW] + await hass.services.async_call( "light", "turn_on", @@ -1261,6 +1275,8 @@ async def test_light_service_call_color_conversion(hass): entity2.entity_id, entity3.entity_id, entity4.entity_id, + entity5.entity_id, + entity6.entity_id, ], "brightness_pct": 100, "hs_color": (240, 100), @@ -1277,6 +1293,10 @@ async def test_light_service_call_color_conversion(hass): assert data == {"brightness": 255, "hs_color": (240.0, 100.0)} _, data = entity4.last_call("turn_on") assert data == {"brightness": 255, "hs_color": (240.0, 100.0)} + _, data = entity5.last_call("turn_on") + assert data == {"brightness": 255, "rgbw_color": (0, 0, 255, 0)} + _, data = entity6.last_call("turn_on") + assert data == {"brightness": 255, "rgbww_color": (0, 0, 255, 0, 0)} await hass.services.async_call( "light", @@ -1288,6 +1308,8 @@ async def test_light_service_call_color_conversion(hass): entity2.entity_id, entity3.entity_id, entity4.entity_id, + entity5.entity_id, + entity6.entity_id, ], "brightness_pct": 50, "rgb_color": (128, 0, 0), @@ -1304,6 +1326,10 @@ async def test_light_service_call_color_conversion(hass): assert data == {"brightness": 128, "rgb_color": (128, 0, 0)} _, data = entity4.last_call("turn_on") assert data == {"brightness": 128, "hs_color": (0.0, 100.0)} + _, data = entity5.last_call("turn_on") + assert data == {"brightness": 128, "rgbw_color": (128, 0, 0, 0)} + _, data = entity6.last_call("turn_on") + assert data == {"brightness": 128, "rgbww_color": (128, 0, 0, 0, 0)} await hass.services.async_call( "light", @@ -1315,6 +1341,8 @@ async def test_light_service_call_color_conversion(hass): entity2.entity_id, entity3.entity_id, entity4.entity_id, + entity5.entity_id, + entity6.entity_id, ], "brightness_pct": 50, "xy_color": (0.1, 0.8), @@ -1331,6 +1359,10 @@ async def test_light_service_call_color_conversion(hass): assert data == {"brightness": 128, "xy_color": (0.1, 0.8)} _, data = entity4.last_call("turn_on") assert data == {"brightness": 128, "hs_color": (125.176, 100.0)} + _, data = entity5.last_call("turn_on") + assert data == {"brightness": 128, "rgbw_color": (0, 255, 22, 0)} + _, data = entity6.last_call("turn_on") + assert data == {"brightness": 128, "rgbww_color": (0, 255, 22, 0, 0)} async def test_light_state_color_conversion(hass):