Skip to content

Commit

Permalink
Decode zone ID as dec, not hex in SYSTEM_STATUS event (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickw444 committed Feb 28, 2019
1 parent 3c59b0d commit 7e93367
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nessclient/event.py
Expand Up @@ -96,11 +96,13 @@ def __init__(self, type: 'SystemStatusEvent.EventType', zone: int, area: int,

@classmethod
def decode(cls, packet: Packet) -> 'SystemStatusEvent':
data = bytearray.fromhex(packet.data)
event_type = int(packet.data[0:2], 16)
zone = int(packet.data[2:4])
area = int(packet.data[4:6], 16)
return SystemStatusEvent(
type=SystemStatusEvent.EventType(data[0]),
zone=data[1],
area=data[2],
type=SystemStatusEvent.EventType(event_type),
zone=zone,
area=area,
timestamp=packet.timestamp,
address=packet.address,
)
Expand Down
14 changes: 14 additions & 0 deletions nessclient_tests/test_event.py
Expand Up @@ -127,6 +127,20 @@ def test_zone_sealed(self):
self.assertEqual(event.type,
SystemStatusEvent.EventType.SEALED)

def test_zone_unsealed_with_zone_15(self):
pkt = make_packet(CommandType.SYSTEM_STATUS, '001500')
event = SystemStatusEvent.decode(pkt)
self.assertEqual(event.area, 0)
self.assertEqual(event.zone, 15)
self.assertEqual(event.type, SystemStatusEvent.EventType.UNSEALED)

def test_zone_unsealed_with_zone_16(self):
pkt = make_packet(CommandType.SYSTEM_STATUS, '001600')
event = SystemStatusEvent.decode(pkt)
self.assertEqual(event.area, 0)
self.assertEqual(event.zone, 16)
self.assertEqual(event.type, SystemStatusEvent.EventType.UNSEALED)


def make_packet(command: CommandType, data: str):
return Packet(address=0, command=command,
Expand Down
11 changes: 11 additions & 0 deletions nessclient_tests/test_packet.py
Expand Up @@ -131,3 +131,14 @@ def test_bad_timestamp(self):
self.assertEqual(pkt.data, '000700')
self.assertEqual(pkt.timestamp, datetime.datetime(
year=2019, month=2, day=25, hour=18, minute=0, second=0))

def test_decode_zone_16(self):
pkt = Packet.decode('8700036100160019022823032274')
self.assertEqual(pkt.start, 0x87)
self.assertEqual(pkt.address, 0x00)
self.assertEqual(pkt.length, 3)
self.assertEqual(pkt.seq, 0x00)
self.assertEqual(pkt.command, CommandType.SYSTEM_STATUS)
self.assertEqual(pkt.data, '001600')
self.assertEqual(pkt.timestamp, datetime.datetime(
year=2019, month=2, day=28, hour=23, minute=3, second=22))

0 comments on commit 7e93367

Please sign in to comment.