From f1a71def32a6923320d198ce92a08e8dadfb4ad4 Mon Sep 17 00:00:00 2001 From: Scott Date: Sun, 8 Dec 2013 09:56:37 -0800 Subject: [PATCH] Simplification and compliance (mostly) with pep8. --- alarmdecoder/decoder.py | 15 +++++++-------- alarmdecoder/devices.py | 19 ++++++++++++------- alarmdecoder/event/event.py | 1 + alarmdecoder/messages.py | 6 +++++- alarmdecoder/tests/test_ad2.py | 1 + alarmdecoder/tests/test_devices.py | 2 ++ alarmdecoder/tests/test_messages.py | 1 + alarmdecoder/tests/test_zonetracking.py | 3 ++- alarmdecoder/util.py | 5 +++++ alarmdecoder/zonetracking.py | 4 +++- 10 files changed, 39 insertions(+), 18 deletions(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 30295b8..e61b804 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -12,6 +12,7 @@ from .util import CommError, NoDeviceError from .messages import Message, ExpanderMessage, RFMessage, LRRMessage from .zonetracking import Zonetracker + class AlarmDecoder(object): """ High-level wrapper around Alarm Decoder (AD2) devices. @@ -237,8 +238,8 @@ class AlarmDecoder(object): raise InvalidMessageError() msg = None - header = data[0:4] + if header[0] != '!' or header == '!KPE': msg = Message(data) @@ -295,7 +296,7 @@ class AlarmDecoder(object): self.on_panic(status=True) elif msg.event_type == 'CANCEL': - if self._panic_status == True: + if self._panic_status is True: self._panic_status = False self.on_panic(status=False) @@ -321,11 +322,9 @@ class AlarmDecoder(object): elif k == 'MASK': self.address_mask = int(v, 16) elif k == 'EXP': - for z in range(5): - self.emulate_zone[z] = (v[z] == 'Y') + self.emulate_zone = [v[z] == 'Y' for z in range(5)] elif k == 'REL': - for r in range(4): - self.emulate_relay[r] = (v[r] == 'Y') + self.emulate_relay = [v[r] == 'Y' for r in range(4)] elif k == 'LRR': self.emulate_lrr = (v == 'Y') elif k == 'DEDUPLICATE': @@ -371,14 +370,14 @@ class AlarmDecoder(object): if message.battery_low == self._battery_status[0]: self._battery_status = (self._battery_status[0], time.time()) else: - if message.battery_low == True or time.time() > self._battery_status[1] + AlarmDecoder.BATTERY_TIMEOUT: + if message.battery_low is True or time.time() > self._battery_status[1] + AlarmDecoder.BATTERY_TIMEOUT: self._battery_status = (message.battery_low, time.time()) self.on_low_battery(status=self._battery_status) if message.fire_alarm == self._fire_status[0]: self._fire_status = (self._fire_status[0], time.time()) else: - if message.fire_alarm == True or time.time() > self._fire_status[1] + AlarmDecoder.FIRE_TIMEOUT: + if message.fire_alarm is True or time.time() > self._fire_status[1] + AlarmDecoder.FIRE_TIMEOUT: self._fire_status = (message.fire_alarm, time.time()) self.on_fire(status=self._fire_status) diff --git a/alarmdecoder/devices.py b/alarmdecoder/devices.py index 2480991..1da682b 100644 --- a/alarmdecoder/devices.py +++ b/alarmdecoder/devices.py @@ -4,10 +4,12 @@ Contains different types of devices belonging to the Alarm Decoder (AD2) family. .. moduleauthor:: Scott Petersen """ -import usb.core, usb.util +import usb.core +import usb.util import time import threading -import serial, serial.tools.list_ports +import serial +import serial.tools.list_ports import socket from OpenSSL import SSL, crypto @@ -16,6 +18,7 @@ from pyftdi.pyftdi.usbtools import * from .util import CommError, TimeoutError, NoDeviceError from .event import event + class Device(object): """ Generic parent device to all Alarm Decoder (AD2) products. @@ -143,6 +146,7 @@ class Device(object): time.sleep(0.01) + class USBDevice(Device): """ AD2USB device exposed with PyFTDI's interface. @@ -334,11 +338,11 @@ class USBDevice(Device): # Open the device and start up the thread. try: self._device.open(self._vendor_id, - self._product_id, - self._endpoint, - self._device_number, - self._serial_number, - self._description) + self._product_id, + self._endpoint, + self._device_number, + self._serial_number, + self._description) self._device.set_baudrate(baudrate) @@ -763,6 +767,7 @@ class SerialDevice(Device): return ret + class SocketDevice(Device): """ Device that supports communication with an Alarm Decoder (AD2) that is diff --git a/alarmdecoder/event/event.py b/alarmdecoder/event/event.py index 203d1a5..6200fa8 100644 --- a/alarmdecoder/event/event.py +++ b/alarmdecoder/event/event.py @@ -9,6 +9,7 @@ # * Added type check in fire() # * Removed earg from fire() and added support for args/kwargs. + class Event(object): def __init__(self, doc=None): diff --git a/alarmdecoder/messages.py b/alarmdecoder/messages.py index 6698c9c..6d94b49 100644 --- a/alarmdecoder/messages.py +++ b/alarmdecoder/messages.py @@ -9,6 +9,7 @@ import re from .util import InvalidMessageError + class BaseMessage(object): """ Base class for messages. @@ -29,6 +30,7 @@ class BaseMessage(object): """ return self.raw + class Message(BaseMessage): """ Represents a message from the alarm panel. @@ -141,6 +143,7 @@ class Message(BaseMessage): if int(self.panel_data[19:21], 16) & 0x01 > 0: self.cursor_location = int(self.bitfield[21:23], 16) # Alpha character index that the cursor is on. + class ExpanderMessage(BaseMessage): """ Represents a message from a zone or relay expansion module. @@ -151,7 +154,6 @@ class ExpanderMessage(BaseMessage): RELAY = 1 """Flag indicating that the expander message relates to a Relay Expander.""" - type = None """Expander message type: ExpanderMessage.ZONE or ExpanderMessage.RELAY""" address = -1 @@ -205,6 +207,7 @@ class ExpanderMessage(BaseMessage): else: raise InvalidMessageError('Unknown expander message header: {0}'.format(data)) + class RFMessage(BaseMessage): """ Represents a message from an RF receiver. @@ -267,6 +270,7 @@ class RFMessage(BaseMessage): except ValueError: raise InvalidMessageError('Received invalid message: {0}'.format(data)) + class LRRMessage(BaseMessage): """ Represent a message from a Long Range Radio. diff --git a/alarmdecoder/tests/test_ad2.py b/alarmdecoder/tests/test_ad2.py index 3310af7..f111b27 100644 --- a/alarmdecoder/tests/test_ad2.py +++ b/alarmdecoder/tests/test_ad2.py @@ -9,6 +9,7 @@ from ..messages import Message, RFMessage, LRRMessage, ExpanderMessage from ..event.event import Event, EventHandler from ..zonetracking import Zonetracker + class TestAlarmDecoder(TestCase): def setUp(self): self._panicked = False diff --git a/alarmdecoder/tests/test_devices.py b/alarmdecoder/tests/test_devices.py index c625b72..af656b8 100644 --- a/alarmdecoder/tests/test_devices.py +++ b/alarmdecoder/tests/test_devices.py @@ -164,6 +164,7 @@ class TestUSBDevice(TestCase): with self.assertRaises(CommError): self._device.read_line() + class TestSerialDevice(TestCase): def setUp(self): self._device = SerialDevice() @@ -250,6 +251,7 @@ class TestSerialDevice(TestCase): with self.assertRaises(CommError): self._device.read_line() + class TestSocketDevice(TestCase): def setUp(self): self._device = SocketDevice() diff --git a/alarmdecoder/tests/test_messages.py b/alarmdecoder/tests/test_messages.py index 5d7a7d1..a7f3b45 100644 --- a/alarmdecoder/tests/test_messages.py +++ b/alarmdecoder/tests/test_messages.py @@ -3,6 +3,7 @@ from unittest import TestCase from ..messages import Message, ExpanderMessage, RFMessage, LRRMessage from ..util import InvalidMessageError + class TestMessages(TestCase): def setUp(self): pass diff --git a/alarmdecoder/tests/test_zonetracking.py b/alarmdecoder/tests/test_zonetracking.py index 063282c..51f19e6 100644 --- a/alarmdecoder/tests/test_zonetracking.py +++ b/alarmdecoder/tests/test_zonetracking.py @@ -4,6 +4,7 @@ from mock import Mock, MagicMock from ..messages import Message, ExpanderMessage from ..zonetracking import Zonetracker, Zone + class TestZonetracking(TestCase): def setUp(self): self._zonetracker = Zonetracker() @@ -135,7 +136,7 @@ class TestZonetracking(TestCase): self._zonetracker.update(msg) self.assertIn(4, self._zonetracker._zones_faulted) - self._zonetracker._zones[4].timestamp -= 35 # forcefully expire the zone + self._zonetracker._zones[4].timestamp -= 35 # forcefully expire the zone # generic message to force an update. msg = Message('[0000000000000000----],000,[f707000600e5800c0c020000]," "') diff --git a/alarmdecoder/util.py b/alarmdecoder/util.py index bd3871e..a9dbc1e 100644 --- a/alarmdecoder/util.py +++ b/alarmdecoder/util.py @@ -7,30 +7,35 @@ Provides utility classes for the Alarm Decoder (AD2) devices. import time import threading + class NoDeviceError(Exception): """ No devices found. """ pass + class CommError(Exception): """ There was an error communicating with the device. """ pass + class TimeoutError(Exception): """ There was a timeout while trying to communicate with the device. """ pass + class InvalidMessageError(Exception): """ The format of the panel message was invalid. """ pass + class Firmware(object): """ Represents firmware for the Alarm Decoder devices. diff --git a/alarmdecoder/zonetracking.py b/alarmdecoder/zonetracking.py index 818184d..c92c7b6 100644 --- a/alarmdecoder/zonetracking.py +++ b/alarmdecoder/zonetracking.py @@ -10,6 +10,7 @@ import time from .event import event from .messages import ExpanderMessage + class Zone(object): """ Representation of a panel zone. @@ -22,7 +23,7 @@ class Zone(object): CHECK = 2 # Wire fault """Status indicating that there is a wiring issue with the zone.""" - STATUS = { CLEAR: 'CLEAR', FAULT: 'FAULT', CHECK: 'CHECK' } + STATUS = {CLEAR: 'CLEAR', FAULT: 'FAULT', CHECK: 'CHECK'} def __init__(self, zone=0, name='', status=CLEAR): """ @@ -52,6 +53,7 @@ class Zone(object): """ return 'Zone({0}, {1}, ts {2})'.format(self.zone, Zone.STATUS[self.status], self.timestamp) + class Zonetracker(object): """ Handles tracking of zone and their statuses.