Add last event data (including "changed_by") to SimpliSafe (#25569)

* Add "last event" sensor for SimpliSafe

* Functionality round 1

* Cleanup

* Whitespace

* Whitespace

* Updated requirements

* Removed unused constants

* Member comments
This commit is contained in:
Aaron Bach 2019-07-30 17:23:42 -06:00 committed by GitHub
parent 0257fe0375
commit fe1e761a7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 38 deletions

View file

@ -1,13 +1,19 @@
"""Support for SimpliSafe alarm control panels."""
import logging
import re
import homeassistant.components.alarm_control_panel as alarm
from simplipy.sensor import SensorTypes
from simplipy.system import SystemStates
from homeassistant.components.alarm_control_panel import (
FORMAT_NUMBER, FORMAT_TEXT, AlarmControlPanel)
from homeassistant.const import (
CONF_CODE, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME,
STATE_ALARM_DISARMED)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.dt import utc_from_timestamp
from .const import DATA_CLIENT, DOMAIN, TOPIC_UPDATE
@ -16,6 +22,11 @@ _LOGGER = logging.getLogger(__name__)
ATTR_ALARM_ACTIVE = 'alarm_active'
ATTR_BATTERY_BACKUP_POWER_LEVEL = 'battery_backup_power_level'
ATTR_GSM_STRENGTH = 'gsm_strength'
ATTR_LAST_EVENT_INFO = 'last_event_info'
ATTR_LAST_EVENT_SENSOR_NAME = 'last_event_sensor_name'
ATTR_LAST_EVENT_SENSOR_TYPE = 'last_event_sensor_type'
ATTR_LAST_EVENT_TIMESTAMP = 'last_event_timestamp'
ATTR_LAST_EVENT_TYPE = 'last_event_type'
ATTR_RF_JAMMING = 'rf_jamming'
ATTR_SYSTEM_ID = 'system_id'
ATTR_WALL_POWER_LEVEL = 'wall_power_level'
@ -32,21 +43,23 @@ async def async_setup_entry(hass, entry, async_add_entities):
"""Set up a SimpliSafe alarm control panel based on a config entry."""
simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
async_add_entities([
SimpliSafeAlarm(system, entry.data.get(CONF_CODE))
SimpliSafeAlarm(simplisafe, system, entry.data.get(CONF_CODE))
for system in simplisafe.systems.values()
], True)
class SimpliSafeAlarm(alarm.AlarmControlPanel):
class SimpliSafeAlarm(AlarmControlPanel):
"""Representation of a SimpliSafe alarm."""
def __init__(self, system, code):
def __init__(self, simplisafe, system, code):
"""Initialize the SimpliSafe alarm."""
self._async_unsub_dispatcher_connect = None
self._attrs = {ATTR_SYSTEM_ID: system.system_id}
self._changed_by = None
self._code = code
self._system = system
self._simplisafe = simplisafe
self._state = None
self._system = system
# Some properties only exist for V2 or V3 systems:
for prop in (
@ -55,14 +68,19 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
if hasattr(system, prop):
self._attrs[prop] = getattr(system, prop)
@property
def changed_by(self):
"""Return info about who changed the alarm last."""
return self._changed_by
@property
def code_format(self):
"""Return one or more digits/characters."""
if not self._code:
return None
if isinstance(self._code, str) and re.search('^\\d+$', self._code):
return alarm.FORMAT_NUMBER
return alarm.FORMAT_TEXT
return FORMAT_NUMBER
return FORMAT_TEXT
@property
def device_info(self):
@ -86,17 +104,17 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
@property
def name(self):
"""Return the name of the device."""
"""Return the name of the entity."""
return self._system.address
@property
def state(self):
"""Return the state of the device."""
"""Return the state of the entity."""
return self._state
@property
def unique_id(self):
"""Return the unique ID."""
"""Return the unique ID of the entity."""
return self._system.system_id
def _validate_code(self, code, state):
@ -116,11 +134,6 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, TOPIC_UPDATE, update)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
async def async_alarm_disarm(self, code=None):
"""Send disarm command."""
if not self._validate_code(code, 'disarming'):
@ -144,9 +157,10 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
async def async_update(self):
"""Update alarm status."""
from simplipy.system import SystemStates
event_data = self._simplisafe.last_event_data[self._system.system_id]
self._attrs[ATTR_ALARM_ACTIVE] = self._system.alarm_going_off
if event_data['pinName']:
self._changed_by = event_data['pinName']
if self._system.state == SystemStates.error:
return
@ -161,3 +175,20 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
self._state = STATE_ALARM_ARMED_AWAY
else:
self._state = None
last_event = self._simplisafe.last_event_data[self._system.system_id]
self._attrs.update({
ATTR_ALARM_ACTIVE: self._system.alarm_going_off,
ATTR_LAST_EVENT_INFO: last_event['info'],
ATTR_LAST_EVENT_SENSOR_NAME: last_event['sensorName'],
ATTR_LAST_EVENT_SENSOR_TYPE: SensorTypes(
last_event['sensorType']).name,
ATTR_LAST_EVENT_TIMESTAMP: utc_from_timestamp(
last_event['eventTimestamp']),
ATTR_LAST_EVENT_TYPE: last_event['eventType'],
})
async def async_will_remove_from_hass(self) -> None:
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()