diff --git a/homeassistant/components/mqtt/light/schema_json.py b/homeassistant/components/mqtt/light/schema_json.py index 3479f1611d8..b1e5c1c18d4 100644 --- a/homeassistant/components/mqtt/light/schema_json.py +++ b/homeassistant/components/mqtt/light/schema_json.py @@ -220,6 +220,8 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity): self._attr_supported_color_modes = self._config[CONF_SUPPORTED_COLOR_MODES] if self.supported_color_modes and len(self.supported_color_modes) == 1: self._attr_color_mode = next(iter(self.supported_color_modes)) + else: + self._attr_color_mode = ColorMode.UNKNOWN def _update_color(self, values: dict[str, Any]) -> None: if not self._config[CONF_COLOR_MODE]: diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index c5c24c3ae79..d1fa2b72a31 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -303,6 +303,80 @@ async def test_single_color_mode( assert state.attributes.get(light.ATTR_COLOR_MODE) == color_modes[0] +@pytest.mark.parametrize("hass_config", [COLOR_MODES_CONFIG]) +async def test_turn_on_with_unknown_color_mode_optimistic( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, +) -> None: + """Test setup and turn with unknown color_mode in optimistic mode.""" + await mqtt_mock_entry() + state = hass.states.get("light.test") + assert state.state == STATE_UNKNOWN + + # Turn on the light without brightness or color_temp attributes + await common.async_turn_on(hass, "light.test") + state = hass.states.get("light.test") + assert state.attributes.get("color_mode") == light.ColorMode.UNKNOWN + assert state.attributes.get("brightness") is None + assert state.attributes.get("color_temp") is None + assert state.state == STATE_ON + + # Turn on the light with brightness or color_temp attributes + await common.async_turn_on(hass, "light.test", brightness=50, color_temp=192) + state = hass.states.get("light.test") + assert state.attributes.get("color_mode") == light.ColorMode.COLOR_TEMP + assert state.attributes.get("brightness") == 50 + assert state.attributes.get("color_temp") == 192 + assert state.state == STATE_ON + + +@pytest.mark.parametrize( + "hass_config", + [ + ( + help_custom_config( + light.DOMAIN, + COLOR_MODES_CONFIG, + ({"state_topic": "test_light"},), + ) + ) + ], +) +async def test_controlling_state_with_unknown_color_mode( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, +) -> None: + """Test setup and turn with unknown color_mode in optimistic mode.""" + await mqtt_mock_entry() + state = hass.states.get("light.test") + assert state.state == STATE_UNKNOWN + + # Send `on` state but omit other attributes + async_fire_mqtt_message( + hass, + "test_light", + '{"state": "ON"}', + ) + state = hass.states.get("light.test") + assert state.state == STATE_ON + assert state.attributes.get(light.ATTR_COLOR_TEMP) is None + assert state.attributes.get(light.ATTR_BRIGHTNESS) is None + assert state.attributes.get(light.ATTR_COLOR_MODE) == light.ColorMode.UNKNOWN + + # Send complete light state + async_fire_mqtt_message( + hass, + "test_light", + '{"state": "ON", "brightness": 50, "color_mode": "color_temp", "color_temp": 192}', + ) + state = hass.states.get("light.test") + assert state.state == STATE_ON + + assert state.attributes.get(light.ATTR_COLOR_TEMP) == 192 + assert state.attributes.get(light.ATTR_BRIGHTNESS) == 50 + assert state.attributes.get(light.ATTR_COLOR_MODE) == light.ColorMode.COLOR_TEMP + + @pytest.mark.parametrize( "hass_config", [