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

@ -130,6 +130,7 @@ async def async_setup_entry(hass, config_entry):
systems = await api.get_systems() systems = await api.get_systems()
simplisafe = SimpliSafe(hass, config_entry, systems) simplisafe = SimpliSafe(hass, config_entry, systems)
await simplisafe.async_update()
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = simplisafe hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = simplisafe
hass.async_create_task( hass.async_create_task(
@ -139,6 +140,8 @@ async def async_setup_entry(hass, config_entry):
async def refresh(event_time): async def refresh(event_time):
"""Refresh data from the SimpliSafe account.""" """Refresh data from the SimpliSafe account."""
await simplisafe.async_update() await simplisafe.async_update()
_LOGGER.debug('Updated data for all SimpliSafe systems')
async_dispatcher_send(hass, TOPIC_UPDATE)
hass.data[DOMAIN][DATA_LISTENER][ hass.data[DOMAIN][DATA_LISTENER][
config_entry.entry_id] = async_track_time_interval( config_entry.entry_id] = async_track_time_interval(
@ -193,25 +196,34 @@ class SimpliSafe:
"""Initialize.""" """Initialize."""
self._config_entry = config_entry self._config_entry = config_entry
self._hass = hass self._hass = hass
self.last_event_data = {}
self.systems = systems self.systems = systems
async def _update_system(self, system):
"""Update a system."""
try:
await system.update()
latest_event = await system.get_latest_event()
except SimplipyError as err:
_LOGGER.error(
'SimpliSafe error while updating "%s": %s',
system.address, err)
return
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(
'Unknown error while updating "%s": %s', system.address, err)
return
self.last_event_data[system.system_id] = latest_event
if system.api.refresh_token_dirty:
_async_save_refresh_token(
self._hass, self._config_entry, system.api.refresh_token)
async def async_update(self): async def async_update(self):
"""Get updated data from SimpliSafe.""" """Get updated data from SimpliSafe."""
systems = self.systems.values() tasks = [
tasks = [system.update() for system in systems] self._update_system(system) for system in self.systems.values()
]
results = await asyncio.gather(*tasks, return_exceptions=True) await asyncio.gather(*tasks)
for system, result in zip(systems, results):
if isinstance(result, Exception):
_LOGGER.error(
'There was error updating "%s": %s', system.address,
result)
continue
if system.api.refresh_token_dirty:
_async_save_refresh_token(
self._hass, self._config_entry, system.api.refresh_token)
_LOGGER.debug('Updated status of "%s"', system.address)
async_dispatcher_send(
self._hass, TOPIC_UPDATE.format(system.system_id))

View file

@ -1,13 +1,19 @@
"""Support for SimpliSafe alarm control panels.""" """Support for SimpliSafe alarm control panels."""
import logging import logging
import re 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 ( from homeassistant.const import (
CONF_CODE, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, CONF_CODE, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME,
STATE_ALARM_DISARMED) STATE_ALARM_DISARMED)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.dt import utc_from_timestamp
from .const import DATA_CLIENT, DOMAIN, TOPIC_UPDATE from .const import DATA_CLIENT, DOMAIN, TOPIC_UPDATE
@ -16,6 +22,11 @@ _LOGGER = logging.getLogger(__name__)
ATTR_ALARM_ACTIVE = 'alarm_active' ATTR_ALARM_ACTIVE = 'alarm_active'
ATTR_BATTERY_BACKUP_POWER_LEVEL = 'battery_backup_power_level' ATTR_BATTERY_BACKUP_POWER_LEVEL = 'battery_backup_power_level'
ATTR_GSM_STRENGTH = 'gsm_strength' 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_RF_JAMMING = 'rf_jamming'
ATTR_SYSTEM_ID = 'system_id' ATTR_SYSTEM_ID = 'system_id'
ATTR_WALL_POWER_LEVEL = 'wall_power_level' 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.""" """Set up a SimpliSafe alarm control panel based on a config entry."""
simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
async_add_entities([ async_add_entities([
SimpliSafeAlarm(system, entry.data.get(CONF_CODE)) SimpliSafeAlarm(simplisafe, system, entry.data.get(CONF_CODE))
for system in simplisafe.systems.values() for system in simplisafe.systems.values()
], True) ], True)
class SimpliSafeAlarm(alarm.AlarmControlPanel): class SimpliSafeAlarm(AlarmControlPanel):
"""Representation of a SimpliSafe alarm.""" """Representation of a SimpliSafe alarm."""
def __init__(self, system, code): def __init__(self, simplisafe, system, code):
"""Initialize the SimpliSafe alarm.""" """Initialize the SimpliSafe alarm."""
self._async_unsub_dispatcher_connect = None self._async_unsub_dispatcher_connect = None
self._attrs = {ATTR_SYSTEM_ID: system.system_id} self._attrs = {ATTR_SYSTEM_ID: system.system_id}
self._changed_by = None
self._code = code self._code = code
self._system = system self._simplisafe = simplisafe
self._state = None self._state = None
self._system = system
# Some properties only exist for V2 or V3 systems: # Some properties only exist for V2 or V3 systems:
for prop in ( for prop in (
@ -55,14 +68,19 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
if hasattr(system, prop): if hasattr(system, prop):
self._attrs[prop] = getattr(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 @property
def code_format(self): def code_format(self):
"""Return one or more digits/characters.""" """Return one or more digits/characters."""
if not self._code: if not self._code:
return None return None
if isinstance(self._code, str) and re.search('^\\d+$', self._code): if isinstance(self._code, str) and re.search('^\\d+$', self._code):
return alarm.FORMAT_NUMBER return FORMAT_NUMBER
return alarm.FORMAT_TEXT return FORMAT_TEXT
@property @property
def device_info(self): def device_info(self):
@ -86,17 +104,17 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
@property @property
def name(self): def name(self):
"""Return the name of the device.""" """Return the name of the entity."""
return self._system.address return self._system.address
@property @property
def state(self): def state(self):
"""Return the state of the device.""" """Return the state of the entity."""
return self._state return self._state
@property @property
def unique_id(self): def unique_id(self):
"""Return the unique ID.""" """Return the unique ID of the entity."""
return self._system.system_id return self._system.system_id
def _validate_code(self, code, state): def _validate_code(self, code, state):
@ -116,11 +134,6 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
self._async_unsub_dispatcher_connect = async_dispatcher_connect( self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, TOPIC_UPDATE, update) 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): async def async_alarm_disarm(self, code=None):
"""Send disarm command.""" """Send disarm command."""
if not self._validate_code(code, 'disarming'): if not self._validate_code(code, 'disarming'):
@ -144,9 +157,10 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
async def async_update(self): async def async_update(self):
"""Update alarm status.""" """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: if self._system.state == SystemStates.error:
return return
@ -161,3 +175,20 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel):
self._state = STATE_ALARM_ARMED_AWAY self._state = STATE_ALARM_ARMED_AWAY
else: else:
self._state = None 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()

View file

@ -7,4 +7,4 @@ DATA_CLIENT = 'client'
DEFAULT_SCAN_INTERVAL = timedelta(seconds=30) DEFAULT_SCAN_INTERVAL = timedelta(seconds=30)
TOPIC_UPDATE = 'update_{0}' TOPIC_UPDATE = 'update'

View file

@ -4,7 +4,7 @@
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/components/simplisafe", "documentation": "https://www.home-assistant.io/components/simplisafe",
"requirements": [ "requirements": [
"simplisafe-python==4.0.1" "simplisafe-python==4.2.0"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [ "codeowners": [

View file

@ -1691,7 +1691,7 @@ shodan==1.13.0
simplepush==1.1.4 simplepush==1.1.4
# homeassistant.components.simplisafe # homeassistant.components.simplisafe
simplisafe-python==4.0.1 simplisafe-python==4.2.0
# homeassistant.components.sisyphus # homeassistant.components.sisyphus
sisyphus-control==2.2 sisyphus-control==2.2

View file

@ -346,7 +346,7 @@ ring_doorbell==0.2.3
rxv==0.6.0 rxv==0.6.0
# homeassistant.components.simplisafe # homeassistant.components.simplisafe
simplisafe-python==4.0.1 simplisafe-python==4.2.0
# homeassistant.components.sleepiq # homeassistant.components.sleepiq
sleepyq==0.7 sleepyq==0.7