Improve color conversion for RGBWW lights (#49807)

This commit is contained in:
Erik Montnemery
2021-04-28 15:46:41 +02:00
committed by GitHub
parent 9e1042d9e0
commit e96cbccc92
4 changed files with 123 additions and 31 deletions
+39 -3
View File
@@ -1202,6 +1202,39 @@ async def test_light_state_rgbw(hass):
}
async def test_light_state_rgbww(hass):
"""Test rgbww color conversion in state updates."""
platform = getattr(hass.components, "test.light")
platform.init(empty=True)
platform.ENTITIES.append(platform.MockLight("Test_rgbww", STATE_ON))
entity0 = platform.ENTITIES[0]
entity0.supported_color_modes = {light.COLOR_MODE_RGBWW}
entity0.color_mode = light.COLOR_MODE_RGBWW
entity0.hs_color = "Invalid" # Should be ignored
entity0.rgb_color = "Invalid" # Should be ignored
entity0.rgbw_color = "Invalid" # Should be ignored
entity0.rgbww_color = (1, 2, 3, 4, 5)
entity0.white_value = "Invalid" # Should be ignored
entity0.xy_color = "Invalid" # Should be ignored
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
await hass.async_block_till_done()
state = hass.states.get(entity0.entity_id)
assert dict(state.attributes) == {
"color_mode": light.COLOR_MODE_RGBWW,
"friendly_name": "Test_rgbww",
"supported_color_modes": [light.COLOR_MODE_RGBWW],
"supported_features": 0,
"hs_color": (60.0, 20.0),
"rgb_color": (5, 5, 4),
"rgbww_color": (1, 2, 3, 4, 5),
"xy_color": (0.339, 0.354),
}
async def test_light_service_call_color_conversion(hass):
"""Test color conversion in service calls."""
platform = getattr(hass.components, "test.light")
@@ -1332,7 +1365,8 @@ async def test_light_service_call_color_conversion(hass):
_, data = entity5.last_call("turn_on")
assert data == {"brightness": 255, "rgbw_color": (0, 0, 0, 255)}
_, data = entity6.last_call("turn_on")
assert data == {"brightness": 255, "rgbww_color": (255, 255, 255, 0, 0)}
# The midpoint the the white channels is warm, compensated by adding green + blue
assert data == {"brightness": 255, "rgbww_color": (0, 76, 141, 255, 255)}
await hass.services.async_call(
"light",
@@ -1398,7 +1432,8 @@ async def test_light_service_call_color_conversion(hass):
_, 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)}
# The midpoint the the white channels is warm, compensated by adding green + blue
assert data == {"brightness": 128, "rgbww_color": (0, 76, 141, 255, 255)}
await hass.services.async_call(
"light",
@@ -1464,7 +1499,8 @@ async def test_light_service_call_color_conversion(hass):
_, 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)}
# The midpoint the the white channels is warm, compensated by adding green + blue
assert data == {"brightness": 128, "rgbww_color": (0, 75, 140, 255, 255)}
async def test_light_state_color_conversion(hass):
+6 -6
View File
@@ -480,12 +480,12 @@ async def test_controlling_state_via_topic2(hass, mqtt_mock, caplog):
assert state.attributes.get("color_mode") == "rgbww"
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") == "colorloop"
assert state.attributes.get("hs_color") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("hs_color") == (20.552, 70.98)
assert state.attributes.get("rgb_color") == (255, 136, 74)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") == (255, 128, 64, 32, 16)
assert state.attributes.get("white_value") is None
assert state.attributes.get("xy_color") is None
assert state.attributes.get("xy_color") == (0.571, 0.361)
# Light turned off
async_fire_mqtt_message(hass, "test_light_rgb", '{"state":"OFF"}')
@@ -892,11 +892,11 @@ async def test_sending_mqtt_commands_and_optimistic2(hass, mqtt_mock):
assert state.attributes["brightness"] == 75
assert state.attributes["color_mode"] == "rgbww"
assert state.attributes["rgbww_color"] == (255, 128, 0, 45, 32)
assert "hs_color" not in state.attributes
assert "rgb_color" not in state.attributes
assert state.attributes["hs_color"] == (29.872, 92.157)
assert state.attributes["rgb_color"] == (255, 137, 20)
assert "rgbw_color" not in state.attributes
assert "white_value" not in state.attributes
assert "xy_color" not in state.attributes
assert state.attributes["xy_color"] == (0.596, 0.382)
mqtt_mock.async_publish.assert_called_once_with(
"test_light_rgb/set",
JsonValidator(