replace sleeps with track_point_in_time

This commit is contained in:
sfam 2015-10-13 00:56:24 +00:00
parent e57b3ae847
commit 1b7ce2146c
4 changed files with 69 additions and 46 deletions

View File

@ -73,40 +73,44 @@ def setup(hass, config):
return True return True
def alarm_disarm(hass, code, entity_id=None): def alarm_disarm(hass, code=None, entity_id=None):
""" Send the alarm the command for disarm. """ """ Send the alarm the command for disarm. """
data = {ATTR_CODE: code} data = {}
if code:
data[ATTR_CODE] = code
if entity_id: if entity_id:
data[ATTR_ENTITY_ID] = entity_id data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_DISARM, data) hass.services.call(DOMAIN, SERVICE_ALARM_DISARM, data)
def alarm_arm_home(hass, code, entity_id=None): def alarm_arm_home(hass, code=None, entity_id=None):
""" Send the alarm the command for arm home. """ """ Send the alarm the command for arm home. """
data = {ATTR_CODE: code} data = {}
if code:
data[ATTR_CODE] = code
if entity_id: if entity_id:
data[ATTR_ENTITY_ID] = entity_id data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_ARM_HOME, data) hass.services.call(DOMAIN, SERVICE_ALARM_ARM_HOME, data)
def alarm_arm_away(hass, code, entity_id=None): def alarm_arm_away(hass, code=None, entity_id=None):
""" Send the alarm the command for arm away. """ """ Send the alarm the command for arm away. """
data = {ATTR_CODE: code} data = {}
if code:
data[ATTR_CODE] = code
if entity_id: if entity_id:
data[ATTR_ENTITY_ID] = entity_id data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_ARM_AWAY, data) hass.services.call(DOMAIN, SERVICE_ALARM_ARM_AWAY, data)
def alarm_trigger(hass, code, entity_id=None): def alarm_trigger(hass, code=None, entity_id=None):
""" Send the alarm the command for trigger. """ """ Send the alarm the command for trigger. """
data = {ATTR_CODE: code} data = {}
if code:
data[ATTR_CODE] = code
if entity_id: if entity_id:
data[ATTR_ENTITY_ID] = entity_id data[ATTR_ENTITY_ID] = entity_id

View File

