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

Restore manual alarm-control-panel state using async_get_last_state #17521

Merged
merged 7 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions homeassistant/components/alarm_control_panel/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_point_in_time
import homeassistant.util.dt as dt_util
from homeassistant.helpers.restore_state import async_get_last_state

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -306,3 +307,10 @@ def device_state_attributes(self):
state_attr[ATTR_POST_PENDING_STATE] = self._state

return state_attr

async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
state = await async_get_last_state(self.hass, self.entity_id)
if state:
self._state = state.state
self._state_ts = state.last_updated
55 changes: 50 additions & 5 deletions tests/components/alarm_control_panel/test_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import unittest
from unittest.mock import patch, MagicMock
from homeassistant.components.alarm_control_panel import demo


from homeassistant.setup import setup_component
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_NIGHT, STATE_ALARM_ARMED_CUSTOM_BYPASS,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
from homeassistant.components import alarm_control_panel
import homeassistant.util.dt as dt_util

from tests.common import fire_time_changed, get_test_home_assistant
from tests.common import (fire_time_changed, get_test_home_assistant,
mock_component, mock_restore_cache)
from tests.components.alarm_control_panel import common
from homeassistant.core import State, CoreState

CODE = 'HELLO_CODE'

Expand Down Expand Up @@ -1319,3 +1318,49 @@ def test_arm_away_after_disabled_disarmed(self):

state = self.hass.states.get(entity_id)
self.assertEqual(STATE_ALARM_TRIGGERED, state.state)


async def test_restore_armed_state(hass):
"""Ensure armed state is restored on startup."""
mock_restore_cache(hass, (
State('alarm_control_panel.test', STATE_ALARM_ARMED_AWAY),
))

hass.state = CoreState.starting
mock_component(hass, 'recorder')

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

state = hass.states.get('alarm_control_panel.test')
assert state
assert state.state == STATE_ALARM_ARMED_AWAY


async def test_restore_disarmed_state(hass):
"""Ensure disarmed state is restored on startup."""
mock_restore_cache(hass, (
State('alarm_control_panel.test', STATE_ALARM_DISARMED),
))

hass.state = CoreState.starting
mock_component(hass, 'recorder')

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

state = hass.states.get('alarm_control_panel.test')
assert state
assert state.state == STATE_ALARM_DISARMED