core/homeassistant/components/alarm_control_panel/manual.py

171 lines
5.8 KiB
Python
Raw Normal View History

2015-09-28 23:36:46 +00:00
"""
2015-10-13 18:01:23 +00:00
Support for manual alarms.
2015-09-28 23:36:46 +00:00
2015-10-13 18:01:23 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/alarm_control_panel.manual/
2015-09-28 23:36:46 +00:00
"""
import datetime
2016-02-19 05:27:50 +00:00
import logging
import voluptuous as vol
2015-09-28 23:36:46 +00:00
import homeassistant.components.alarm_control_panel as alarm
import homeassistant.util.dt as dt_util
2015-09-28 23:36:46 +00:00
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED,
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
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.event import track_point_in_time
2015-09-28 23:36:46 +00:00
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=0)),
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__)
2015-09-28 23:36:46 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 19:21:43 +00:00
"""Setup the manual alarm platform."""
2015-09-28 23:36:46 +00:00
add_devices([ManualAlarm(
hass,
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)
2015-09-28 23:36:46 +00:00
)])
class ManualAlarm(alarm.AlarmControlPanel):
2015-10-15 06:38:42 +00:00
"""
Represents an alarm status.
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
or disarm if `disarm_after_trigger` is true.
2015-10-15 06:38:42 +00:00
"""
2016-03-07 19:21:43 +00:00
def __init__(self, hass, name, code, pending_time,
trigger_time, disarm_after_trigger):
2016-03-07 19:21:43 +00:00
"""Initalize the manual alarm panel."""
2015-09-28 23:36:46 +00:00
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
2015-09-28 23:36:46 +00:00
@property
def should_poll(self):
2016-03-07 15:45:21 +00:00
"""No polling needed."""
2015-09-28 23:36:46 +00:00
return False
@property
def name(self):
2016-03-07 19:21:43 +00:00
"""Return the name of the device."""
2015-09-28 23:36:46 +00:00
return self._name
@property
def state(self):
2016-03-07 19:21:43 +00:00
"""Return the state of the device."""
2016-01-13 05:53:27 +00:00
if self._state in (STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_AWAY) and \
self._pending_time and self._state_ts + self._pending_time > \
dt_util.utcnow():
return STATE_ALARM_PENDING
2015-10-15 06:38:42 +00:00
if self._state == STATE_ALARM_TRIGGERED and self._trigger_time:
2015-10-15 15:26:56 +00:00
if self._state_ts + self._pending_time > dt_util.utcnow():
2015-10-15 06:38:42 +00:00
return STATE_ALARM_PENDING
2015-10-15 15:26:56 +00:00
elif (self._state_ts + self._pending_time +
self._trigger_time) < dt_util.utcnow():
if self._disarm_after_trigger:
return STATE_ALARM_DISARMED
else:
return self._pre_trigger_state
2015-09-28 23:36:46 +00:00
return self._state
@property
def code_format(self):
2016-03-07 15:45:21 +00:00
"""One or more characters."""
2015-09-28 23:36:46 +00:00
return None if self._code is None else '.+'
def alarm_disarm(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send disarm command."""
if not self._validate_code(code, STATE_ALARM_DISARMED):
return
self._state = STATE_ALARM_DISARMED
self._state_ts = dt_util.utcnow()
self.update_ha_state()
2015-09-28 23:36:46 +00:00
def alarm_arm_home(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm home command."""
if not self._validate_code(code, STATE_ALARM_ARMED_HOME):
return
self._state = STATE_ALARM_ARMED_HOME
self._state_ts = dt_util.utcnow()
self.update_ha_state()
if self._pending_time:
track_point_in_time(
self._hass, self.async_update_ha_state,
self._state_ts + self._pending_time)
2015-09-28 23:36:46 +00:00
def alarm_arm_away(self, code=None):
2016-03-07 15:45:21 +00:00
"""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.update_ha_state()
if self._pending_time:
track_point_in_time(
self._hass, self.async_update_ha_state,
self._state_ts + self._pending_time)
2015-09-28 23:36:46 +00:00
def alarm_trigger(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send alarm trigger command. No code needed."""
self._pre_trigger_state = self._state
self._state = STATE_ALARM_TRIGGERED
self._state_ts = dt_util.utcnow()
self.update_ha_state()
if self._trigger_time:
track_point_in_time(
self._hass, self.async_update_ha_state,
2015-10-15 15:26:56 +00:00
self._state_ts + self._pending_time)
2015-10-15 06:38:42 +00:00
track_point_in_time(
self._hass, self.async_update_ha_state,
2015-10-15 15:26:56 +00:00
self._state_ts + self._pending_time + self._trigger_time)
2015-10-15 06:38:42 +00:00
def _validate_code(self, code, state):
2016-03-07 15:45:21 +00:00
"""Validate given code."""
check = self._code is None or code == self._code
if not check:
_LOGGER.warning('Invalid code given for %s', state)
return check