Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mqtt json light state updates using deprecated color handling #105283

Merged
merged 4 commits into from Dec 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions homeassistant/components/mqtt/light/schema_json.py
Expand Up @@ -406,6 +406,9 @@ def state_received(msg: ReceiveMessage) -> None:
values["color_temp"],
self.entity_id,
)
# Allow to switch back to color_temp
if "color" not in values:
self._attr_hs_color = None
Comment on lines +409 to +411
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With legacy color handling _attr_hs_color is used to determine the color mode, so we should reset it if we switch to color_temp:

https://github.com/home-assistant/core/pull/105283/files#diff-cf35ae315d8066bc8f6fe286f36504d3f522cf3d6c7088af1ab409756b2badc2R459-L467


if self.supported_features and LightEntityFeature.EFFECT:
with suppress(KeyError):
Expand Down
87 changes: 87 additions & 0 deletions tests/components/mqtt/test_light_json.py
Expand Up @@ -725,6 +725,93 @@ async def test_controlling_state_via_topic2(
)


@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
light.DOMAIN: {
"schema": "json",
"name": "test",
"command_topic": "test_light_rgb/set",
"state_topic": "test_light_rgb/set",
"rgb": True,
"color_temp": True,
"brightness": True,
}
}
}
],
)
async def test_controlling_the_state_with_legacy_color_handling(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test state updates for lights with a legacy color handling."""
supported_color_modes = ["color_temp", "hs"]
await mqtt_mock_entry()

state = hass.states.get("light.test")
assert state.state == STATE_UNKNOWN
expected_features = light.SUPPORT_FLASH | light.SUPPORT_TRANSITION
assert state.attributes.get(ATTR_SUPPORTED_FEATURES) == expected_features
assert state.attributes.get("brightness") is None
assert state.attributes.get("color_mode") is None
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") is None
assert state.attributes.get("rgb_color") is None
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("supported_color_modes") == supported_color_modes
assert state.attributes.get("xy_color") is None
assert not state.attributes.get(ATTR_ASSUMED_STATE)

for _ in range(0, 2):
# Returned state after the light was turned on
# Receiving legacy color mode: rgb.
async_fire_mqtt_message(
hass,
"test_light_rgb/set",
'{ "state": "ON", "brightness": 255, "level": 100, "hue": 16,'
'"saturation": 100, "color": { "r": 255, "g": 67, "b": 0 }, '
'"bulb_mode": "color", "color_mode": "rgb" }',
)

state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_mode") == "hs"
assert state.attributes.get("color_temp") is None
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") == (15.765, 100.0)
assert state.attributes.get("rgb_color") == (255, 67, 0)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("xy_color") == (0.674, 0.322)

# Returned state after the lights color mode was changed
# Receiving legacy color mode: color_temp
async_fire_mqtt_message(
hass,
"test_light_rgb/set",
'{ "state": "ON", "brightness": 255, "level": 100, '
'"kelvin": 92, "color_temp": 353, "bulb_mode": "white", '
'"color_mode": "color_temp" }',
)

state = hass.states.get("light.test")
assert state.state == STATE_ON
assert state.attributes.get("brightness") == 255
assert state.attributes.get("color_mode") == "color_temp"
assert state.attributes.get("color_temp") == 353
assert state.attributes.get("effect") is None
assert state.attributes.get("hs_color") == (28.125, 61.661)
assert state.attributes.get("rgb_color") == (255, 171, 97)
assert state.attributes.get("rgbw_color") is None
assert state.attributes.get("rgbww_color") is None
assert state.attributes.get("xy_color") == (0.513, 0.386)


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