core/homeassistant/components/alarm_control_panel/verisure.py

96 lines
3.0 KiB
Python
Raw Normal View History

2015-09-13 05:42:38 +00:00
"""
2015-09-13 18:21:02 +00:00
Interfaces with Verisure alarm control panel.
2015-11-09 12:12:18 +00:00
For more details about this platform, please refer to the documentation at
2016-03-07 15:45:21 +00:00
https://home-assistant.io/components/alarm_control_panel.verisure/
2015-09-13 05:42:38 +00:00
"""
import logging
from time import sleep
2015-09-13 05:42:38 +00:00
2015-09-13 18:21:02 +00:00
import homeassistant.components.alarm_control_panel as alarm
from homeassistant.components.verisure import CONF_ALARM, CONF_CODE_DIGITS
2016-02-27 20:50:19 +00:00
from homeassistant.components.verisure import HUB as hub
2015-09-13 18:21:02 +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_UNKNOWN)
2015-09-13 05:42:38 +00:00
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Verisure platform."""
2015-09-13 05:42:38 +00:00
alarms = []
if int(hub.config.get(CONF_ALARM, 1)):
hub.update_overview()
alarms.append(VerisureAlarm())
2015-09-13 05:42:38 +00:00
add_devices(alarms)
def set_arm_state(state, code=None):
"""Send set arm state command."""
transaction_id = hub.session.set_arm_state(code, state)[
'armStateChangeTransactionId']
_LOGGER.info('verisure set arm state %s', state)
transaction = {}
while 'result' not in transaction:
sleep(0.5)
transaction = hub.session.get_arm_state_transaction(transaction_id)
# pylint: disable=unexpected-keyword-arg
hub.update_overview(no_throttle=True)
2015-09-13 18:21:02 +00:00
class VerisureAlarm(alarm.AlarmControlPanel):
"""Representation of a Verisure alarm status."""
2016-03-07 19:21:43 +00:00
def __init__(self):
"""Initialize the Verisure alarm panel."""
2015-09-13 05:42:38 +00:00
self._state = STATE_UNKNOWN
self._digits = hub.config.get(CONF_CODE_DIGITS)
self._changed_by = None
2015-09-13 05:42:38 +00:00
@property
def name(self):
2016-03-07 19:21:43 +00:00
"""Return the name of the device."""
return '{} alarm'.format(hub.session.installations[0]['alias'])
2015-09-13 05:42:38 +00:00
@property
def state(self):
2016-03-07 19:21:43 +00:00
"""Return the state of the device."""
2015-09-14 15:33:43 +00:00
return self._state
@property
def code_format(self):
"""Return one or more digits/characters."""
return 'Number'
@property
def changed_by(self):
"""Return the last change triggered by."""
return self._changed_by
2015-09-14 15:33:43 +00:00
def update(self):
2016-03-07 15:45:21 +00:00
"""Update alarm status."""
hub.update_overview()
status = hub.get_first("$.armState.statusType")
if status == 'DISARMED':
2015-09-13 05:42:38 +00:00
self._state = STATE_ALARM_DISARMED
elif status == 'ARMED_HOME':
2015-09-13 05:42:38 +00:00
self._state = STATE_ALARM_ARMED_HOME
elif status == 'ARMED_AWAY':
2015-09-13 05:42:38 +00:00
self._state = STATE_ALARM_ARMED_AWAY
elif status != 'PENDING':
_LOGGER.error('Unknown alarm state %s', status)
self._changed_by = hub.get_first("$.armState.name")
2015-09-13 18:21:02 +00:00
def alarm_disarm(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send disarm command."""
set_arm_state('DISARMED', code)
2015-09-13 18:21:02 +00:00
def alarm_arm_home(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm home command."""
set_arm_state('ARMED_HOME', code)
2015-09-13 18:21:02 +00:00
def alarm_arm_away(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm away command."""
set_arm_state('ARMED_AWAY', code)