Skip to content

Commit f473c15

Browse files
committed
Fix missing color_mode initialization in MQTT JSON light schema
Initialize `_attr_color_mode` to `ColorMode.UNKNOWN` before the color mode branching logic in `_setup_from_config`, matching the pattern already used in `schema_template.py` (added in #162715). Without this, `_attr_color_mode` can remain `None` (the new default from #162715) in edge cases during entity (re)setup, causing "does not report a color mode" errors when the light is on. Also removes the unused `_fixed_color_mode` class variable.
1 parent b5480da commit f473c15

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

homeassistant/components/mqtt/light/schema_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
146146
_entity_id_format = ENTITY_ID_FORMAT
147147
_attributes_extra_blocked = MQTT_LIGHT_ATTRIBUTES_BLOCKED
148148

149-
_fixed_color_mode: ColorMode | str | None = None
150149
_flash_times: dict[str, int | None]
151150
_topic: dict[str, str | None]
152151
_optimistic: bool
@@ -190,6 +189,7 @@ def _setup_from_config(self, config: ConfigType) -> None:
190189
self._attr_supported_features |= (
191190
config[CONF_TRANSITION] and LightEntityFeature.TRANSITION
192191
)
192+
self._attr_color_mode = ColorMode.UNKNOWN
193193
if supported_color_modes := self._config.get(CONF_SUPPORTED_COLOR_MODES):
194194
self._attr_supported_color_modes = supported_color_modes
195195
if self.supported_color_modes and len(self.supported_color_modes) == 1:

tests/components/mqtt/test_light_json.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,58 @@ async def test_brightness_only(
513513
assert state.state == STATE_OFF
514514

515515

516+
@pytest.mark.parametrize(
517+
"hass_config",
518+
[
519+
{
520+
mqtt.DOMAIN: {
521+
light.DOMAIN: {
522+
"schema": "json",
523+
"name": "test",
524+
"state_topic": "test_light",
525+
"command_topic": "test_light/set",
526+
"supported_color_modes": ["brightness"],
527+
}
528+
}
529+
},
530+
{
531+
mqtt.DOMAIN: {
532+
light.DOMAIN: {
533+
"schema": "json",
534+
"name": "test",
535+
"state_topic": "test_light",
536+
"command_topic": "test_light/set",
537+
"supported_color_modes": ["color_temp"],
538+
}
539+
}
540+
},
541+
],
542+
)
543+
async def test_single_color_mode_turn_on(
544+
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
545+
) -> None:
546+
"""Test turning on a single color mode light does not raise.
547+
548+
Regression test: PR #162715 changed _attr_color_mode default to None
549+
and added a strict check. The JSON schema must initialize color_mode
550+
during setup so that turn_on does not raise "does not report a color mode".
551+
"""
552+
mqtt_mock = await mqtt_mock_entry()
553+
554+
state = hass.states.get("light.test")
555+
assert state.state == STATE_UNKNOWN
556+
557+
# This should not raise "does not report a color mode"
558+
await common.async_turn_on(hass, "light.test")
559+
mqtt_mock.async_publish.assert_called_once_with(
560+
"test_light/set", '{"state":"ON"}', 0, False
561+
)
562+
563+
async_fire_mqtt_message(hass, "test_light", '{"state":"ON", "brightness": 50}')
564+
state = hass.states.get("light.test")
565+
assert state.state == STATE_ON
566+
567+
516568
@pytest.mark.parametrize(
517569
"hass_config",
518570
[

0 commit comments

Comments
 (0)