core/homeassistant/components/alarm_control_panel/manual.py

142 lines
5.2 KiB
Python
Raw Normal View History

2015-09-28 23:36:46 +00:00
"""
homeassistant.components.alarm_control_panel.manual
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
https://home-assistant.io/components/alarm_control_panel.manual.html
2015-09-28 23:36:46 +00:00
"""
import logging
import datetime
2015-09-28 23:36:46 +00:00
import homeassistant.components.alarm_control_panel as alarm
from homeassistant.helpers.event import track_point_in_time
import homeassistant.util.dt as dt_util
2015-09-28 23:36:46 +00:00
from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = []
DEFAULT_ALARM_NAME = 'HA Alarm'
DEFAULT_PENDING_TIME = 60
DEFAULT_TRIGGER_TIME = 120
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up 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),
)])
# pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=abstract-method
2015-09-28 23:36:46 +00:00
class ManualAlarm(alarm.AlarmControlPanel):
2015-10-13 18:01:23 +00:00
""" Represents an alarm status. """
2015-09-28 23:36:46 +00:00
def __init__(self, hass, name, code, pending_time, trigger_time):
self._state = STATE_ALARM_DISARMED
self._hass = hass
self._name = name
self._code = code
self._pending_time = datetime.timedelta(seconds=pending_time)
self._trigger_time = datetime.timedelta(seconds=trigger_time)
self._state_ts = None
2015-09-28 23:36:46 +00:00
self._pending_to = None
@property
def should_poll(self):
2015-10-13 18:01:23 +00:00
""" No polling needed. """
2015-09-28 23:36:46 +00:00
return False
@property
def name(self):
""" Returns the name of the device. """
return self._name
@property
def state(self):
""" Returns the state of the device. """
return self._state
@property
def code_format(self):
2015-10-13 18:01:23 +00:00
""" One or more characters. """
2015-09-28 23:36:46 +00:00
return None if self._code is None else '.+'
def update_state(self, state, pending_to):
2015-10-13 18:01:23 +00:00
""" Changes between state with delay. """
self._state = state
self._state_ts = dt_util.utcnow()
self._pending_to = pending_to
2015-09-28 23:36:46 +00:00
self.update_ha_state()
def alarm_disarm(self, code=None):
""" Send disarm command. """
if code == str(self._code) or self.code_format is None:
self.update_state(STATE_ALARM_DISARMED, None)
2015-09-28 23:36:46 +00:00
else:
_LOGGER.warning("Wrong code entered while disarming!")
def alarm_arm_home(self, code=None):
""" Send arm home command. """
if code == str(self._code) or self.code_format is None:
self.update_state(STATE_ALARM_PENDING, STATE_ALARM_ARMED_HOME)
def delayed_alarm_arm_home(event_time):
2015-10-13 18:01:23 +00:00
""" Callback for delayed action. """
if self._pending_to == STATE_ALARM_ARMED_HOME and \
dt_util.utcnow() - self._state_ts >= self._pending_time:
self.update_state(STATE_ALARM_ARMED_HOME, None)
track_point_in_time(
self._hass, delayed_alarm_arm_home,
dt_util.utcnow() + self._pending_time)
2015-09-28 23:36:46 +00:00
else:
_LOGGER.warning("Wrong code entered while arming home!")
def alarm_arm_away(self, code=None):
""" Send arm away command. """
if code == str(self._code) or self.code_format is None:
self.update_state(STATE_ALARM_PENDING, STATE_ALARM_ARMED_AWAY)
def delayed_alarm_arm_away(event_time):
2015-10-13 18:01:23 +00:00
""" Callback for delayed action. """
if self._pending_to == STATE_ALARM_ARMED_AWAY and \
dt_util.utcnow() - self._state_ts >= self._pending_time:
self.update_state(STATE_ALARM_ARMED_AWAY, None)
track_point_in_time(
self._hass, delayed_alarm_arm_away,
dt_util.utcnow() + self._pending_time)
2015-09-28 23:36:46 +00:00
else:
_LOGGER.warning("Wrong code entered while arming away!")
def alarm_trigger(self, code=None):
""" Send alarm trigger command. No code needed. """
self.update_state(STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
def delayed_alarm_trigger(event_time):
""" callback for delayed action """
if self._pending_to == STATE_ALARM_TRIGGERED and \
dt_util.utcnow() - self._state_ts >= self._pending_time:
self.update_state(STATE_ALARM_TRIGGERED, STATE_ALARM_DISARMED)
def delayed_alarm_disarm(event_time):
2015-10-13 18:01:23 +00:00
""" Callback for delayed action. """
if self._pending_to == STATE_ALARM_DISARMED and \
dt_util.utcnow() - self._state_ts >= self._trigger_time:
self.update_state(STATE_ALARM_DISARMED, None)
track_point_in_time(
self._hass, delayed_alarm_disarm,
dt_util.utcnow() + self._trigger_time)
track_point_in_time(
self._hass, delayed_alarm_trigger,
dt_util.utcnow() + self._pending_time)