add disarm_after_trigger to manual alarm panel
parent
1e3f7ad9a4
commit
abaffc2d8c
|
@ -10,5 +10,5 @@ import homeassistant.components.alarm_control_panel.manual as manual
|
|||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Demo alarm control panel platform."""
|
||||
add_devices([
|
||||
manual.ManualAlarm(hass, 'Alarm', '1234', 5, 10),
|
||||
manual.ManualAlarm(hass, 'Alarm', '1234', 5, 10, False),
|
||||
])
|
||||
|
|
|
@ -7,28 +7,46 @@ https://home-assistant.io/components/alarm_control_panel.manual/
|
|||
import datetime
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.components.alarm_control_panel as alarm
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED,
|
||||
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
|
||||
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, CONF_PLATFORM, CONF_NAME,
|
||||
CONF_CODE, CONF_PENDING_TIME, CONF_TRIGGER_TIME, CONF_DISARM_AFTER_TRIGGER)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.event import track_point_in_time
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_ALARM_NAME = 'HA Alarm'
|
||||
DEFAULT_PENDING_TIME = 60
|
||||
DEFAULT_TRIGGER_TIME = 120
|
||||
DEFAULT_DISARM_AFTER_TRIGGER = False
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_PLATFORM): 'manual',
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_ALARM_NAME): cv.string,
|
||||
vol.Optional(CONF_CODE): cv.string,
|
||||
vol.Optional(CONF_PENDING_TIME, default=DEFAULT_PENDING_TIME):
|
||||
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
||||
vol.Optional(CONF_TRIGGER_TIME, default=DEFAULT_TRIGGER_TIME):
|
||||
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
||||
vol.Optional(CONF_DISARM_AFTER_TRIGGER,
|
||||
default=DEFAULT_DISARM_AFTER_TRIGGER): cv.boolean,
|
||||
})
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the manual alarm platform."""
|
||||
add_devices([ManualAlarm(
|
||||
hass,
|
||||
config.get('name', DEFAULT_ALARM_NAME),
|
||||
config.get('code'),
|
||||
config.get('pending_time', DEFAULT_PENDING_TIME),
|
||||
config.get('trigger_time', DEFAULT_TRIGGER_TIME),
|
||||
config[CONF_NAME],
|
||||
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)
|
||||
)])
|
||||
|
||||
|
||||
|
@ -40,10 +58,12 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
|
||||
When armed, will be pending for 'pending_time', after that armed.
|
||||
When triggered, will be pending for 'trigger_time'. After that will be
|
||||
triggered for 'trigger_time', after that we return to the previous state.
|
||||
triggered for 'trigger_time', after that we return to the previous state
|
||||
or disarm if `disarm_after_trigger` is true.
|
||||
"""
|
||||
|
||||
def __init__(self, hass, name, code, pending_time, trigger_time):
|
||||
def __init__(self, hass, name, code, pending_time,
|
||||
trigger_time, disarm_after_trigger):
|
||||
"""Initalize the manual alarm panel."""
|
||||
self._state = STATE_ALARM_DISARMED
|
||||
self._hass = hass
|
||||
|
@ -51,6 +71,7 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
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
|
||||
|
||||
|
@ -78,7 +99,10 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
|||
return STATE_ALARM_PENDING
|
||||
elif (self._state_ts + self._pending_time +
|
||||
self._trigger_time) < dt_util.utcnow():
|
||||
return self._pre_trigger_state
|
||||
if self._disarm_after_trigger:
|
||||
return STATE_ALARM_DISARMED
|
||||
else:
|
||||
return self._pre_trigger_state
|
||||
|
||||
return self._state
|
||||
|
||||
|
|
|
@ -26,7 +26,9 @@ CONF_API_KEY = 'api_key'
|
|||
CONF_BEFORE = 'before'
|
||||
CONF_BELOW = 'below'
|
||||
CONF_CONDITION = 'condition'
|
||||
CONF_CODE = 'code'
|
||||
CONF_CUSTOMIZE = 'customize'
|
||||
CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger'
|
||||
CONF_ELEVATION = 'elevation'
|
||||
CONF_ENTITY_ID = 'entity_id'
|
||||
CONF_ENTITY_NAMESPACE = 'entity_namespace'
|
||||
|
@ -42,13 +44,15 @@ CONF_NAME = 'name'
|
|||
CONF_OFFSET = 'offset'
|
||||
CONF_OPTIMISTIC = 'optimistic'
|
||||
CONF_PASSWORD = 'password'
|
||||
CONF_PENDING_TIME = 'pending_time'
|
||||
CONF_PLATFORM = 'platform'
|
||||
CONF_PORT = 'port'
|
||||
CONF_SCAN_INTERVAL = 'scan_interval'
|
||||
CONF_STATE = 'state'
|
||||
CONF_TEMPERATURE_UNIT = 'temperature_unit'
|
||||
CONF_UNIT_SYSTEM = 'unit_system'
|
||||
CONF_TIME_ZONE = 'time_zone'
|
||||
CONF_TRIGGER_TIME = 'trigger_time'
|
||||
CONF_UNIT_SYSTEM = 'unit_system'
|
||||
CONF_USERNAME = 'username'
|
||||
CONF_VALUE_TEMPLATE = 'value_template'
|
||||
CONF_WEEKDAY = 'weekday'
|
||||
|
|
|
@ -32,7 +32,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 0
|
||||
'pending_time': 0,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -53,7 +54,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 1
|
||||
'pending_time': 1,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -83,7 +85,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 1
|
||||
'pending_time': 1,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -104,7 +107,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 0
|
||||
'pending_time': 0,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -125,7 +129,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 1
|
||||
'pending_time': 1,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -155,7 +160,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'code': CODE,
|
||||
'pending_time': 1
|
||||
'pending_time': 1,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -175,7 +181,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'trigger_time': 0
|
||||
'trigger_time': 0,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -196,7 +203,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 2,
|
||||
'trigger_time': 3
|
||||
'trigger_time': 3,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -228,13 +236,45 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
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(alarm_control_panel.setup(self.hass, {
|
||||
'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'trigger_time': 5,
|
||||
'pending_time': 0,
|
||||
'disarm_after_trigger': True
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
||||
self.assertEqual(STATE_ALARM_DISARMED,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id)
|
||||
self.hass.pool.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.pool.block_till_done()
|
||||
|
||||
self.assertEqual(STATE_ALARM_DISARMED,
|
||||
self.hass.states.get(entity_id).state)
|
||||
|
||||
def test_disarm_while_pending_trigger(self):
|
||||
"""Test disarming while pending state."""
|
||||
self.assertTrue(alarm_control_panel.setup(self.hass, {
|
||||
'alarm_control_panel': {
|
||||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'trigger_time': 5
|
||||
'trigger_time': 5,
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
@ -270,7 +310,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
|||
'platform': 'manual',
|
||||
'name': 'test',
|
||||
'pending_time': 5,
|
||||
'code': CODE + '2'
|
||||
'code': CODE + '2',
|
||||
'disarm_after_trigger': False
|
||||
}}))
|
||||
|
||||
entity_id = 'alarm_control_panel.test'
|
||||
|
|
Loading…
Reference in New Issue