Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Mar 28, 2024
1 parent 77a3728 commit f5d66ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
29 changes: 17 additions & 12 deletions paradox/hardware/spectra_magellan/adapters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-

import datetime
from enum import Enum

from construct import Adapter
from enum import Enum


class DateAdapter(Adapter):
def _decode(self, obj, context, path):
Expand Down Expand Up @@ -35,7 +34,6 @@ class PartitionStateAdapter(Adapter):
states = dict(arm=4, disarm=5, arm_sleep=3, arm_stay=1, none=0)

def _decode(self, obj, context, path):

for k, v in enumerate(self.states):
if v == obj[0]:
return k
Expand All @@ -53,7 +51,6 @@ class ZoneStateAdapter(Adapter):
states = dict(bypass=0x10)

def _decode(self, obj, context, path):

for k, v in enumerate(self.states):
if v == obj[0]:
return k
Expand Down Expand Up @@ -227,16 +224,24 @@ def _decode(self, obj, context, path):
disabled = sum(obj) == 0

if disabled:
return 'disabled'
return "disabled"

try:
activation_event = PGMDefinitionAdapter.PGMEvent(obj[0]) if sum(obj[:3]) != 0 else 'disabled'
except:
activation_event = 'unknown'
activation_event = (
PGMDefinitionAdapter.PGMEvent(obj[0])
if sum(obj[:3]) != 0
else "disabled"
)
except Exception:
activation_event = "unknown"

try:
deactivation_event = PGMDefinitionAdapter.PGMEvent(obj[3]) if sum(obj[3:]) != 0 else 'disabled'
except:
deactivation_event = 'unknown'
deactivation_event = (
PGMDefinitionAdapter.PGMEvent(obj[3])
if sum(obj[3:]) != 0
else "disabled"
)
except Exception:
deactivation_event = "unknown"

return dict(activation=activation_event, deactivation=deactivation_event)
56 changes: 36 additions & 20 deletions tests/hardware/spectra_magellan/test_event_parsing.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,67 @@
import binascii
import typing

import pytest

from paradox import event
from paradox.hardware.spectra_magellan.event import event_map
from paradox.hardware.spectra_magellan.parsers import LiveEvent

events = [
b"e2141301040b08300200000000000000000000000000000000000000000000020000000055", # Software Log on
b"e2141301040b042d0600000000000000000000000000000000000000000000010000000051", # Clock loss restore
b"e2141301040b09030300000000000000000000000000000000000000000000000000000028",
(
b"e2141301040b08300200000000000000000000000000000000000000000000020000000055"
), # Software Log on
(
b"e2141301040b042d0600000000000000000000000000000000000000000000010000000051"
), # Clock loss restore
(b"e2141301040b09030300000000000000000000000000000000000000000000000000000028"),
# {'type': 'Bell', 'minor': (3, ' Bell squawk disarm'), 'major': (3, 'Bell status (Partition 1)')}
b"e2141301040b09220100000000000000000000000000000000000000000000010000000046",
(b"e2141301040b09220100000000000000000000000000000000000000000000010000000046"),
# {'type': 'Special', 'minor': (1, 'Disarming through WinLoad'), 'major': (34, 'Special disarming')}
b"e2141301040b09030300000000000000000000000000000000000000000000000000000028",
(b"e2141301040b09030300000000000000000000000000000000000000000000000000000028"),
# {'type': 'Bell', 'minor': (3, ' Bell squawk disarm'), 'major': (3, 'Bell status (Partition 1)')}
b"e2141301040b09220101000000000000000000000000000000000000000000010000000047",
(b"e2141301040b09220101000000000000000000000000000000000000000000010000000047"),
# {'type': 'Special', 'minor': (1, 'Disarming through WinLoad'), 'major': (34, 'Special disarming')}
b"e2141301040b09030200000000000000000000000000000000000000000000010000000028",
(b"e2141301040b09030200000000000000000000000000000000000000000000010000000028"),
# {'type': 'Bell', 'minor': (2, ' Bell squawk arm'), 'major': (3, 'Bell status (Partition 1)')}
b"e2141301040b0a1e0500000000000000000000000000000000000000000000010000000047",
(b"e2141301040b0a1e0500000000000000000000000000000000000000000000010000000047"),
# {'type': 'Special', 'minor': (5, 'Arming through WinLoad'), 'major': (30, 'Special arming')}
b"e2141301040b0b1e0501000000000000000000000000000000000000000000010000000049",
(b"e2141301040b0b1e0501000000000000000000000000000000000000000000010000000049"),
# {'type': 'Special', 'minor': (5, 'Arming through WinLoad'), 'major': (30, 'Special arming')}
b"e214120b15110e061b00000000000000000000000000000000000000000000000000000068",
b"e2141301040b09020b0100000000025858585858585858585858202020202001000000009b",
(b"e214120b15110e061b00000000000000000000000000000000000000000000000000000068"),
(b"e2141301040b09020b0100000000025858585858585858585858202020202001000000009b"),
# {'type': 'Partition', 'minor': (11, 'Disarm partition'), 'major': (2, 'Partition status')}
b"e2141301040b09020800000000000258585858585858585858582020202020000000000096",
(b"e2141301040b09020800000000000258585858585858585858582020202020000000000096"),
# {'type': 'Partition', 'minor': (8, 'Squawk ON (Partition 1)'), 'major': (2, 'Partition status')}
b"e2141301040b09020900000000000258585858585858585858582020202020000000000097",
(b"e2141301040b09020900000000000258585858585858585858582020202020000000000097"),
# {'type': 'Partition', 'minor': (9, 'Squawk OFF (Partition 1)'), 'major': (2, 'Partition status')}
b"e2141301040b09020800000000000258585858585858585858582020202020000000000096",
(b"e2141301040b09020800000000000258585858585858585858582020202020000000000096"),
# {'type': 'Partition', 'minor': (8, 'Squawk ON (Partition 1)'), 'major': (2, 'Partition status')}
b"e2141301040b09020e0000000000025858585858585858585858202020202001000000009d",
(b"e2141301040b09020e0000000000025858585858585858585858202020202001000000009d"),
# {'type': 'Partition', 'minor': (14, 'Exit delay started'), 'major': (2, 'Partition status')}
b"e2141301040b0b020c0100000000025858585858585858585858202020202001000000009e",
(b"e2141301040b0b020c0100000000025858585858585858585858202020202001000000009e"),
# { 'type': 'Partition', 'minor': (12, 'Arm partition'), 'major': (2, 'Partition status')}
b"e2141301040b09020e0100000000025858585858585858585858202020202001000000009e",
(b"e2141301040b09020e0100000000025858585858585858585858202020202001000000009e"),
# {'type': 'Partition', 'minor': (8, 'Squawk ON (Partition 1)'), 'major': (2, 'Partition status')}
b"e2141301040b09020900000000000258585858585858585858582020202020010000000098",
(b"e2141301040b09020900000000000258585858585858585858582020202020010000000098"),
# {'type': 'Partition', 'minor': (9, 'Squawk OFF (Partition 1)'), 'major': (2, 'Partition status')}
b"e2141301040b0a020c0000000000025858585858585858585858202020202001000000009c",
(b"e2141301040b0a020c0000000000025858585858585858585858202020202001000000009c"),
# {'type': 'Partition', 'minor': (12, 'Arm partition'), 'major': (2, 'Partition status')}
b"e2141301040b09020b0000000000025858585858585858585858202020202001000000009a",
(b"e2141301040b09020b0000000000025858585858585858585858202020202001000000009a"),
# {'type': 'Partition', 'minor': (11, 'Disarm partition'), 'major': (2, 'Partition status')}
]


@pytest.mark.parametrize("hex", events)
def test_event_parsing(hex):
payload = binascii.unhexlify(hex)

raw = LiveEvent.parse(payload)
event_ = event.LiveEvent(raw, event_map)

print(event_)


def test_disarm_partition0():
hex = b"e2141301040b09020b0100000000025858585858585858585858202020202001000000009b" # {'type': 'Partition', 'minor': (11, 'Disarm partition'), 'major': (2, 'Partition status')}
payload = binascii.unhexlify(hex)
Expand Down

0 comments on commit f5d66ca

Please sign in to comment.