mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix restore state for light when saved attribute is None (#103096)
This commit is contained in:
parent
a851907f78
commit
0304ac5a1b
@ -149,31 +149,29 @@ async def _async_reproduce_state(
|
|||||||
service = SERVICE_TURN_ON
|
service = SERVICE_TURN_ON
|
||||||
for attr in ATTR_GROUP:
|
for attr in ATTR_GROUP:
|
||||||
# All attributes that are not colors
|
# All attributes that are not colors
|
||||||
if attr in state.attributes:
|
if (attr_state := state.attributes.get(attr)) is not None:
|
||||||
service_data[attr] = state.attributes[attr]
|
service_data[attr] = attr_state
|
||||||
|
|
||||||
if (
|
if (
|
||||||
state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
|
state.attributes.get(ATTR_COLOR_MODE, ColorMode.UNKNOWN)
|
||||||
!= ColorMode.UNKNOWN
|
!= ColorMode.UNKNOWN
|
||||||
):
|
):
|
||||||
color_mode = state.attributes[ATTR_COLOR_MODE]
|
color_mode = state.attributes[ATTR_COLOR_MODE]
|
||||||
if color_mode_attr := COLOR_MODE_TO_ATTRIBUTE.get(color_mode):
|
if cm_attr := COLOR_MODE_TO_ATTRIBUTE.get(color_mode):
|
||||||
if color_mode_attr.state_attr not in state.attributes:
|
if (cm_attr_state := state.attributes.get(cm_attr.state_attr)) is None:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Color mode %s specified but attribute %s missing for: %s",
|
"Color mode %s specified but attribute %s missing for: %s",
|
||||||
color_mode,
|
color_mode,
|
||||||
color_mode_attr.state_attr,
|
cm_attr.state_attr,
|
||||||
state.entity_id,
|
state.entity_id,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
service_data[color_mode_attr.parameter] = state.attributes[
|
service_data[cm_attr.parameter] = cm_attr_state
|
||||||
color_mode_attr.state_attr
|
|
||||||
]
|
|
||||||
else:
|
else:
|
||||||
# Fall back to Choosing the first color that is specified
|
# Fall back to Choosing the first color that is specified
|
||||||
for color_attr in COLOR_GROUP:
|
for color_attr in COLOR_GROUP:
|
||||||
if color_attr in state.attributes:
|
if (color_attr_state := state.attributes.get(color_attr)) is not None:
|
||||||
service_data[color_attr] = state.attributes[color_attr]
|
service_data[color_attr] = color_attr_state
|
||||||
break
|
break
|
||||||
|
|
||||||
elif state.state == STATE_OFF:
|
elif state.state == STATE_OFF:
|
||||||
|
@ -22,6 +22,20 @@ VALID_RGBW_COLOR = {"rgbw_color": (255, 63, 111, 10)}
|
|||||||
VALID_RGBWW_COLOR = {"rgbww_color": (255, 63, 111, 10, 20)}
|
VALID_RGBWW_COLOR = {"rgbww_color": (255, 63, 111, 10, 20)}
|
||||||
VALID_XY_COLOR = {"xy_color": (0.59, 0.274)}
|
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(
|
async def test_reproducing_states(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
@ -237,3 +251,39 @@ async def test_deprecation_warning(
|
|||||||
)
|
)
|
||||||
assert len(turn_on_calls) == 1
|
assert len(turn_on_calls) == 1
|
||||||
assert DEPRECATION_WARNING % ["brightness_pct"] in caplog.text
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user