From ce9940bbfb3993c57b22ff6dadf58caefc5c1bd0 Mon Sep 17 00:00:00 2001 From: f34rdotcom Date: Thu, 5 Apr 2018 03:51:45 +0000 Subject: [PATCH] added on_ready_change event handler and tests --- alarmdecoder/decoder.py | 29 +++++++++++++++++++++++++++++ test/test_ad2.py | 16 ++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 123fbe4..d06fd56 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -33,6 +33,7 @@ class AlarmDecoder(object): on_arm = event.Event("This event is called when the panel is armed.\n\n**Callback definition:** *def callback(device, stay)*") on_disarm = event.Event("This event is called when the panel is disarmed.\n\n**Callback definition:** *def callback(device)*") on_power_changed = event.Event("This event is called when panel power switches between AC and DC.\n\n**Callback definition:** *def callback(device, status)*") + on_ready_changed = event.Event("This event is called when panel ready state changes.\n\n**Callback definition:** *def callback(device, status)*") on_alarm = event.Event("This event is called when the alarm is triggered.\n\n**Callback definition:** *def callback(device, zone)*") on_alarm_restored = event.Event("This event is called when the alarm stops sounding.\n\n**Callback definition:** *def callback(device, zone)*") on_fire = event.Event("This event is called when a fire is detected.\n\n**Callback definition:** *def callback(device, status)*") @@ -140,6 +141,7 @@ class AlarmDecoder(object): self._battery_timeout = AlarmDecoder.BATTERY_TIMEOUT self._fire_timeout = AlarmDecoder.FIRE_TIMEOUT self._power_status = None + self._ready_status = None self._alarm_status = None self._bypass_status = {} self._armed_status = None @@ -609,6 +611,7 @@ class AlarmDecoder(object): """ if isinstance(message, Message) and not self._ignore_message_states: self._update_power_status(message) + self._update_ready_status(message) self._update_alarm_status(message) self._update_zone_bypass_status(message) self._update_armed_status(message) @@ -646,6 +649,32 @@ class AlarmDecoder(object): return self._power_status + def _update_ready_status(self, message=None, status=None): + """ + Uses the provided message to update the ready state. + + :param message: message to use to update + :type message: :py:class:`~alarmdecoder.messages.Message` + :param status: ready status, overrides message bits. + :type status: bool + + :returns: bool indicating the new status + """ + ready_status = status + if isinstance(message, Message): + ready_status = message.ready + + if ready_status is None: + return + + if ready_status != self._ready_status: + self._ready_status, old_status = ready_status, self._ready_status + + if old_status is not None: + self.on_ready_changed(status=self._ready_status) + + return self._ready_status + def _update_alarm_status(self, message=None, status=None, zone=None, user=None): """ Uses the provided message to update the alarm state. diff --git a/test/test_ad2.py b/test/test_ad2.py index 0ec6c52..71a22bd 100644 --- a/test/test_ad2.py +++ b/test/test_ad2.py @@ -20,6 +20,7 @@ class TestAlarmDecoder(TestCase): self._panicked = False self._relay_changed = False self._power_changed = False + self._ready_changed = False self._alarmed = False self._bypassed = False self._battery = False @@ -46,6 +47,7 @@ class TestAlarmDecoder(TestCase): self._decoder.on_panic += self.on_panic self._decoder.on_relay_changed += self.on_relay_changed self._decoder.on_power_changed += self.on_power_changed + self._decoder.on_ready_changed += self.on_ready_changed self._decoder.on_alarm += self.on_alarm self._decoder.on_alarm_restored += self.on_alarm_restored self._decoder.on_bypass += self.on_bypass @@ -79,6 +81,9 @@ class TestAlarmDecoder(TestCase): def on_power_changed(self, sender, *args, **kwargs): self._power_changed = kwargs['status'] + def on_ready_changed(self, sender, *args, **kwargs): + self._ready_changed = kwargs['status'] + def on_alarm(self, sender, *args, **kwargs): self._alarmed = True @@ -240,6 +245,17 @@ class TestAlarmDecoder(TestCase): msg = self._decoder._handle_message(b'[0000000100000000----],000,[f707000600e5800c0c020000]," "') self.assertTrue(self._power_changed) + def test_ready_changed_event(self): + msg = self._decoder._handle_message(b'[0000000000000000----],000,[f707000600e5800c0c020000]," "') + self.assertFalse(self._ready_changed) # Not set first time we hit it. + + msg = self._decoder._handle_message(b'[1000000000000000----],000,[f707000600e5800c0c020000]," "') + self.assertFalse(self._ready_changed) + + msg = self._decoder._handle_message(b'[0000000000000000----],000,[f707000600e5800c0c020000]," "') + self.assertTrue(self._ready_changed) + + def test_alarm_event(self): msg = self._decoder._handle_message(b'[0000000000100000----],000,[f707000600e5800c0c020000]," "') self.assertFalse(self._alarmed) # Not set first time we hit it.