Skip to content

Commit

Permalink
✨ [feature] Add tampered_sensors partition attribute
Browse files Browse the repository at this point in the history
This allows to escalate to the partition the tampered status of
sensors, which might make it easier to use than the per-sensor
attribute flag.
  • Loading branch information
XaF committed Jan 8, 2023
1 parent 3f40c8a commit 4b6dcef
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
5 changes: 5 additions & 0 deletions apps/qolsysgw/mqtt/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ def update_attributes(self):
'last_error_desc': self._partition.last_error_desc,
'last_error_at': self._partition.last_error_at,
'disarm_failed': self._partition.disarm_failed,
'tampered_sensors': (
','.join(str(s) for s in self._partition.tampered_sensors)
if self._partition.tampered_sensors
else None
),
}),
)

Expand Down
44 changes: 40 additions & 4 deletions apps/qolsysgw/qolsys/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timezone

from qolsys.observable import QolsysObservable
from qolsys.sensors import QolsysSensor


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -32,6 +33,7 @@ def __init__(self, partition_id: int, name: str, status: str,
self._last_error_desc = None
self._last_error_at = None
self._disarm_failed = 0
self._tampered_sensors = []

@property
def id(self):
Expand Down Expand Up @@ -73,11 +75,16 @@ def last_error_at(self):
def disarm_failed(self):
return self._disarm_failed

@property
def tampered_sensors(self):
return self._tampered_sensors

@status.setter
def status(self, value):
new_value = value.upper()
if self._status != new_value:
LOGGER.debug(f"Partition '{self.id}' ({self.name}) status updated to '{new_value}'")
LOGGER.debug(f"Partition '{self.id}' ({self.name}) status "
f"updated to '{new_value}'")
prev_value = self._status

self._status = new_value
Expand All @@ -95,7 +102,8 @@ def status(self, value):
def secure_arm(self, value):
new_value = bool(value)
if self._secure_arm != new_value:
LOGGER.debug(f"Partition '{self.id}' ({self.name}) secure arm updated to '{new_value}'")
LOGGER.debug(f"Partition '{self.id}' ({self.name}) secure arm "
f"updated to '{new_value}'")
prev_value = self._secure_arm
self._secure_arm = new_value

Expand All @@ -108,7 +116,8 @@ def alarm_type(self, value):
value = value.upper()

if self._alarm_type != value:
LOGGER.debug(f"Partition '{self.id}' ({self.name}) alarm type updated to '{value}'")
LOGGER.debug(f"Partition '{self.id}' ({self.name}) alarm type "
f"updated to '{value}'")
prev_value = self._alarm_type
self._alarm_type = value

Expand All @@ -120,11 +129,30 @@ def disarm_failed(self, value):
new_value = int(value)

if self._disarm_failed != new_value:
LOGGER.debug(f"Partition '{self.id}' ({self.name}) disarm failed updated to '{value}'")
LOGGER.debug(f"Partition '{self.id}' ({self.name}) disarm failed "
f"updated to '{new_value}'")
self._disarm_failed = new_value

self.notify(change=self.NOTIFY_UPDATE_ATTRIBUTES)

@tampered_sensors.setter
def tampered_sensors(self, value):
new_value = sorted(value)

if self._tampered_sensors != new_value:
LOGGER.debug(f"Partition '{self.id}' ({self.name}) tampered "
f"sensors updated to '{new_value}'")
self._tampered_sensors = new_value

self.notify(change=self.NOTIFY_UPDATE_ATTRIBUTES)

def _update_tampered_sensors(self):
self.tampered_sensors = sorted([
zone_id
for zone_id, zone in self._sensors.items()
if zone.tampered
])

def triggered(self, alarm_type: str = None):
self.status = 'ALARM'
self.alarm_type = alarm_type
Expand Down Expand Up @@ -153,6 +181,7 @@ def add_sensor(self, sensor):
return

self._sensors[sensor.zone_id] = sensor
sensor.register(self, callback=self._update_sensor_callback)
self.notify(change=self.NOTIFY_ADD_SENSOR, new_value=sensor)

def update_sensor(self, sensor):
Expand All @@ -170,9 +199,16 @@ def remove_zone(self, zone_id):

del self._sensors[zone_id]

zone.unregister(self)

self.notify(change=self.NOTIFY_REMOVE_SENSOR,
prev_value=zone)

def _update_sensor_callback(self, sensor: QolsysSensor, change,
prev_value=None, new_value=None):
if change == QolsysSensor.NOTIFY_UPDATE_PATTERN.format(attr='tampered'):
self._update_tampered_sensors()

def __str__(self):
return (f"<QolsysPartition id={self.id} name={self.name} "
f"status={self.status} secure_arm={self.secure_arm} "
Expand Down
8 changes: 6 additions & 2 deletions tests/end-to-end/test_qolsysgw.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ async def _check_initial_state(self, ctx):
'disarm_failed': 0,
'secure_arm': False,
'supported_features': 63,
'tampered_sensors': None,
},
'entity_id': 'alarm_control_panel.partition0',
'state': 'disarmed',
Expand Down Expand Up @@ -364,6 +365,7 @@ async def _check_initial_state(self, ctx):
'disarm_failed': 0,
'secure_arm': False,
'supported_features': 63,
'tampered_sensors': None,
},
'entity_id': 'alarm_control_panel.partition1',
'state': 'disarmed',
Expand Down Expand Up @@ -430,7 +432,7 @@ async def _check_panel_events(self, ctx):
closed_entities = [100, 110, 111, 120, 121, 130, 140, 141, 150,
200, 210, 220]
open_entities = [101]
tamper_entities = [100, 110, 111, 210]
tamper_entities = [100, 110, 111, 210, 220]
untamper_entities_to_open = [100]
untamper_entities_to_closed_short = [110]
untamper_entities_to_closed_long = [111]
Expand Down Expand Up @@ -532,6 +534,7 @@ def zone_active_event(zone_id, closed=False):
'disarm_failed': 1,
'secure_arm': False,
'supported_features': 63,
'tampered_sensors': None,
},
'entity_id': 'alarm_control_panel.partition0',
'state': 'armed_away',
Expand Down Expand Up @@ -679,6 +682,7 @@ def zone_active_event(zone_id, closed=False):
'disarm_failed': 0,
'secure_arm': True,
'supported_features': 63,
'tampered_sensors': '210,220',
},
'entity_id': 'alarm_control_panel.partition1',
'state': 'triggered',
Expand Down Expand Up @@ -717,7 +721,7 @@ def zone_active_event(zone_id, closed=False):
'zone_alarm_type': 0,
'zone_physical_type': 10,
'zone_type': 8,
'tampered': False,
'tampered': True,
},
'entity_id': 'binary_sensor.my_heat_sensor',
'state': 'on',
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_qolsys_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async def _check_partition_mqtt_messages(self, gw, partition_flat_name,
'last_error_desc': state.last_error_desc,
'last_error_at': state.last_error_at,
'disarm_failed': state.disarm_failed,
'tampered_sensors': None,
},
mqtt_attributes['payload'],
)
Expand Down

0 comments on commit 4b6dcef

Please sign in to comment.