@ -33,8 +33,10 @@ Default is 120 seconds.
""" """
import logging import logging
import time import datetime
import homeassistant.components.alarm_control_panel as alarm import homeassistant.components.alarm_control_panel as alarm
from homeassistant.helpers.event import track_point_in_time
import homeassistant.util.dt as dt_util
from homeassistant.const import ( from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY, STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
@ -62,6 +64,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
# pylint: disable=too-many-arguments, too-many-instance-attributes # pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=abstract-method
class ManualAlarm(alarm.AlarmControlPanel): class ManualAlarm(alarm.AlarmControlPanel):
""" represents an alarm status within home assistant. """ """ represents an alarm status within home assistant. """
@ -70,8 +73,9 @@ class ManualAlarm(alarm.AlarmControlPanel):
self._hass = hass self._hass = hass
self._name = name self._name = name
self._code = code self._code = code
self._pending_time = pending_time self._pending_time = datetime.timedelta(seconds=pending_time)
self._trigger_time = trigger_time self._trigger_time = datetime.timedelta(seconds=trigger_time)
self._state_ts = None
self._pending_to = None self._pending_to = None
@property @property
@ -94,49 +98,70 @@ class ManualAlarm(alarm.AlarmControlPanel):
""" One or more characters """ """ One or more characters """
return None if self._code is None else '.+' return None if self._code is None else '.+'
def change_alarm_state(self, begin, end, delay=0): def update_state(self, state, pending_to):
""" changes between state with delay """ """ changes between state with delay """
self._state = begin self._state = state
self._pending_to = end self._state_ts = dt_util.utcnow()
self._pending_to = pending_to
self.update_ha_state() self.update_ha_state()
time.sleep(delay)
if self._pending_to == end and begin != end:
self._state = end
self._pending_to = None
self.update_ha_state()
def alarm_disarm(self, code=None): def alarm_disarm(self, code=None):
""" Send disarm command. """ """ Send disarm command. """
if code == str(self._code) or self.code_format is None: if code == str(self._code) or self.code_format is None:
self.change_alarm_state( self.update_state(STATE_ALARM_DISARMED, None)
STATE_ALARM_DISARMED, STATE_ALARM_DISARMED)
else: else:
_LOGGER.warning("Wrong code entered while disarming!") _LOGGER.warning("Wrong code entered while disarming!")
def alarm_arm_home(self, code=None): def alarm_arm_home(self, code=None):
""" Send arm home command. """ """ Send arm home command. """
if code == str(self._code) or self.code_format is None: if code == str(self._code) or self.code_format is None:
self.change_alarm_state( self.update_state(STATE_ALARM_PENDING, STATE_ALARM_ARMED_HOME)
STATE_ALARM_PENDING, STATE_ALARM_ARMED_HOME,
self._pending_time) def delayed_alarm_arm_home(event_time):
""" 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)
else: else:
_LOGGER.warning("Wrong code entered while arming home!") _LOGGER.warning("Wrong code entered while arming home!")
def alarm_arm_away(self, code=None): def alarm_arm_away(self, code=None):
""" Send arm away command. """ """ Send arm away command. """
if code == str(self._code) or self.code_format is None: if code == str(self._code) or self.code_format is None:
self.change_alarm_state( self.update_state(STATE_ALARM_PENDING, STATE_ALARM_ARMED_AWAY)
STATE_ALARM_PENDING, STATE_ALARM_ARMED_AWAY,
self._pending_time) def delayed_alarm_arm_away(event_time):
""" 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)
else: else:
_LOGGER.warning("Wrong code entered while arming away!") _LOGGER.warning("Wrong code entered while arming away!")
def alarm_trigger(self, code=None): def alarm_trigger(self, code=None):
""" Send alarm trigger command. No code needed. """ """ Send alarm trigger command. No code needed. """
self.change_alarm_state( self.update_state(STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED,
self._pending_time) def delayed_alarm_trigger(event_time):
if self._state == STATE_ALARM_TRIGGERED: """ callback for delayed action """
self.change_alarm_state( if self._pending_to == STATE_ALARM_TRIGGERED and \
STATE_ALARM_TRIGGERED, STATE_ALARM_DISARMED, dt_util.utcnow() - self._state_ts >= self._pending_time:
self._trigger_time) self.update_state(STATE_ALARM_TRIGGERED, STATE_ALARM_DISARMED)
def delayed_alarm_disarm(event_time):
""" 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)

View File

@ -100,6 +100,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
# pylint: disable=too-many-arguments, too-many-instance-attributes # pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=abstract-method
class MqttAlarm(alarm.AlarmControlPanel): class MqttAlarm(alarm.AlarmControlPanel):
""" represents a MQTT alarm status within home assistant. """ """ represents a MQTT alarm status within home assistant. """
@ -166,7 +167,3 @@ class MqttAlarm(alarm.AlarmControlPanel):
self._payload_arm_away, self._qos) self._payload_arm_away, self._qos)
else: else:
_LOGGER.warning("Wrong code entered while arming away!") _LOGGER.warning("Wrong code entered while arming away!")
def alarm_trigger(self, code=None):
""" Send alarm trigger command. No code needed. """
return

View File

@ -33,6 +33,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices(alarms) add_devices(alarms)
# pylint: disable=abstract-method
class VerisureAlarm(alarm.AlarmControlPanel): class VerisureAlarm(alarm.AlarmControlPanel):
""" represents a Verisure alarm status within home assistant. """ """ represents a Verisure alarm status within home assistant. """
@ -91,7 +92,3 @@ class VerisureAlarm(alarm.AlarmControlPanel):
code, code,
verisure.MY_PAGES.ALARM_ARMED_AWAY) verisure.MY_PAGES.ALARM_ARMED_AWAY)
_LOGGER.warning('arming away') _LOGGER.warning('arming away')
def alarm_trigger(self, code=None):
""" Send alarm trigger command. No code needed. """
return