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:
parent
e5ce8e920d
commit
1a4b14c217
2 changed files with 28 additions and 5 deletions
|
@ -495,8 +495,12 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||
self._attr_color_mode = color_mode
|
||||
if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is None:
|
||||
rgb = convert_color(*color)
|
||||
percent_bright = float(color_util.color_RGB_to_hsv(*rgb)[2]) / 100.0
|
||||
self._attr_brightness = min(round(percent_bright * 255), 255)
|
||||
brightness = max(rgb)
|
||||
self._attr_brightness = brightness
|
||||
# Normalize the color to 100% brightness
|
||||
color = tuple(
|
||||
min(round(channel / brightness * 255), 255) for channel in color
|
||||
)
|
||||
return color
|
||||
|
||||
@callback
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue