Skip to content

Commit

Permalink
Merge pull request #36 from hostcc/feature/simulate-sensor-activity-i…
Browse files Browse the repository at this point in the history
…n-alarm-cb

feat: Set sensor occupancy upon alarm
  • Loading branch information
hostcc committed Aug 27, 2024
2 parents 018b402 + eac2c85 commit 2452876
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/pyg90alarm/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ async def on_sensor_activity(self, idx, name, occupancy=True):
alerts (only for `door` type sensors, if door open/close alerts are
enabled)
"""
_LOGGER.debug('on_sensor_acitvity: %s %s %s', idx, name, occupancy)
_LOGGER.debug('on_sensor_activity: %s %s %s', idx, name, occupancy)
sensor = await self.find_sensor(idx, name)
if sensor:
_LOGGER.debug('Setting occupancy to %s (previously %s)',
Expand Down Expand Up @@ -573,9 +573,15 @@ async def on_alarm(self, event_id, zone_name):
"""
sensor = await self.find_sensor(event_id, zone_name)
# The callback is still delivered to the caller even if the sensor
# isn't found, only `extra_data` is skipped. That is to ensur the
# isn't found, only `extra_data` is skipped. That is to ensure the
# important callback isn't filtered
extra_data = sensor.extra_data if sensor else None
# Invoke the sensor activity callback to set the sensor occupancy if
# sensor is known, but only if that isn't already set - it helps when
# device notifications on triggerring sensor's activity aren't receveid
# by a reason
if sensor and not sensor.occupancy:
await self.on_sensor_activity(event_id, zone_name, True)

G90Callback.invoke(
self._alarm_cb, event_id, zone_name, extra_data
Expand Down
13 changes: 12 additions & 1 deletion tests/test_alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ async def test_door_open_close_callback(mock_device):
b'["Hall",100,0,1,1,0,32,0,0,16,1,0,""],'
b'["Room",101,0,1,1,0,32,0,0,16,1,0,""]'
b']]IEND\0',
# Alert configuration, used by sensor activity callback invoked when
# handling alarm
b'ISTART[117,[256]]IEND\0',
],
notification_data=[
b'[208,[3,100,1,1,"Hall","DUMMYGUID",1630876128,0,[""]]]\0',
Expand Down Expand Up @@ -425,6 +427,8 @@ async def test_alarm_callback(mock_device):
await asyncio.wait([future], timeout=0.1)
# Verify extra data is passed to the callback
alarm_cb.assert_called_once_with(100, 'Hall', 'Dummy extra data')
# Verify the triggering sensor is set to active
assert sensors[0].occupancy is True

# Simulate alarm for sensor with no extra data
alarm_cb.reset_mock()
Expand All @@ -433,6 +437,8 @@ async def test_alarm_callback(mock_device):
await asyncio.wait([future], timeout=0.1)
# Verify no extra data is passed to the callback
alarm_cb.assert_called_once_with(101, 'Room', None)
# Verify the triggering sensor is set to active
assert sensors[0].occupancy is True

# Simulate alarm for non-existent sensor
alarm_cb.reset_mock()
Expand Down Expand Up @@ -517,6 +523,9 @@ async def test_history(mock_device):
b'["Sensor 1",33,0,138,0,0,33,0,0,17,1,0,""],'
b'["Sensor 2",100,0,138,0,0,33,0,0,17,1,0,""]'
b']]IEND\0',
# Alert configuration, used by sensor activity callback invoked when
# handling alarm
b'ISTART[117,[256]]IEND\0',
])
async def test_simulate_alerts_from_history(mock_device):
# Callback handlers for alarm and arm/disarm, just setting their
Expand Down Expand Up @@ -545,9 +554,11 @@ async def test_simulate_alerts_from_history(mock_device):
# Stop simulating the alert from history
await g90.stop_simulating_alerts_from_history()

sensors = await g90.get_sensors()
# Ensure callbacks have been called and with expected arguments
alarm_cb.assert_called_once_with(33, 'Sensor 1', None)
armdisarm_cb.assert_called_once_with(3)
assert sensors[0].occupancy is True


async def test_simulate_alerts_from_history_exception(mock_device, caplog):
Expand Down

0 comments on commit 2452876

Please sign in to comment.