Fix missing color_mode initialization in MQTT JSON light schema (#167429)

This commit is contained in:
Nils Ove Erstad
2026-04-07 09:20:48 +02:00
committed by GitHub
parent 4dba27f15e
commit 74a6f781a1
2 changed files with 53 additions and 1 deletions
@@ -146,7 +146,6 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
_entity_id_format = ENTITY_ID_FORMAT
_attributes_extra_blocked = MQTT_LIGHT_ATTRIBUTES_BLOCKED
_fixed_color_mode: ColorMode | str | None = None
_flash_times: dict[str, int | None]
_topic: dict[str, str | None]
_optimistic: bool
@@ -190,6 +189,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
self._attr_supported_features |= (
config[CONF_TRANSITION] and LightEntityFeature.TRANSITION
)
self._attr_color_mode = ColorMode.UNKNOWN
if supported_color_modes := self._config.get(CONF_SUPPORTED_COLOR_MODES):
self._attr_supported_color_modes = supported_color_modes
if self.supported_color_modes and len(self.supported_color_modes) == 1:
+52
View File
@@ -513,6 +513,58 @@ async def test_brightness_only(
assert state.state == STATE_OFF
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"state_topic": "test_light",
"command_topic": "test_light/set",
"supported_color_modes": ["brightness"],
}
}
},
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"state_topic": "test_light",
"command_topic": "test_light/set",
"supported_color_modes": ["color_temp"],
}
}
},
],
)
async def test_single_color_mode_turn_on(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test turning on a single color mode light does not raise.
Regression test: PR #162715 changed _attr_color_mode default to None
and added a strict check. The JSON schema must initialize color_mode
during setup so that turn_on does not raise "does not report a color mode".
"""
mqtt_mock = await mqtt_mock_entry()
state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
# This should not raise "does not report a color mode"
await common.async_turn_on(hass, "light.test")
mqtt_mock.async_publish.assert_called_once_with(
"test_light/set", '{"state":"ON"}', 0, False
)
async_fire_mqtt_message(hass, "test_light", '{"state":"ON", "brightness": 50}')
state = hass.states.get("light.test")
assert state.state == STATE_ON
@pytest.mark.parametrize(
"hass_config",
[