Use unknown color_mode for MQTT json lights if color mode is not set (#108909)

pull/108918/head
Jan Bouwhuis 2024-01-26 14:42:47 +01:00 committed by GitHub
parent 00c2ba69f7
commit 7bec5ef6bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 0 deletions

View File

@ -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]:

View File

@ -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",
[