2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Verisure alarm control panels."""
|
2015-09-13 05:42:38 +00:00
|
|
|
import logging
|
2017-06-26 20:30:25 +00:00
|
|
|
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.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
|
|
|
STATE_ALARM_DISARMED,
|
|
|
|
)
|
2015-09-13 05:42:38 +00:00
|
|
|
|
2019-05-23 17:27:42 +00:00
|
|
|
from . import CONF_ALARM, CONF_CODE_DIGITS, CONF_GIID, HUB as hub
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2015-09-13 05:42:38 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Verisure platform."""
|
2015-09-13 05:42:38 +00:00
|
|
|
alarms = []
|
2016-09-07 01:18:34 +00:00
|
|
|
if int(hub.config.get(CONF_ALARM, 1)):
|
2017-06-26 20:30:25 +00:00
|
|
|
hub.update_overview()
|
|
|
|
alarms.append(VerisureAlarm())
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(alarms)
|
2015-09-13 05:42:38 +00:00
|
|
|
|
|
|
|
|
2017-06-26 20:30:25 +00:00
|
|
|
def set_arm_state(state, code=None):
|
|
|
|
"""Send set arm state command."""
|
|
|
|
transaction_id = hub.session.set_arm_state(code, state)[
|
2019-07-31 19:25:30 +00:00
|
|
|
"armStateChangeTransactionId"
|
|
|
|
]
|
|
|
|
_LOGGER.info("verisure set arm state %s", state)
|
2017-06-26 20:30:25 +00:00
|
|
|
transaction = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
while "result" not in transaction:
|
2017-06-26 20:30:25 +00:00
|
|
|
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):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a Verisure alarm status."""
|
2016-03-07 19:21:43 +00:00
|
|
|
|
2017-06-26 20:30:25 +00:00
|
|
|
def __init__(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the Verisure alarm panel."""
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2016-09-07 01:18:34 +00:00
|
|
|
self._digits = hub.config.get(CONF_CODE_DIGITS)
|
2016-08-08 16:00:20 +00:00
|
|
|
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."""
|
2019-05-23 17:27:42 +00:00
|
|
|
giid = hub.config.get(CONF_GIID)
|
|
|
|
if giid is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
aliass = {i["giid"]: i["alias"] for i in hub.session.installations}
|
2019-05-23 17:27:42 +00:00
|
|
|
if giid in aliass.keys():
|
2019-07-31 19:25:30 +00:00
|
|
|
return "{} alarm".format(aliass[giid])
|
2019-05-23 17:27:42 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Verisure installation giid not found: %s", giid)
|
2019-05-23 17:27:42 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
|
|
|
|
2015-09-19 22:22:37 +00:00
|
|
|
@property
|
|
|
|
def code_format(self):
|
2018-05-29 05:50:27 +00:00
|
|
|
"""Return one or more digits/characters."""
|
2019-01-13 10:59:12 +00:00
|
|
|
return alarm.FORMAT_NUMBER
|
2015-09-19 22:22:37 +00:00
|
|
|
|
2016-08-08 16:00:20 +00:00
|
|
|
@property
|
|
|
|
def changed_by(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the last change triggered by."""
|
2016-08-08 16:00:20 +00:00
|
|
|
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."""
|
2017-06-26 20:30:25 +00:00
|
|
|
hub.update_overview()
|
|
|
|
status = hub.get_first("$.armState.statusType")
|
2019-07-31 19:25:30 +00:00
|
|
|
if status == "DISARMED":
|
2015-09-13 05:42:38 +00:00
|
|
|
self._state = STATE_ALARM_DISARMED
|
2019-07-31 19:25:30 +00:00
|
|
|
elif status == "ARMED_HOME":
|
2015-09-13 05:42:38 +00:00
|
|
|
self._state = STATE_ALARM_ARMED_HOME
|
2019-07-31 19:25:30 +00:00
|
|
|
elif status == "ARMED_AWAY":
|
2015-09-13 05:42:38 +00:00
|
|
|
self._state = STATE_ALARM_ARMED_AWAY
|
2019-07-31 19:25:30 +00:00
|
|
|
elif status != "PENDING":
|
|
|
|
_LOGGER.error("Unknown alarm state %s", status)
|
2017-06-26 20:30:25 +00:00
|
|
|
self._changed_by = hub.get_first("$.armState.name")
|
2015-09-13 18:21:02 +00:00
|
|
|
|
2015-09-19 17:32:37 +00:00
|
|
|
def alarm_disarm(self, code=None):
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send disarm command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
set_arm_state("DISARMED", code)
|
2015-09-13 18:21:02 +00:00
|
|
|
|
2015-09-19 17:32:37 +00:00
|
|
|
def alarm_arm_home(self, code=None):
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm home command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
set_arm_state("ARMED_HOME", code)
|
2015-09-13 18:21:02 +00:00
|
|
|
|
2015-09-19 17:32:37 +00:00
|
|
|
def alarm_arm_away(self, code=None):
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm away command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
set_arm_state("ARMED_AWAY", code)
|