|
| 1 | +#=========================================================================== |
| 2 | +# |
| 3 | +# Tests for: insteont_mqtt/db/DeviceScanManagerI2.py |
| 4 | +# |
| 5 | +#=========================================================================== |
| 6 | +import pytest |
| 7 | +import insteon_mqtt as IM |
| 8 | +import insteon_mqtt.message as Msg |
| 9 | +from insteon_mqtt.db.Device import START_MEM_LOC |
| 10 | + |
| 11 | +class Test_DeviceScanManagerI2: |
| 12 | + def test_start_scan(self): |
| 13 | + dev_addr = IM.Address('0a.12.34') |
| 14 | + device = MockDevice(dev_addr, 0) |
| 15 | + manager = IM.db.DeviceScanManagerI2(device, device.db) |
| 16 | + |
| 17 | + first_mem_addr = START_MEM_LOC |
| 18 | + data = bytes([ |
| 19 | + 0x00, |
| 20 | + 0x00, # ALDB record request |
| 21 | + first_mem_addr >> 8, # Address MSB |
| 22 | + first_mem_addr & 0xff, # Address LSB |
| 23 | + 0x01, # Read one record |
| 24 | + ] + [0x00] * 9) |
| 25 | + db_msg = Msg.OutExtended.direct(dev_addr, 0x2f, 0x00, data) |
| 26 | + |
| 27 | + manager.start_scan() |
| 28 | + assert device.sent[0].to_bytes() == db_msg.to_bytes() |
| 29 | + |
| 30 | + #------------------------------------------------------------------- |
| 31 | + @pytest.mark.parametrize("init_db,mem_addr,recv,exp_calls,next_addr", [ |
| 32 | + # Have all but last record. Receive it, making DB complete. |
| 33 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 34 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 35 | + [ 0x00, 0x00, 0x0f, 0xf7, 0xff, |
| 36 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 37 | + [ 0x00, 0x00, 0x0f, 0xef, 0xff, |
| 38 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 39 | + None, |
| 40 | + [ 0x00, 0x01, 0x0f, 0xe7, 0xff, |
| 41 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 42 | + [ "Database received" ], |
| 43 | + None ), |
| 44 | + # Have all but third record. Receive it, making DB complete. |
| 45 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 46 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 47 | + [ 0x00, 0x00, 0x0f, 0xf7, 0xff, |
| 48 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 49 | + [ 0x00, 0x00, 0x0f, 0xe7, 0xff, |
| 50 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 51 | + None, |
| 52 | + [ 0x00, 0x01, 0x0f, 0xef, 0xff, |
| 53 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 54 | + [ "Database received" ], |
| 55 | + None ), |
| 56 | + # Have first two records. Receive third and request fourth. |
| 57 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 58 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 59 | + [ 0x00, 0x00, 0x0f, 0xef, 0xff, |
| 60 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 61 | + None, |
| 62 | + [ 0x00, 0x01, 0x0f, 0xf7, 0xff, |
| 63 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 64 | + [ ], |
| 65 | + 0xfe7 ), |
| 66 | + # Have first two records. Receive last. DB still incomplete. |
| 67 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 68 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 69 | + [ 0x00, 0x00, 0x0f, 0xf7, 0xff, |
| 70 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 71 | + None, |
| 72 | + [ 0x00, 0x01, 0x0f, 0xe7, 0xff, |
| 73 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 74 | + [ "Database incomplete" ], |
| 75 | + None ), |
| 76 | + # Have first and last records. Receive third. DB still incomplete. |
| 77 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 78 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 79 | + [ 0x00, 0x00, 0x0f, 0xe7, 0xff, |
| 80 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 81 | + None, |
| 82 | + [ 0x00, 0x01, 0x0f, 0xef, 0xff, |
| 83 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 84 | + [ "Database incomplete" ], |
| 85 | + None ), |
| 86 | + # Have first and last records. Receive second and request third. |
| 87 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 88 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 89 | + [ 0x00, 0x00, 0x0f, 0xe7, 0xff, |
| 90 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 91 | + None, |
| 92 | + [ 0x00, 0x01, 0x0f, 0xf7, 0xff, |
| 93 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 94 | + [ ], |
| 95 | + 0xfef ), |
| 96 | + # Have first and last records. Expecting second, but receive third. |
| 97 | + # Request second. |
| 98 | + ( [ [ 0x00, 0x00, 0x0f, 0xff, 0xff, |
| 99 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 100 | + [ 0x00, 0x00, 0x0f, 0xe7, 0xff, |
| 101 | + 0x00, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ] ], |
| 102 | + 0xff7, |
| 103 | + [ 0x00, 0x01, 0x0f, 0xef, 0xff, |
| 104 | + 0xff, 0x01, 0x0a, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00 ], |
| 105 | + [ ], |
| 106 | + 0xff7 ) |
| 107 | + ]) |
| 108 | + def test_handle_record(self, init_db, mem_addr, recv, exp_calls, |
| 109 | + next_addr): |
| 110 | + calls = [] |
| 111 | + |
| 112 | + def callback(success, msg, value): |
| 113 | + calls.append(msg) |
| 114 | + |
| 115 | + modem_addr = IM.Address('09.12.34') |
| 116 | + dev_addr = IM.Address('0a.12.34') |
| 117 | + device = MockDevice(dev_addr, 0) |
| 118 | + if mem_addr is None: |
| 119 | + # Expecting the same address that we're about to receive |
| 120 | + mem_addr = (recv[2] << 8) + recv[3] |
| 121 | + manager = IM.db.DeviceScanManagerI2(device, device.db, callback, |
| 122 | + mem_addr=mem_addr) |
| 123 | + |
| 124 | + # Initialize DB starting state |
| 125 | + for data in init_db: |
| 126 | + entry = IM.db.DeviceEntry.from_bytes(data, db=device.db) |
| 127 | + device.db.add_entry(entry) |
| 128 | + |
| 129 | + # Handle received message, check callbacks |
| 130 | + flags = Msg.Flags(Msg.Flags.Type.DIRECT, True) |
| 131 | + msg = Msg.InpExtended(dev_addr, modem_addr, flags, 0x2f, 0x00, recv) |
| 132 | + manager.handle_record(msg, callback) |
| 133 | + assert len(calls) == len(exp_calls) |
| 134 | + for idx, call in enumerate(exp_calls): |
| 135 | + assert calls[idx] == call |
| 136 | + |
| 137 | + # Check address of next requested entry (if any expected) |
| 138 | + if next_addr is not None: |
| 139 | + assert len(device.sent) == 1 |
| 140 | + sent_data = device.sent[0].data |
| 141 | + requested_addr = (sent_data[2] << 8) + sent_data[3] |
| 142 | + assert requested_addr == next_addr |
| 143 | + |
| 144 | +#=========================================================================== |
| 145 | +class MockDevice: |
| 146 | + """Mock insteon_mqtt/Device class |
| 147 | + """ |
| 148 | + def __init__(self, addr, db_delta): |
| 149 | + self.sent = [] |
| 150 | + self.addr = addr |
| 151 | + self.db = IM.db.Device(addr, None, self) |
| 152 | + self.db.delta = db_delta |
| 153 | + |
| 154 | + def send(self, msg, handler, priority=None, after=None): |
| 155 | + self.sent.append(msg) |
0 commit comments