Fix MQTT rgb light brightness scaling (#89264)

* Normalize received RGB colors to 100% brightness

* Assert on rgb_color attribute

* Use max for RGB to get brightness

* Avoid division and add clamp

* remove clamp

Co-authored-by: Erik Montnemery <erik@montnemery.com>

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis
2023-03-09 08:02:59 +01:00
committed by GitHub
parent e5ce8e920d
commit 1a4b14c217
2 changed files with 28 additions and 5 deletions

View File

@ -636,8 +636,8 @@ async def test_brightness_from_rgb_controlling_scale(
}
},
)
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
@ -650,10 +650,29 @@ async def test_brightness_from_rgb_controlling_scale(
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 255
async_fire_mqtt_message(hass, "test_scale_rgb/rgb/status", "127,0,0")
async_fire_mqtt_message(hass, "test_scale_rgb/rgb/status", "128,64,32")
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 127
assert state.attributes.get("brightness") == 128
assert state.attributes.get("rgb_color") == (255, 128, 64)
mqtt_mock.async_publish.reset_mock()
await common.async_turn_on(hass, "light.test", brightness=191)
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_has_calls(
[
call("test_scale_rgb/set", "on", 0, False),
call("test_scale_rgb/rgb/set", "191,95,47", 0, False),
],
any_order=True,
)
async_fire_mqtt_message(hass, "test_scale_rgb/rgb/status", "191,95,47")
await hass.async_block_till_done()
state = hass.states.get("light.test")
assert state.attributes.get("brightness") == 191
assert state.attributes.get("rgb_color") == (255, 127, 63)
async def test_controlling_state_via_topic_with_templates(