Fix restore state for light when saved attribute is None (#103096)

This commit is contained in:
Erik Montnemery 2023-10-31 10:05:03 +01:00 committed by GitHub
parent a851907f78
commit 0304ac5a1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 10 deletions

View file

@ -149,31 +149,29 @@ async def _async_reproduce_state(
service = SERVICE_TURN_ON
for attr in ATTR_GROUP:
# All attributes that are not colors
if attr in state.attributes:
service_data[attr] = state.attributes[attr]
if (attr_state := state.attributes.get(attr)) is not None:
service_data[attr] = attr_state
if (
state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
!= ColorMode.UNKNOWN
):
color_mode = state.attributes[ATTR_COLOR_MODE]
if color_mode_attr := COLOR_MODE_TO_ATTRIBUTE.get(color_mode):
if color_mode_attr.state_attr not in state.attributes:
if cm_attr := COLOR_MODE_TO_ATTRIBUTE.get(color_mode):
if (cm_attr_state := state.attributes.get(cm_attr.state_attr)) is None:
_LOGGER.warning(
"Color mode %s specified but attribute %s missing for: %s",
color_mode,
color_mode_attr.state_attr,
cm_attr.state_attr,
state.entity_id,
)
return
service_data[color_mode_attr.parameter] = state.attributes[
color_mode_attr.state_attr
]
service_data[cm_attr.parameter] = cm_attr_state
else:
# Fall back to Choosing the first color that is specified
for color_attr in COLOR_GROUP:
if color_attr in state.attributes:
service_data[color_attr] = state.attributes[color_attr]
if (color_attr_state := state.attributes.get(color_attr)) is not None:
service_data[color_attr] = color_attr_state
break
elif state.state == STATE_OFF:

View file

@ -22,6 +22,20 @@ VALID_RGBW_COLOR = {"rgbw_color": (255, 63, 111, 10)}
VALID_RGBWW_COLOR = {"rgbww_color": (255, 63, 111, 10, 20)}
VALID_XY_COLOR = {"xy_color": (0.59, 0.274)}
NONE_BRIGHTNESS = {"brightness": None}
NONE_FLASH = {"flash": None}
NONE_EFFECT = {"effect": None}
NONE_TRANSITION = {"transition": None}
NONE_COLOR_NAME = {"color_name": None}
NONE_COLOR_TEMP = {"color_temp": None}
NONE_HS_COLOR = {"hs_color": None}
NONE_KELVIN = {"kelvin": None}
NONE_PROFILE = {"profile": None}
NONE_RGB_COLOR = {"rgb_color": None}
NONE_RGBW_COLOR = {"rgbw_color": None}
NONE_RGBWW_COLOR = {"rgbww_color": None}
NONE_XY_COLOR = {"xy_color": None}
async def test_reproducing_states(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
@ -237,3 +251,39 @@ async def test_deprecation_warning(
)
assert len(turn_on_calls) == 1
assert DEPRECATION_WARNING % ["brightness_pct"] in caplog.text
@pytest.mark.parametrize(
"saved_state",
(
NONE_BRIGHTNESS,
NONE_FLASH,
NONE_EFFECT,
NONE_TRANSITION,
NONE_COLOR_NAME,
NONE_COLOR_TEMP,
NONE_HS_COLOR,
NONE_KELVIN,
NONE_PROFILE,
NONE_RGB_COLOR,
NONE_RGBW_COLOR,
NONE_RGBWW_COLOR,
NONE_XY_COLOR,
),
)
async def test_filter_none(hass: HomeAssistant, saved_state) -> None:
"""Test filtering of parameters which are None."""
hass.states.async_set("light.entity", "off", {})
turn_on_calls = async_mock_service(hass, "light", "turn_on")
await async_reproduce_state(hass, [State("light.entity", "on", saved_state)])
assert len(turn_on_calls) == 1
assert turn_on_calls[0].domain == "light"
assert dict(turn_on_calls[0].data) == {"entity_id": "light.entity"}
# This should do nothing, the light is already in the desired state
hass.states.async_set("light.entity", "on", {})
await async_reproduce_state(hass, [State("light.entity", "on", saved_state)])
assert len(turn_on_calls) == 1