From 8b4aed5990dd458c36e93523830f4350d6a8fa88 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Wed, 19 Jul 2017 11:07:27 -0700 Subject: [PATCH] Refactoring the code used to parse the numeric_code in Message. --- alarmdecoder/decoder.py | 5 +---- alarmdecoder/messages/lrr/system.py | 2 -- alarmdecoder/messages/panel_message.py | 29 ++++++++++++++++++++++++++ alarmdecoder/zonetracking.py | 8 +------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 2f588b1..38fa046 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -592,10 +592,7 @@ class AlarmDecoder(object): alarm_zone = zone if isinstance(message, Message): alarm_status = message.alarm_sounding - try: - alarm_zone = int(message.numeric_code) - except ValueError: - alarm_zone = int(message.numeric_code, 16) + alarm_zone = message.parse_numeric_code() if alarm_status != self._alarm_status: self._alarm_status, old_status = alarm_status, self._alarm_status diff --git a/alarmdecoder/messages/lrr/system.py b/alarmdecoder/messages/lrr/system.py index fe6b07f..eda27d6 100644 --- a/alarmdecoder/messages/lrr/system.py +++ b/alarmdecoder/messages/lrr/system.py @@ -109,8 +109,6 @@ class LRRSystem(object): def _handle_unknown_message(self, message): # TODO: Log this somewhere useful. - print("UNKNOWN LRR EVENT: {0}".format(message)) - return False def _get_event_status(self, message): diff --git a/alarmdecoder/messages/panel_message.py b/alarmdecoder/messages/panel_message.py index bf58151..1de86ef 100644 --- a/alarmdecoder/messages/panel_message.py +++ b/alarmdecoder/messages/panel_message.py @@ -130,6 +130,35 @@ class Message(BaseMessage): # Current cursor location on the alpha display. self.cursor_location = int(self.panel_data[21:23], 16) + def parse_numeric_code(self, force_hex=False): + """ + Parses and returns the numeric code as an integer. + + The numeric code can be either base 10 or base 16, depending on + where the message came from. + + :param force_hex: force the numeric code to be processed as base 16. + :type force_hex: boolean + + :raises: ValueError + """ + code = None + got_error = False + + if not force_hex: + try: + code = int(self.numeric_code) + except ValueError: + got_error = True + + if force_hex or got_error: + try: + code = int(self.numeric_code, 16) + except ValueError: + raise + + return code + def dict(self, **kwargs): """ Dictionary representation. diff --git a/alarmdecoder/zonetracking.py b/alarmdecoder/zonetracking.py index 04ef9cf..e56ec12 100644 --- a/alarmdecoder/zonetracking.py +++ b/alarmdecoder/zonetracking.py @@ -178,13 +178,7 @@ class Zonetracker(object): # Process fault elif self.alarmdecoder_object.mode != DSC and (message.check_zone or message.text.startswith("FAULT") or message.text.startswith("ALARM")): - # Apparently this representation can be both base 10 - # or base 16, depending on where the message came - # from. - try: - zone = int(message.numeric_code) - except ValueError: - zone = int(message.numeric_code, 16) + zone = message.parse_numeric_code() # NOTE: Odd case for ECP failures. Apparently they report as # zone 191 (0xBF) regardless of whether or not the