diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index 153726a89e8..358a97ed30d 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -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 diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 1e486a3492c..fcdec1fbfe3 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -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(