Skip to content

Commit

Permalink
Fix manual.alarm_control_panel RestoreEntity bugs (#82990)
Browse files Browse the repository at this point in the history
* manual: add previous state for TRIGGERED as well

This will be useful to properly restore timers on restart.

* manual: correctly restore timers on startup
  • Loading branch information
bonzini committed Dec 19, 2022
1 parent 20f0aba commit 5259471
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 18 deletions.
25 changes: 16 additions & 9 deletions homeassistant/components/manual/alarm_control_panel.py
Expand Up @@ -358,7 +358,10 @@ def _async_update_state(self, state: str) -> None:
self._state = state
self._state_ts = dt_util.utcnow()
self.async_schedule_update_ha_state()
self._async_set_state_update_events()

def _async_set_state_update_events(self) -> None:
state = self._state
if state == STATE_ALARM_TRIGGERED:
pending_time = self._pending_time(state)
async_track_point_in_time(
Expand Down Expand Up @@ -403,6 +406,10 @@ def extra_state_attributes(self) -> dict[str, Any]:
ATTR_PREVIOUS_STATE: self._previous_state,
ATTR_NEXT_STATE: self._state,
}
if self.state == STATE_ALARM_TRIGGERED:
return {
ATTR_PREVIOUS_STATE: self._previous_state,
}
return {}

@callback
Expand All @@ -414,14 +421,14 @@ async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
if state := await self.async_get_last_state():
if (
state.state in (STATE_ALARM_PENDING, STATE_ALARM_ARMING)
and hasattr(state, "attributes")
and state.attributes[ATTR_PREVIOUS_STATE]
):
# If in arming or pending state, we return to the ATTR_PREVIOUS_STATE
self._state = state.attributes[ATTR_PREVIOUS_STATE]
self._state_ts = dt_util.utcnow()
self._state_ts = state.last_updated
if hasattr(state, "attributes") and ATTR_NEXT_STATE in state.attributes:
# If in arming or pending state we record the transition,
# not the current state
self._state = state.attributes[ATTR_NEXT_STATE]
else:
self._state = state.state
self._state_ts = state.last_updated

if hasattr(state, "attributes") and ATTR_PREVIOUS_STATE in state.attributes:
self._previous_state = state.attributes[ATTR_PREVIOUS_STATE]
self._async_set_state_update_events()

0 comments on commit 5259471

Please sign in to comment.