Add attribute to show who last un/set alarm (SPC) (#9906)

* Add attribute to show who last un/set alarm.

This allows showing the name of the SPC user who last
issued an arm/disarm command and also allows for
automations to depend on this value.

* Optimize

* Update spc.py

* Update spc.py

* fix

* Fix test.

* Fix for removed is_state_attr.
This commit is contained in:
Martin Berg authored and Paulus Schoutsen committed 2017-11-11 12:36:03 -08:00
1 parent 68fb995c63
commit db56748d88
6 files changed
+82 -45

No files matched your search

@@ -34,10 +34,8 @@ def async_setup_platform(hass, config, async_add_devices,
discovery_info[ATTR_DISCOVER_AREAS] is None):
return
devices = [SpcAlarm(hass=hass,
area_id=area['id'],
name=area['name'],
state=_get_alarm_state(area['mode']))
api = hass.data[DATA_API]
devices = [SpcAlarm(api, area)
for area in discovery_info[ATTR_DISCOVER_AREAS]]
async_add_devices(devices)
@@ -46,21 +44,29 @@ def async_setup_platform(hass, config, async_add_devices,
class SpcAlarm(alarm.AlarmControlPanel):
"""Represents the SPC alarm panel."""
def __init__(self, hass, area_id, name, state):
def __init__(self, api, area):
"""Initialize the SPC alarm panel."""
self._hass = hass
self._area_id = area_id
self._name = name
self._state = state
self._api = hass.data[DATA_API]
hass.data[DATA_REGISTRY].register_alarm_device(area_id, self)
self._area_id = area['id']
self._name = area['name']
self._state = _get_alarm_state(area['mode'])
if self._state == STATE_ALARM_DISARMED:
self._changed_by = area.get('last_unset_user_name', 'unknown')
else:
self._changed_by = area.get('last_set_user_name', 'unknown')
self._api = api
@asyncio.coroutine
def async_update_from_spc(self, state):
def async_added_to_hass(self):
"""Calbback for init handlers."""
self.hass.data[DATA_REGISTRY].register_alarm_device(
self._area_id, self)
@asyncio.coroutine
def async_update_from_spc(self, state, extra):
"""Update the alarm panel with a new state."""
self._state = state
yield from self.async_update_ha_state()
self._changed_by = extra.get('changed_by', 'unknown')
self.async_schedule_update_ha_state()
@property
def should_poll(self):
@@ -72,6 +78,11 @@ class SpcAlarm(alarm.AlarmControlPanel):
"""Return the name of the device."""
return self._name
@property
def changed_by(self):
"""Return the user the last change was triggered by."""
return self._changed_by
@property
def state(self):
"""Return the state of the device."""
@@ -67,7 +67,7 @@ class SpcBinarySensor(BinarySensorDevice):
spc_registry.register_sensor_device(zone_id, self)
@asyncio.coroutine
def async_update_from_spc(self, state):
def async_update_from_spc(self, state, extra):
"""Update the state of the device."""
self._state = state
yield from self.async_update_ha_state()
+7 -3
View File
@@ -87,9 +87,14 @@ def _async_process_message(sia_message, spc_registry):
# ZX - Zone Short
# ZD - Zone Disconnected
if sia_code in ('BA', 'CG', 'NL', 'OG', 'OQ'):
extra = {}
if sia_code in ('BA', 'CG', 'NL', 'OG'):
# change in area status, notify alarm panel device
device = spc_registry.get_alarm_device(spc_id)
data = sia_message['description'].split('¦')
if len(data) == 3:
extra['changed_by'] = data[1]
else:
# change in zone status, notify sensor device
device = spc_registry.get_sensor_device(spc_id)
@@ -98,7 +103,6 @@ def _async_process_message(sia_message, spc_registry):
'CG': STATE_ALARM_ARMED_AWAY,
'NL': STATE_ALARM_ARMED_HOME,
'OG': STATE_ALARM_DISARMED,
'OQ': STATE_ALARM_DISARMED,
'ZO': STATE_ON,
'ZC': STATE_OFF,
'ZX': STATE_UNKNOWN,
@@ -110,7 +114,7 @@ def _async_process_message(sia_message, spc_registry):
_LOGGER.warning("No device mapping found for SPC area/zone id %s.",
spc_id)
elif new_state:
yield from device.async_update_from_spc(new_state)
yield from device.async_update_from_spc(new_state, extra)
class SpcRegistry: