A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.8 KiB

  1. from unittest import TestCase
  2. from mock import Mock, MagicMock
  3. from alarmdecoder.messages import Message, ExpanderMessage
  4. from alarmdecoder.zonetracking import Zonetracker, Zone
  5. class TestZonetracking(TestCase):
  6. def setUp(self):
  7. self._zonetracker = Zonetracker()
  8. self._zonetracker.on_fault += self.fault_event
  9. self._zonetracker.on_restore += self.restore_event
  10. self._faulted = False
  11. self._restored = False
  12. def tearDown(self):
  13. pass
  14. def fault_event(self, sender, *args, **kwargs):
  15. self._faulted = True
  16. def restore_event(self, sender, *args, **kwargs):
  17. self._restored = True
  18. def _build_expander_message(self, msg):
  19. msg = ExpanderMessage(msg)
  20. zone = self._zonetracker.expander_to_zone(msg.address, msg.channel)
  21. return zone, msg
  22. def test_zone_fault(self):
  23. zone, msg = self._build_expander_message('!EXP:07,01,01')
  24. self._zonetracker.update(msg)
  25. self.assertEquals(self._zonetracker._zones[zone].status, Zone.FAULT)
  26. self.assertTrue(self._faulted)
  27. def test_zone_restore(self):
  28. zone, msg = self._build_expander_message('!EXP:07,01,01')
  29. self._zonetracker.update(msg)
  30. zone, msg = self._build_expander_message('!EXP:07,01,00')
  31. self._zonetracker.update(msg)
  32. self.assertEquals(self._zonetracker._zones[zone].status, Zone.CLEAR)
  33. self.assertTrue(self._restored)
  34. def test_message_ready(self):
  35. msg = Message('[0000000000000010----],001,[f707000600e5800c0c020000]," "')
  36. self._zonetracker.update(msg)
  37. self.assertEquals(len(self._zonetracker._zones_faulted), 1)
  38. msg = Message('[1000000000000000----],000,[f707000600e5800c0c020000]," "')
  39. self._zonetracker.update(msg)
  40. self.assertEquals(len(self._zonetracker._zones_faulted), 0)
  41. def test_message_fault_text(self):
  42. msg = Message('[0000000000000000----],001,[f707000600e5800c0c020000],"FAULT 1 "')
  43. self._zonetracker.update(msg)
  44. self.assertEquals(len(self._zonetracker._zones_faulted), 1)
  45. def test_ECP_failure(self):
  46. msg = Message('[0000000000000010----],0bf,[f707000600e5800c0c020000],"CHECK 1 "')
  47. self._zonetracker.update(msg)
  48. self.assertEquals(self._zonetracker._zones['1'].status, Zone.CHECK)
  49. def test_zone_restore_skip(self):
  50. panel_messages = [
  51. '[0000000000000000----],001,[f707000600e5800c0c020000],"FAULT 1 "',
  52. '[0000000000000000----],002,[f707000600e5800c0c020000],"FAULT 2 "',
  53. '[0000000000000000----],001,[f707000600e5800c0c020000],"FAULT 1 "',
  54. '[0000000000000000----],001,[f707000600e5800c0c020000],"FAULT 1 "'
  55. ]
  56. for m in panel_messages:
  57. msg = Message(m)
  58. self._zonetracker.update(msg)
  59. self.assertIn(1, self._zonetracker._zones_faulted)
  60. self.assertNotIn(2, self._zonetracker._zones_faulted)
  61. def test_zone_out_of_order_fault(self):
  62. panel_messages = [
  63. '[0000000000000010----],001,[f707000600e5800c0c020000],"FAULT 1 "',
  64. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  65. '[0000000000000010----],003,[f707000600e5800c0c020000],"FAULT 3 "',
  66. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  67. ]
  68. for m in panel_messages:
  69. msg = Message(m)
  70. self._zonetracker.update(msg)
  71. self.assertIn(1, self._zonetracker._zones_faulted)
  72. self.assertIn(3, self._zonetracker._zones_faulted)
  73. self.assertIn(4, self._zonetracker._zones_faulted)
  74. def test_zone_multi_zone_skip_restore(self):
  75. panel_messages = [
  76. '[0000000000000010----],001,[f707000600e5800c0c020000],"FAULT 1 "',
  77. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  78. '[0000000000000010----],002,[f707000600e5800c0c020000],"FAULT 2 "',
  79. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  80. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  81. ]
  82. for m in panel_messages:
  83. msg = Message(m)
  84. self._zonetracker.update(msg)
  85. self.assertNotIn(1, self._zonetracker._zones_faulted)
  86. self.assertNotIn(2, self._zonetracker._zones_faulted)
  87. self.assertIn(4, self._zonetracker._zones_faulted)
  88. def test_zone_timeout_restore(self):
  89. panel_messages = [
  90. '[0000000000000010----],001,[f707000600e5800c0c020000],"FAULT 1 "',
  91. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  92. '[0000000000000010----],002,[f707000600e5800c0c020000],"FAULT 2 "',
  93. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  94. '[0000000000000010----],004,[f707000600e5800c0c020000],"FAULT 4 "',
  95. ]
  96. for m in panel_messages:
  97. msg = Message(m)
  98. self._zonetracker.update(msg)
  99. self.assertIn(4, self._zonetracker._zones_faulted)
  100. self._zonetracker._zones[4].timestamp -= 35 # forcefully expire the zone
  101. # generic message to force an update.
  102. msg = Message('[0000000000000000----],000,[f707000600e5800c0c020000]," "')
  103. self._zonetracker.update(msg)
  104. self.assertNotIn(4, self._zonetracker._zones_faulted)