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()
Example #3
0
    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_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_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()
Example #6
0
    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
Example #7
0
    def test_eq(self):
        msg1 = Msg.Flags(Msg.Flags.Type.BROADCAST, False)

        msg2 = Msg.Flags(Msg.Flags.Type.BROADCAST, False, 2, 3)
        assert msg1 == msg2

        msg3 = Msg.Flags(Msg.Flags.Type.BROADCAST, False, 3, 2)
        assert msg1 == msg3

        msg4 = Msg.Flags(Msg.Flags.Type.BROADCAST, True)
        assert msg1 != msg4

        msg5 = Msg.Flags(Msg.Flags.Type.DIRECT, False)
        assert msg1 != msg5
Example #8
0
    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()
Example #9
0
    def test_recs(self):
        proto = None
        calls = []

        addr = IM.Address('0a.12.34')
        handler = IM.handler.DeviceGetDb(addr, calls.append)

        flags = Msg.Flags(Msg.Flags.Type.DIRECT, True)
        data = bytes([0x01, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0, 0, 0])
        msg = Msg.InpExtended(addr, addr, flags, 0x2f, 0x00, data)

        r = handler.msg_received(proto, msg)
        assert r == Msg.CONTINUE
        assert len(calls) == 1
        assert calls[0] == msg

        msg.data = bytes(14)
        r = handler.msg_received(proto, msg)
        assert r == Msg.FINISHED
        assert len(calls) == 2
        assert calls[1] is None

        # no match
        msg.cmd1 = 0x00
        r = handler.msg_received(proto, msg)
        assert r == Msg.UNKNOWN
Example #10
0
    def test_recs(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)

        flags = Msg.Flags(Msg.Flags.Type.DIRECT, True)
        data = bytes([0x01, 0, 0, 0, 0, 0xFF, 0, 0x01, 0, 0, 0, 0, 0, 0])
        msg = Msg.InpExtended(addr, addr, flags, 0x2f, 0x00, data)

        r = handler.msg_received(proto, msg)
        assert r == Msg.CONTINUE
        assert len(calls) == 0

        msg.data = bytes(14)
        r = handler.msg_received(proto, msg)
        assert r == Msg.FINISHED
        assert len(calls) == 1
        assert calls[0] == "Database received"

        # no match
        msg.cmd1 = 0x00
        r = handler.msg_received(proto, msg)
        assert r == Msg.UNKNOWN
Example #11
0
    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
Example #12
0
    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
Example #13
0
    def test_handle_ext_flags(self, test_device):
        # Captured data from my own motion detector
        # 00 01 03 00 ff 0e 00 ff 0e 01 18 4f 00 d2
        # set as a 2842 model
        test_device.db.set_info(0x10, 0x01, 0x00)
        modem_addr = IM.Address(0x01, 0xAA, 0xFF)
        flags = Msg.Flags(Msg.Flags.Type.DIRECT, True)
        msg = Msg.InpExtended(
            test_device.addr, modem_addr, flags, 0x2e, 0x00,
            bytes([
                0x00, 0x01, 0x03, 0x00, 0xff, 0x0e, 0x00, 0xff, 0x0e, 0x01,
                0x18, 0x4f, 0x00, 0xd2
            ]))

        def on_done(success, *args):
            assert success

        with mock.patch.object(IM.Signal, 'emit') as mocked:
            test_device.handle_ext_flags(msg, on_done)
            assert test_device.led_on
            assert test_device.night_only
            assert test_device.on_only
            assert test_device.battery_voltage_time > 0
            assert mocked.call_count == 1
            # the emit call should be false
            assert not mocked.call_args.args[1]
Example #14
0
    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
Example #15
0
 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_handle_refresh_state(self, test_device):
     # handle_refresh_state(self, msg, on_done):
     def on_done(success, *args):
         assert success == True
     msg = Msg.OutStandard(IM.Address(12,14,15),
                           Msg.Flags(Msg.Flags.Type.DIRECT_ACK, False),
                           0x2e, 0x00, is_ack=True)
     test_device.handle_refresh_state(msg, on_done)
Example #17
0
    def test_hops(self):
        obj = Msg.Flags(Msg.Flags.Type.DIRECT, False, 2, 3)
        assert obj.hops_left == 2
        assert obj.max_hops == 3

        obj.set_hops(1)
        assert obj.hops_left == 1
        assert obj.max_hops == 1
Example #18
0
 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)
Example #19
0
    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_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
Example #21
0
 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)
Example #22
0
 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()
Example #23
0
 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()
Example #24
0
 def test_handle_ext_flags(self, test_device):
     from_addr = IM.Address(0x01, 0x02, 0x05)
     flags = Msg.Flags(Msg.Flags.Type.DIRECT, True)
     data = bytes([0x01, 0x01, 0x00, 0x00, 0x20, 0x20, 0x1c, 0x1c, 0x1f,
                   0x00, 0x01, 0x00, 0x00, 0x00])
     msg = Msg.InpExtended(from_addr, test_device.addr, flags,
                           Msg.CmdType.EXTENDED_SET_GET, 0x00, data)
     def on_done(success, *args):
         assert success
     test_device.handle_ext_flags(msg, on_done)
     assert test_device.get_on_level() == 0x1C
Example #25
0
 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)
Example #26
0
    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
Example #27
0
 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)
Example #28
0
    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_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
Example #30
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