Add manual alarm_control_panel pending time per state (#9264)
* - Enhanced manual alarm_control_panel config so that you can specify different pending time for different alarm state * - Fixed demo alaram control panel * - Updated configuration structure for state specific pending times * - Addressed comment * Address code review comments * - Fixed failing tests - Updated demo alarm component to use new per state pending_time setting * - Removing previously added comment which might have caused build to fail? * - moved "copy.deepcopy(config)" out of loop so config is only copied oncepull/8875/merge
parent
20f3e3dcf9
commit
9c603d932d
|
@ -5,10 +5,26 @@ For more details about this platform, please refer to the documentation
|
|||
https://home-assistant.io/components/demo/
|
||||
"""
|
||||
import homeassistant.components.alarm_control_panel.manual as manual
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_NIGHT,
|
||||
STATE_ALARM_TRIGGERED, CONF_PENDING_TIME)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the Demo alarm control panel platform."""
|
||||
add_devices([
|
||||
manual.ManualAlarm(hass, 'Alarm', '1234', 5, 10, False),
|
||||
manual.ManualAlarm(hass, 'Alarm', '1234', 5, 10, False, {
|
||||
STATE_ALARM_ARMED_AWAY: {
|
||||
CONF_PENDING_TIME: 5
|
||||
},
|
||||
STATE_ALARM_ARMED_HOME: {
|
||||
CONF_PENDING_TIME: 5
|
||||
},
|
||||
STATE_ALARM_ARMED_NIGHT: {
|
||||
CONF_PENDING_TIME: 5
|
||||
},
|
||||
STATE_ALARM_TRIGGERED: {
|
||||
CONF_PENDING_TIME: 5
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
|
|
@ -4,6 +4,7 @@ Support for manual alarms.
|
|||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/alarm_control_panel.manual/
|
||||
"""
|
||||
import copy
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
|
@ -24,9 +25,28 @@ DEFAULT_PENDING_TIME = 60
|
|||
DEFAULT_TRIGGER_TIME = 120
|
||||
DEFAULT_DISARM_AFTER_TRIGGER = False
|
||||
|
||||
SUPPORTED_PENDING_STATES = [STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_ARMED_NIGHT, STATE_ALARM_TRIGGERED]
|
||||
|
||||
ATTR_POST_PENDING_STATE = 'post_pending_state'
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
|
||||
def _state_validator(config):
|
||||
config = copy.deepcopy(config)
|
||||
for state in SUPPORTED_PENDING_STATES:
|
||||
if CONF_PENDING_TIME not in config[state]:
|
||||
config[state][CONF_PENDING_TIME] = config[CONF_PENDING_TIME]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
STATE_SETTING_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_PENDING_TIME):
|
||||
vol.All(vol.Coerce(int), vol.Range(min=0))
|
||||
})
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema(vol.All({
|
||||
vol.Required(CONF_PLATFORM): 'manual',
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_ALARM_NAME): cv.string,
|
||||
vol.Optional(CONF_CODE): cv.string,
|
||||
|
@ -36,7 +56,11 @@ PLATFORM_SCHEMA = vol.Schema({
|
|||
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
||||
vol.Optional(CONF_DISARM_AFTER_TRIGGER,
|
||||
default=DEFAULT_DISARM_AFTER_TRIGGER): cv.boolean,
|
||||
})
|
||||
vol.Optional(STATE_ALARM_ARMED_AWAY, default={}): STATE_SETTING_SCHEMA,
|
||||
vol.Optional(STATE_ALARM_ARMED_HOME, default={}): STATE_SETTING_SCHEMA,
|
||||
vol.Optional(STATE_ALARM_ARMED_NIGHT, default={}): STATE_SETTING_SCHEMA,
|
||||
vol.Optional(STATE_ALARM_TRIGGERED, default={}): STATE_SETTING_SCHEMA,
|
||||
}, _state_validator))
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -49,7 +73,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
config.get(CONF_CODE),
|
||||
config.get(CONF_PENDING_TIME, DEFAULT_PENDING_TIME),
|
||||
config.get(CONF_TRIGGER_TIME, DEFAULT_TRIGGER_TIME),
|
||||
config.get(CONF_DISARM_AFTER_TRIGGER, DEFAULT_DISARM_AFTER_TRIGGER)
|
||||
config.get(CONF_DISARM_AFTER_TRIGGER, DEFAULT_DISARM_AFTER_TRIGGER),
|
||||
config
|
||||
)])
|
||||
|
||||
|
||||
|
@ -63,19 +88,23 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
or disarm if `disarm_after_trigger` is true.
|
||||
"""
|
||||
|
||||
def __init__(self, hass, name, code, pending_time,
|
||||
trigger_time, disarm_after_trigger):
|
||||
def __init__(self, hass, name, code, pending_time, trigger_time,
|
||||
disarm_after_trigger, config):
|
||||
"""Init the manual alarm panel."""
|
||||
self._state = STATE_ALARM_DISARMED
|
||||
self._hass = hass
|
||||
self._name = name
|
||||
self._code = str(code) if code else None
|
||||
self._pending_time = datetime.timedelta(seconds=pending_time)
|
||||
self._trigger_time = datetime.timedelta(seconds=trigger_time)
|
||||
self._disarm_after_trigger = disarm_after_trigger
|
||||
self._pre_trigger_state = self._state
|
||||
self._state_ts = None
|
||||
|
||||
self._pending_time_by_state = {}
|
||||
for state in SUPPORTED_PENDING_STATES:
|
||||
self._pending_time_by_state[state] = datetime.timedelta(
|
||||
seconds=config[state][CONF_PENDING_TIME])
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return the plling state."""
|
||||
|
@ -89,17 +118,10 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
if self._state in (STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_ARMED_AWAY,
|
||||
STATE_ALARM_ARMED_NIGHT) and \
|
||||
self._pending_time and self._state_ts + self._pending_time > \
|
||||
dt_util.utcnow():
|
||||
return STATE_ALARM_PENDING
|
||||
|
||||
if self._state == STATE_ALARM_TRIGGERED and self._trigger_time:
|
||||
if self._state_ts + self._pending_time > dt_util.utcnow():
|
||||
if self._within_pending_time(self._state):
|
||||
return STATE_ALARM_PENDING
|
||||
elif (self._state_ts + self._pending_time +
|
||||
elif (self._state_ts + self._pending_time_by_state[self._state] +
|
||||
self._trigger_time) < dt_util.utcnow():
|
||||
if self._disarm_after_trigger:
|
||||
return STATE_ALARM_DISARMED
|
||||
|
@ -107,8 +129,16 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
self._state = self._pre_trigger_state
|
||||
return self._state
|
||||
|
||||
if self._state in SUPPORTED_PENDING_STATES and \
|
||||
self._within_pending_time(self._state):
|
||||
return STATE_ALARM_PENDING
|
||||
|
||||
return self._state
|
||||
|
||||
def _within_pending_time(self, state):
|
||||
pending_time = self._pending_time_by_state[state]
|
||||
return self._state_ts + pending_time > dt_util.utcnow()
|
||||
|
||||
@property
|
||||
def code_format(self):
|
||||
"""One or more characters."""
|
||||
|
@ -128,58 +158,47 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
if not self._validate_code(code, STATE_ALARM_ARMED_HOME):
|
||||
return
|
||||
|
||||
self._state = STATE_ALARM_ARMED_HOME
|
||||
self._state_ts = dt_util.utcnow()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
if self._pending_time:
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + self._pending_time)
|
||||
self._update_state(STATE_ALARM_ARMED_HOME)
|
||||
|
||||
def alarm_arm_away(self, code=None):
|
||||
"""Send arm away command."""
|
||||
if not self._validate_code(code, STATE_ALARM_ARMED_AWAY):
|
||||
return
|
||||
|
||||
self._state = STATE_ALARM_ARMED_AWAY
|
||||
self._state_ts = dt_util.utcnow()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
if self._pending_time:
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + self._pending_time)
|
||||
self._update_state(STATE_ALARM_ARMED_AWAY)
|
||||
|
||||
def alarm_arm_night(self, code=None):
|
||||
"""Send arm night command."""
|
||||
if not self._validate_code(code, STATE_ALARM_ARMED_NIGHT):
|
||||
return
|
||||
|
||||
self._state = STATE_ALARM_ARMED_NIGHT
|
||||
self._state_ts = dt_util.utcnow()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
if self._pending_time:
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + self._pending_time)
|
||||
self._update_state(STATE_ALARM_ARMED_NIGHT)
|
||||
|
||||
def alarm_trigger(self, code=None):
|
||||
"""Send alarm trigger command. No code needed."""
|
||||
self._pre_trigger_state = self._state
|
||||
self._state = STATE_ALARM_TRIGGERED
|
||||
|
||||
self._update_state(STATE_ALARM_TRIGGERED)
|
||||
|
||||
def _update_state(self, state):
|
||||
self._state = state
|
||||
self._state_ts = dt_util.utcnow()
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
if self._trigger_time:
|
||||
pending_time = self._pending_time_by_state[state]
|
||||
|
||||
if state == STATE_ALARM_TRIGGERED and self._trigger_time:
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + self._pending_time)
|
||||
self._state_ts + pending_time)
|
||||
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + self._pending_time + self._trigger_time)
|
||||
self._state_ts + self._trigger_time + pending_time)
|
||||
elif state in SUPPORTED_PENDING_STATES and pending_time:
|
||||
track_point_in_time(
|
||||
self._hass, self.async_update_ha_state,
|
||||
self._state_ts + pending_time)
|
||||
|
||||
def _validate_code(self, code, state):
|
||||
"""Validate given code."""
|
||||
|
|
|
@ -352,6 +352,137 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
self.assertEqual(STATE_ALARM_DISARMED,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_armed_home_with_specific_pending(self):
|
||||
"""Test arm home method."""
|
||||
self.assertTrue(setup_component(
|
||||
self.hass, alarm_control_panel.DOMAIN,
|
||||
{'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 10,
|
||||
'armed_home': {
|
||||
'pending_time': 2
|
||||
}
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
||||
alarm_control_panel.alarm_arm_home(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_PENDING,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||
'dt_util.utcnow'), return_value=future):
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_ARMED_HOME,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_armed_away_with_specific_pending(self):
|
||||
"""Test arm home method."""
|
||||
self.assertTrue(setup_component(
|
||||
self.hass, alarm_control_panel.DOMAIN,
|
||||
{'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 10,
|
||||
'armed_away': {
|
||||
'pending_time': 2
|
||||
}
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
||||
alarm_control_panel.alarm_arm_away(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_PENDING,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||
'dt_util.utcnow'), return_value=future):
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_ARMED_AWAY,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_armed_night_with_specific_pending(self):
|
||||
"""Test arm home method."""
|
||||
self.assertTrue(setup_component(
|
||||
self.hass, alarm_control_panel.DOMAIN,
|
||||
{'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 10,
|
||||
'armed_night': {
|
||||
'pending_time': 2
|
||||
}
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
||||
alarm_control_panel.alarm_arm_night(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_PENDING,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||
'dt_util.utcnow'), return_value=future):
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_ARMED_NIGHT,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_trigger_with_specific_pending(self):
|
||||
"""Test arm home method."""
|
||||
self.assertTrue(setup_component(
|
||||
self.hass, alarm_control_panel.DOMAIN,
|
||||
{'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 10,
|
||||
'triggered': {
|
||||
'pending_time': 2
|
||||
},
|
||||
'trigger_time': 3,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
||||
alarm_control_panel.alarm_trigger(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_PENDING,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||
'dt_util.utcnow'), return_value=future):
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_TRIGGERED,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||
'dt_util.utcnow'), return_value=future):
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_DISARMED,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_trigger_with_disarm_after_trigger(self):
|
||||
"""Test disarm after trigger."""
|
||||
self.assertTrue(setup_component(
|
||||
|
|
Loading…
Reference in New Issue