def test_handle_on_off_manual(self, test_device): with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x00) test_device.handle_broadcast(msg) assert mocked.call_count == 2 calls = [ call(test_device, IM.on_off.Manual.DOWN), call(test_device, False, IM.on_off.Mode.MANUAL, 'device') ] mocked.assert_has_calls(calls) with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x01) test_device.handle_broadcast(msg) assert mocked.call_count == 2 calls = [ call(test_device, IM.on_off.Manual.UP), call(test_device, True, IM.on_off.Mode.MANUAL, 'device') ] mocked.assert_has_calls(calls)
def test_handle_set_msb(self): # tests handle_set_msb and get_next_lsb protocol = MockProto() modem = MockModem() addr = IM.Address(0x01, 0x02, 0x03) device = IM.device.Base(protocol, modem, addr) # Create the new entry at the current last memory location. db_flags = Msg.DbFlags(in_use=True, is_controller=True, is_last_rec=False) i1_entry = IM.db.DeviceEntry(addr, 0x01, device.db.last.mem_loc, db_flags, None, db=device.db) manager = IM.db.DeviceModifyManagerI1(device, device.db, i1_entry) # Test bad MSB, should cause resend of set msb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x28, 0x0E) db_msg = Msg.OutStandard.direct(device.addr, 0x28, 0x0F) manager.handle_set_msb(msg, None) assert protocol.msgs[0].to_bytes() == db_msg.to_bytes() # Test receive correct msb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x28, 0x0F) db_msg = Msg.OutStandard.direct(device.addr, 0x2B, 0xF8) manager.handle_set_msb(msg, None) assert protocol.msgs[1].to_bytes() == db_msg.to_bytes()
def test_finish_write(self): # tests the finished entry in advance_lsb protocol = MockProto() modem = MockModem() addr = IM.Address(0x01, 0x02, 0x03) device = IM.device.Base(protocol, modem, addr) calls = [] def callback(success, msg, data): calls.append(msg) # Create the new entry at the current last memory location. db_flags = Msg.DbFlags(in_use=False, is_controller=True, is_last_rec=False) i1_entry = IM.db.DeviceEntry(addr, 0x01, device.db.last.mem_loc, db_flags, None) manager = IM.db.DeviceModifyManagerI1(device, device.db, i1_entry) # Test received unused from start flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x2B, 0x62) manager.handle_lsb_response(msg, callback) assert calls[0] == "Database entry written" # Test received the last byte manager.record_index = 7 flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x29, 0x00) manager.handle_lsb_response(msg, callback) assert calls[1] == "Database entry written"
def test_engine_version(self, tmpdir): # Tests response to get engine version proto = MockProto() modem = MockModem(tmpdir) calls = [] addr = IM.Address('0a.12.34') device = IM.device.Base(proto, modem, addr) def callback(success, msg, data): calls.append(msg) # i1 test out = Msg.OutStandard.direct(addr, 0x0D, 0x00) handler = IM.handler.StandardCmd(out, device.handle_engine, callback) flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x0D, 0x00) handler._PLM_sent = True handler._PLM_ACK = True r = handler.msg_received(proto, msg) assert r == Msg.FINISHED assert calls[0] == "Operation complete" assert device.db.engine == 0 #i2 msg = Msg.InpStandard(addr, addr, flags, 0x0D, 0x01) r = handler.msg_received(proto, msg) assert device.db.engine == 1 #i2cs msg = Msg.InpStandard(addr, addr, flags, 0x0D, 0x02) r = handler.msg_received(proto, msg) assert device.db.engine == 2
def test_cmd_from_msg(self): # Tests matching the command from the outbound message. proto = MockProto() calls = [] def callback(msg, on_done=None): calls.append(msg) addr = IM.Address('0a.12.34') # sent message, match input command out = Msg.OutStandard.direct(addr, 0x11, 0xff) handler = IM.handler.StandardCmd(out, callback) r = handler.msg_received(proto, "dummy") assert r == Msg.UNKNOWN # ack back. out.is_ack = False r = handler.msg_received(proto, out) assert r == Msg.CONTINUE # wrong cmd out.cmd1 = 0x13 r = handler.msg_received(proto, out) assert r == Msg.UNKNOWN # wrong addr out.cmd1 = 0x11 out.to_addr = IM.Address('0a.12.33') r = handler.msg_received(proto, out) assert r == Msg.UNKNOWN # direct Pre NAK flags = Msg.Flags(Msg.Flags.Type.DIRECT_NAK, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0xFC) r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE # Now pass in the input message. # expected input meesage flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) r = handler.msg_received(proto, msg) assert r == Msg.FINISHED assert len(calls) == 1 assert calls[0] == msg # wrong cmd msg.cmd1 = 0x13 r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN # wrong addr msg.cmd1 = 0x11 msg.from_addr = IM.Address('0a.12.33') r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN
def test_handle_lsb_response(self): # tests handle_lsb_response and write_lsb_byte protocol = MockProto() modem = MockModem() addr = IM.Address(0x01, 0x02, 0x03) device = IM.device.Base(protocol, modem, addr) # Create the new entry at the current last memory location. db_flags = Msg.DbFlags(in_use=True, is_controller=True, is_last_rec=False) i1_entry = IM.db.DeviceEntry(addr, 0x01, device.db.last.mem_loc, db_flags, None, db=device.db) manager = IM.db.DeviceModifyManagerI1(device, device.db, i1_entry) # Test wrong LSB, should cause poke of set lsb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x2B, 0xA2) db_msg = Msg.OutStandard.direct(device.addr, 0x29, 0xE2) manager.handle_lsb_response(msg, None) assert protocol.msgs[0].to_bytes() == db_msg.to_bytes() # Test receive correct lsb, should cause request for next lsb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device.addr, device.addr, flags, 0x29, 0xE2) db_msg = Msg.OutStandard.direct(device.addr, 0x2B, 0xF9) manager.handle_lsb_response(msg, None) assert protocol.msgs[1].to_bytes() == db_msg.to_bytes()
def test_handle_manual_not_load(self, test_device): test_device._load_group = 1 with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x02) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x00) test_device.handle_broadcast(msg) calls = [ call(test_device, manual=IM.on_off.Manual.DOWN, button=2, reason='device') ] assert mocked.call_count == 1 mocked.assert_has_calls(calls, any_order=True) with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x02) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x01) test_device.handle_broadcast(msg) calls = [ call(test_device, manual=IM.on_off.Manual.UP, button=2, reason='device') ] assert mocked.call_count == 1 mocked.assert_has_calls(calls, any_order=True)
def test_set_on_level(self, test_device): # set_on_level(self, level, on_done=None) assert(test_device.get_on_level() == 255) def level_bytes(level): data = bytes([ 0x01, 0x06, level, ] + [0x00] * 11) return data for params in ([1, 0x01], [127, 127], [255, 0xFF]): test_device.set_on_level(on_level=params[0]) assert len(test_device.protocol.sent) == 1 assert test_device.protocol.sent[0].msg.cmd1 == 0x2e assert (test_device.protocol.sent[0].msg.data == level_bytes(params[1])) test_device.protocol.clear() test_device.set_on_level(on_level=64) # Fake having completed the set_on_level(64) request flags = IM.message.Flags(IM.message.Flags.Type.DIRECT_ACK, False) ack = IM.message.InpStandard(test_device.addr.hex, test_device.modem.addr.hex, flags, 0x2e, 0x00) test_device.handle_on_level(ack, IM.util.make_callback(None), 64) assert(test_device.get_on_level() == 64) test_device.protocol.clear() # Try multiple button presses in a row; confirm that level goes to # default on-level then to full brightness, as expected. # Fast-on should always go to full brightness. params = [ (Msg.CmdType.ON, 0x00, {"level":64, "mode":IM.on_off.Mode.NORMAL, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.ON, 0x00, {"level":255, "mode":IM.on_off.Mode.NORMAL, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.ON, 0x00, {"level":64, "mode":IM.on_off.Mode.NORMAL, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.OFF, 0x00, {"level":0, "mode":IM.on_off.Mode.NORMAL, "is_on": False, "reason":'device', "button":1}), (Msg.CmdType.ON_FAST, 0x00, {"level":255, "mode":IM.on_off.Mode.FAST, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.ON_FAST, 0x00, {"level":255, "mode":IM.on_off.Mode.FAST, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.OFF_FAST, 0x00, {"level":0, "mode":IM.on_off.Mode.FAST, "is_on": False, "reason":'device', "button":1}), (Msg.CmdType.ON_INSTANT, 0x00, {"level":64, "mode":IM.on_off.Mode.INSTANT, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.ON_INSTANT, 0x00, {"level":255, "mode":IM.on_off.Mode.INSTANT, "is_on": True, "reason":'device', "button":1}), (Msg.CmdType.ON_INSTANT, 0x00, {"level":64, "mode":IM.on_off.Mode.INSTANT, "is_on": True, "reason":'device', "button":1})] for cmd1, cmd2, expected in params: with mock.patch.object(IM.Signal, 'emit') as mocked: print("Trying:", "[%x, %x]" % (cmd1, cmd2)) flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group_num = 0x01 group = IM.Address(0x00, 0x00, group_num) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, cmd1, cmd2) test_device.handle_broadcast(msg) if expected is not None: mocked.assert_called_once_with(test_device, **expected) else: mocked.assert_not_called()
def test_plm_sent_ack(self, tmpdir): proto = MockProto() modem = MockModem(tmpdir) calls = [] addr = IM.Address('0a.12.34') device = IM.device.Base(proto, modem, addr) def callback(success, msg, data): calls.append(msg) # test PLM_sent flag out = Msg.OutStandard.direct(addr, 0x0D, 0x00) handler = IM.handler.StandardCmd(out, device.handle_engine, callback) flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x0D, 0x00) r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN assert not handler._PLM_sent # Signal Sent handler.sending_message(out) assert handler._PLM_sent # test PLM nak r = handler.msg_received(proto, out) assert r == Msg.CONTINUE assert not handler._PLM_ACK # test PLM ack out.is_ack = True r = handler.msg_received(proto, out) assert r == Msg.CONTINUE assert handler._PLM_ACK
def test_input_cmd(self): # Tests matching the command passed in. proto = MockProto() calls = [] def callback(msg, on_done=None): calls.append(msg) addr = IM.Address('0a.12.34') # sent message, match input command out = Msg.OutStandard.direct(addr, 0x11, 0xff) handler = IM.handler.StandardCmd(out, callback) handler._PLM_sent = True handler._PLM_ACK = True # right cmd r = handler.msg_received(proto, out) assert r == Msg.CONTINUE # wrong cmd out.cmd1 = 0x13 r = handler.msg_received(proto, out) assert r == Msg.UNKNOWN # Now pass in the input message. # expected input meesage flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) r = handler.msg_received(proto, msg) assert r == Msg.FINISHED assert len(calls) == 1 assert calls[0] == msg
def test_broadcast(self, test_device, caplog): # test broadcast Messages, Base doesn't handle any flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, 0x11, 0x00) test_device.handle_broadcast(msg) assert "has no handler for broadcast" in caplog.text
def test_acks(self, tmpdir): proto = MockProto() calls = [] modem = IM.Modem(proto) modem.save_path = str(tmpdir) addr = IM.Address('0a.12.34') handler = IM.handler.Broadcast(modem) r = handler.msg_received(proto, "dummy") assert r == Msg.UNKNOWN flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) # no device r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN device = IM.device.Base(proto, modem, addr, "foo") device.handle_broadcast = calls.append modem.add(device) r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE assert len(calls) == 1 # cleanup should be ignored since prev was processed. flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_CLEANUP, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE assert len(calls) == 1 # If broadcast wasn't found, cleanup should be handled. handler._handled = False r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE assert len(calls) == 2 flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN
def test_handle_refresh_wet(self, test_device): with mock.patch.object(test_device, '_set_is_wet') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x04) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.OFF, 0x11) test_device.handle_refresh(msg) mocked.assert_called_once_with(True)
def test_device_sent_ack(self): # Tests matching the command from the outbound message. proto = MockProto() calls = [] def callback(msg, on_done=None): calls.append(msg) addr = IM.Address('0a.12.34') # sent message, match input command out = Msg.OutStandard.direct(addr, 0x10, 0x00) handler = IM.handler.BroadcastCmdResponse(out, callback) # Signal Sent handler.sending_message(out) assert handler._PLM_sent # test ack sent out.is_ack = True r = handler.msg_received(proto, out) assert r == Msg.CONTINUE assert handler._PLM_ACK # test broadcast before device ack flags = Msg.Flags(Msg.Flags.Type.BROADCAST, False) msg = Msg.InpStandard(addr, addr, flags, 0x01, 0x00) r = handler.msg_received(proto, msg) assert r == Msg.UNKNOWN assert len(calls) == 0 # mock a device ack # expected input meesage flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(addr, addr, flags, 0x10, 0x00) r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE # test a broadcast after device ack flags = Msg.Flags(Msg.Flags.Type.BROADCAST, False) msg = Msg.InpStandard(addr, addr, flags, 0x01, 0x00) r = handler.msg_received(proto, msg) assert r == Msg.FINISHED assert len(calls) == 1 assert calls[0] == msg
def test_handle_broadcast(self, test_device, group_num, cmd1, cmd2, expected, kwargs): with mock.patch.object(IM.Signal, 'emit') as mocked: self._is_wet = False flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, group_num) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, cmd1, cmd2) test_device.handle_broadcast(msg) mocked.assert_called_once_with(test_device, *expected, **kwargs)
def test_handle_on_off(self, test_device, group, cmd1, cmd2, expected): with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, group) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, cmd1, cmd2) test_device.handle_broadcast(msg) if expected is not None: mocked.assert_called_once_with(test_device, **expected) else: mocked.assert_not_called()
def test_broadcast_1(self, test_device, cmd1, expected): with mock.patch.object(Device.BatterySensor, '_set_state') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, cmd1, 0x00) test_device.handle_broadcast(msg) if expected is not None: mocked.assert_called_once_with(**expected) else: mocked.assert_not_called()
def test_handle_manual(self, test_device8): with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x00) test_device8.handle_broadcast(msg) calls = [ call(test_device8, manual=IM.on_off.Manual.DOWN, button=1, reason='device'), call(test_device8, button=1, is_on=False, level=None, reason='device', mode=IM.on_off.Mode.MANUAL) ] mocked.assert_has_calls(calls, any_order=True) with mock.patch.object(IM.Signal, 'emit') as mocked: flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.START_MANUAL_CHANGE, 0x01) test_device8.handle_broadcast(msg) calls = [ call(test_device8, manual=IM.on_off.Manual.UP, button=1, reason='device'), call(test_device8, button=1, is_on=True, level=None, reason='device', mode=IM.on_off.Mode.MANUAL) ] mocked.assert_has_calls(calls, any_order=True)
def test_handle_heartbeat(self, test_device): # tests updating the wet/dry state when heartbeat received with mock.patch.object(IM.Signal, 'emit') as mocked: self._is_wet = False flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x04) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.OFF, 0x00) test_device.handle_broadcast(msg) assert mocked.call_count == 2 calls = [call(test_device, True), call(test_device, True)] mocked.assert_has_calls(calls)
def test_handle_set_msb(self): # tests handle_set_msb device = MockDevice() device_db = IM.db.Device(IM.Address(0x01, 0x02, 0x03)) manager = IM.db.DeviceScanManagerI1(device, device_db) on_done = None manager.msb = 0x0F # Test bad MSB, should cause resend of set msb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device_db.addr, device_db.addr, flags, 0x28, 0x0E) db_msg = Msg.OutStandard.direct(device_db.addr, 0x28, 0x0F) manager.handle_set_msb(msg, on_done) assert device.msgs[0].to_bytes() == db_msg.to_bytes() # Test receive correct msb flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) msg = Msg.InpStandard(device_db.addr, device_db.addr, flags, 0x28, 0x0F) db_msg = Msg.OutStandard.direct(device_db.addr, 0x2B, 0xF8) manager.handle_set_msb(msg, on_done) assert device.msgs[1].to_bytes() == db_msg.to_bytes()
def test_handle_model_bad_resp(self, test_device, caplog): def on_done(success, msg, data): assert not success assert "Operation failed" in msg flags = Msg.Flags(Msg.Flags.Type.BROADCAST, False) to_addr = IM.Address(0x01, 0x30, 0x45) from_addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(from_addr, to_addr, flags, 0x05, 0x00) with caplog.at_level(logging.DEBUG): test_device.handle_model(msg, on_done=on_done) assert 'get_model response with wrong cmd' in caplog.text
def test_nak_str(self): from_addr = IM.Address(0x01, 0x02, 0x03) to_addr = IM.Address(0x03, 0x05, 0x07) # ID not in DB error flags = Msg.Flags(Msg.Flags.Type.DIRECT_NAK, False) obj = Msg.InpStandard(from_addr, to_addr, flags, 0x00, 0xFF) nak_str = obj.nak_str() assert len(nak_str) > 0 # unknow error type flags = Msg.Flags(Msg.Flags.Type.DIRECT_NAK, False) obj = Msg.InpStandard(from_addr, to_addr, flags, 0x00, 0x10) nak_str = obj.nak_str() assert len(nak_str) == 0 # not a nak flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) obj = Msg.InpStandard(from_addr, to_addr, flags, 0x00, 0xFF) nak_str = obj.nak_str() assert len(nak_str) == 0
def test_handle_model(self, test_device, caplog): def on_done(success, msg, data): assert success assert msg == "Operation complete" flags = Msg.Flags(Msg.Flags.Type.BROADCAST, False) to_addr = IM.Address(0x01, 0x30, 0x45) from_addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(from_addr, to_addr, flags, 0x01, 0x00) with caplog.at_level(logging.DEBUG): test_device.handle_model(msg, on_done=on_done) assert 'received model information' in caplog.text assert '2476D' in test_device.db.desc.model
def test_handle_flags(self, test_device, caplog): def on_done(success, msg, data): assert success assert msg == "Operation complete" flags = Msg.Flags(Msg.Flags.Type.DIRECT, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.GET_OPERATING_FLAGS, 0x55) with caplog.at_level(logging.DEBUG): test_device.handle_flags(msg, on_done=on_done) assert 'operating flags: 01010101' in caplog.text
def test_handle_engine_nak(self, test_device, caplog): def on_done(success, msg, data): assert success assert msg == "Operation complete" flags = Msg.Flags(Msg.Flags.Type.DIRECT_NAK, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.GET_ENGINE_VERSION, 0x00) with caplog.at_level(logging.DEBUG): test_device.handle_engine(msg, on_done=on_done) assert 'sent NAK to get engine' in caplog.text assert test_device.db.engine == 2
def test_on_off_ack(self, test_device, load_group, cmd1, cmd2, expected): def on_done(success, *args): pass with mock.patch.object(IM.Signal, 'emit') as mocked: test_device._load_group = load_group flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) from_addr = test_device.addr addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, from_addr, flags, cmd1, cmd2) test_device.handle_ack(msg, on_done) if expected is not None: mocked.assert_called_once_with(test_device, **expected) else: mocked.assert_not_called()
def test_duplicate(self): link = MockSerial() proto = IM.Protocol(link) # test standard input flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) addr = IM.Address('0a.12.33') msg_keep = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) dupe = proto._is_duplicate(msg_keep) assert dupe is False # test dupe with different hops flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False, hops_left=2, max_hops=2) addr = IM.Address('0a.12.33') msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) dupe = proto._is_duplicate(msg) assert dupe is True assert len(proto._read_history) == 1 # not correct message type msg = Msg.InpUserReset() dupe = proto._is_duplicate(msg) assert dupe is False # test deleting an expired message flags = Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False) addr = IM.Address('0a.12.44') msg = Msg.InpStandard(addr, addr, flags, 0x11, 0x01) proto._read_history.append(msg) msg.expire_time = 1 assert len(proto._read_history) == 2 proto._remove_expired_read(time.time()) assert len(proto._read_history) == 1 assert proto._read_history[0] == msg_keep
def test_update_linked_devices(self, test_device, test_entry_1, test_entry_2, test_device_2, caplog): test_device.db.add_entry(test_entry_1) test_device.db.add_entry(test_entry_2) test_device.modem.add(test_device_2) test_device.db_config = IM.db.Device(test_device.addr, None, test_device) flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x01) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, 0x11, 0x00) with caplog.at_level(logging.DEBUG): test_device.update_linked_devices(msg) assert 'device 12.34.ab is not in config' in caplog.text assert 'Device 56.78.cd ignoring group cmd' in caplog.text
def test_handle_broadcast_clear(self, test_device): with mock.patch.object(IM.Signal, 'emit') as mocked: self._is_wet = False flags = Msg.Flags(Msg.Flags.Type.ALL_LINK_BROADCAST, False) group = IM.Address(0x00, 0x00, 0x05) addr = IM.Address(0x01, 0x02, 0x03) msg = Msg.InpStandard(addr, group, flags, Msg.CmdType.ON, 0x00) test_device.handle_broadcast(msg) assert mocked.call_count == 6 calls = [] for type in test_device.Type: if type == test_device.Type.CLEAR: continue calls.append(call(test_device, type, False)) mocked.assert_has_calls(calls)
def test_acks(self): proto = None calls = [] def callback(success, msg, value): calls.append(msg) addr = IM.Address('0a.12.34') db = Mockdb(addr) handler = IM.handler.DeviceDbGet(db, callback) handler._PLM_sent = True handler._PLM_ACK = True # Normal nak std_ack = Msg.OutStandard.direct(addr, 0x2f, 0x00) std_ack.is_ack = False r = handler.msg_received(proto, std_ack) assert r == Msg.CONTINUE # Wrong address nomatch = Msg.OutStandard.direct(IM.Address('0a.12.35'), 0x2f, 0x00) std_ack.is_ack = True r = handler.msg_received(proto, nomatch) assert r == Msg.UNKNOWN # Wrong command std_ack.cmd1 = 0x11 r = handler.msg_received(proto, std_ack) assert r == Msg.UNKNOWN # direct Pre NAK flags = Msg.Flags(Msg.Flags.Type.DIRECT_NAK, False) msg = Msg.InpStandard(addr, addr, flags, 0x2f, 0xFC) r = handler.msg_received(proto, msg) assert r == Msg.CONTINUE # Try w/ an extended msg. ext_data = bytes(14) ext_ack = Msg.OutExtended.direct(addr, 0x2f, 0x00, ext_data) ext_ack.is_ack = True r = handler.msg_received(proto, ext_ack) assert r == Msg.CONTINUE r = handler.msg_received(proto, "dummy") assert r == Msg.UNKNOWN assert calls == []