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
15 changes: 9 additions & 6 deletions homeassistant/components/manual/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,15 @@ async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
if state := await self.async_get_last_state():
self._state_ts = state.last_updated
if next_state := state.attributes.get(ATTR_NEXT_STATE):
# If in arming or pending state we record the transition,
# not the current state
self._state = AlarmControlPanelState(next_state)
else:
self._state = AlarmControlPanelState(state.state)
try:
if next_state := state.attributes.get(ATTR_NEXT_STATE):
# If in arming or pending state we record the transition,
# not the current state
self._state = AlarmControlPanelState(next_state)
else:
self._state = AlarmControlPanelState(state.state)
except ValueError:
return

if prev_state := state.attributes.get(ATTR_PREVIOUS_STATE):
self._previous_state = prev_state
Expand Down
27 changes: 27 additions & 0 deletions tests/components/manual/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,33 @@ async def test_restore_state(hass: HomeAssistant, expected_state) -> None:
assert state.state == expected_state


async def test_restore_state_invalid(hass: HomeAssistant) -> None:
"""Ensure invalid restored state does not crash entity setup."""
mock_restore_cache(hass, (State("alarm_control_panel.test", "unknown"),))

hass.set_state(CoreState.starting)
mock_component(hass, "recorder")

assert await async_setup_component(
hass,
alarm_control_panel.DOMAIN,
{
"alarm_control_panel": {
"platform": "manual",
"name": "test",
"arming_time": 0,
"trigger_time": 0,
"disarm_after_trigger": False,
}
},
)
await hass.async_block_till_done()

state = hass.states.get("alarm_control_panel.test")
assert state
assert state.state == AlarmControlPanelState.DISARMED


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