Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/light/schema_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -190,6 +189,7 @@ def _setup_from_config(self, config: ConfigType) -> None:
self._attr_supported_features |= (
config[CONF_TRANSITION] and LightEntityFeature.TRANSITION
)
self._attr_color_mode = ColorMode.UNKNOWN
Comment thread
noerstad marked this conversation as resolved.
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:
Expand Down
52 changes: 52 additions & 0 deletions tests/components/mqtt/test_light_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
jbouwh marked this conversation as resolved.

Comment thread
jbouwh marked this conversation as resolved.

@pytest.mark.parametrize(
"hass_config",
[
Expand Down
Loading