def test_misc_messages():
    """Test misc messages."""
    callbacks = MessageCallback()
    callbacktest1 = "test callback 1"
    callbacktest2 = "test callback 2"
    callbacktest3 = "test callback 3"

    msgtemplate1 = AllLinkRecordResponse(None, None, None, None, None, None)
    msgtemplate2 = GetImInfo()
    msgtemplate3 = GetNextAllLinkRecord(acknak=MESSAGE_NAK)
    callbacks.add(msgtemplate1, callbacktest1)
    callbacks.add(msgtemplate2, callbacktest2)
    callbacks.add(msgtemplate3, callbacktest3)

    msg1 = AllLinkRecordResponse(0x00, 0x01, '1a2b3c', 0x01, 0x02, 0x03)
    msg2 = GetImInfo()
    msg3 = GetNextAllLinkRecord(acknak=MESSAGE_ACK)
    msg4 = GetNextAllLinkRecord(acknak=MESSAGE_NAK)

    callback_list1 = callbacks.get_callbacks_from_message(msg1)
    callback_list2 = callbacks.get_callbacks_from_message(msg2)
    callback_list3 = callbacks.get_callbacks_from_message(msg3)
    callback_list4 = callbacks.get_callbacks_from_message(msg4)

    assert callback_list1[0] == callbacktest1
    assert callback_list2[0] == callbacktest2
    assert not callback_list3
    assert callback_list4[0] == callbacktest3
Esempio n. 2
0
    def _register_message_handlers(self):
        template_all_link_response = AllLinkRecordResponse(
            None, None, None, None, None, None)
        template_get_im_info = GetImInfo()
        template_next_all_link_rec = GetNextAllLinkRecord(acknak=MESSAGE_NAK)
        template_x10_send = X10Send(None, None, MESSAGE_ACK)
        template_x10_received = X10Received(None, None)

        # self._message_callbacks.add(
        #    template_assign_all_link,
        #    self._handle_assign_to_all_link_group)

        self._message_callbacks.add(template_all_link_response,
                                    self._handle_all_link_record_response)

        self._message_callbacks.add(template_get_im_info,
                                    self._handle_get_plm_info)

        self._message_callbacks.add(template_next_all_link_rec,
                                    self._handle_get_next_all_link_record_nak)

        self._message_callbacks.add(template_x10_send,
                                    self._handle_x10_send_receive)

        self._message_callbacks.add(template_x10_received,
                                    self._handle_x10_send_receive)
Esempio n. 3
0
 def _get_next_all_link_record(self):
     """Request next ALL-Link record."""
     _LOGGER.debug("Starting: _get_next_all_link_record")
     _LOGGER.debug("Requesting Next All-Link Record")
     msg = GetNextAllLinkRecord()
     self.send_msg(msg, wait_nak=True, wait_timeout=.5)
     _LOGGER.debug("Ending: _get_next_all_link_record")
Esempio n. 4
0
def test_getNextAllLinkRecord():
    """Test GetNextAllLinkRecord."""
    ack = 0x06
    nak = 0x15

    msg = GetNextAllLinkRecord()
    assert msg.hex == hexmsg(0x02, 0x6a)
    assert not msg.isack
    assert not msg.isnak
    assert len(msg.hex) / 2 == msg.sendSize

    msg = GetNextAllLinkRecord(ack)
    assert msg.hex == hexmsg(0x02, 0x6a, ack)
    assert msg.isack
    assert not msg.isnak
    assert len(msg.hex) / 2 == msg.receivedSize

    msg = GetNextAllLinkRecord(nak)
    assert msg.hex == hexmsg(0x02, 0x6a, nak)
    assert not msg.isack
    assert msg.isnak
    assert len(msg.hex) / 2 == msg.receivedSize
Esempio n. 5
0
    def _register_message_handlers(self):
        template_assign_all_link = StandardReceive.template(
            commandtuple=COMMAND_ASSIGN_TO_ALL_LINK_GROUP_0X01_NONE)
        template_all_link_response = AllLinkRecordResponse(None, None, None,
                                                           None, None, None)
        template_get_im_info = GetImInfo()
        template_next_all_link_rec = GetNextAllLinkRecord(acknak=MESSAGE_NAK)
        template_all_link_complete = AllLinkComplete(None, None, None,
                                                     None, None, None)
        template_x10_send = X10Send(None, None, MESSAGE_ACK)
        template_x10_received = X10Received(None, None)

        self._message_callbacks.add(
            template_assign_all_link,
            self._handle_assign_to_all_link_group)

        self._message_callbacks.add(
            template_all_link_response,
            self._handle_all_link_record_response)

        self._message_callbacks.add(
            template_get_im_info,
            self._handle_get_plm_info)

        self._message_callbacks.add(
            template_next_all_link_rec,
            self._handle_get_next_all_link_record_nak)

        self._message_callbacks.add(
            template_all_link_complete,
            self._handle_assign_to_all_link_group)

        self._message_callbacks.add(
            template_x10_send,
            self._handle_x10_send_receive)

        self._message_callbacks.add(
            template_x10_received,
            self._handle_x10_send_receive)
Esempio n. 6
0
async def do_plm(loop):
    """Asyncio coroutine to test the PLM."""
    _LOGGER.info('Connecting to Insteon PLM')

    # pylint: disable=not-an-iterable
    conn = await MockConnection.create(loop=loop)

    def async_insteonplm_light_callback(device):
        """Log that our new device callback worked."""
        _LOGGER.warning('New Device: %s %02x %02x %s, %s', device.id,
                        device.cat, device.subcat, device.description,
                        device.model)

    # pylint: disable=unused-variable
    def async_light_on_level_callback(device_id, state, value):
        """Callback to catch changes to light value."""
        _LOGGER.info('Device %s state %s value is changed to %02x', device_id,
                     state, value)

    conn.protocol.add_device_callback(async_insteonplm_light_callback)

    plm = conn.protocol
    plm.connection_made(conn.transport)

    _LOGGER.info('Replying with IM Info')
    _LOGGER.info('_____________________')
    cmd_sent = await wait_for_plm_command(plm, GetImInfo(), loop)
    if not cmd_sent:
        assert False
    msg = insteonplm.messages.getIMInfo.GetImInfo(address='1a2b3c',
                                                  cat=0x03,
                                                  subcat=0x20,
                                                  firmware=0x00,
                                                  acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    assert plm.address == Address('1a2b3c')
    assert plm.cat == 0x03
    assert plm.subcat == 0x20
    assert plm.product_key == 0x00

    _LOGGER.info('Replying with an All-Link Record')
    _LOGGER.info('________________________________')
    cmd_sent = await wait_for_plm_command(plm, GetFirstAllLinkRecord(), loop)
    if not cmd_sent:
        assert False
    msg = GetFirstAllLinkRecord(MESSAGE_ACK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)

    msg = AllLinkRecordResponse(flags=0x00,
                                group=0x01,
                                address='4d5e6f',
                                linkdata1=0x01,
                                linkdata2=0x0b,
                                linkdata3=0x000050)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT)

    _LOGGER.info('Replying with Last All-Link Record')
    _LOGGER.info('__________________________________')
    cmd_sent = await wait_for_plm_command(plm, GetNextAllLinkRecord(), loop)
    if not cmd_sent:
        assert False
    msg = GetNextAllLinkRecord(MESSAGE_NAK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    msg = GetNextAllLinkRecord(MESSAGE_NAK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    msg = GetNextAllLinkRecord(MESSAGE_NAK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    msg = GetNextAllLinkRecord(MESSAGE_NAK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)

    _LOGGER.info('Replying with Device Info Record')
    _LOGGER.info('________________________________')
    msg = StandardSend('4d5e6f', COMMAND_ID_REQUEST_0X10_0X00)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False
    msg = StandardSend('4d5e6f',
                       COMMAND_ID_REQUEST_0X10_0X00,
                       acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    msg = StandardReceive(address='4d5e6f',
                          target='010b00',
                          commandtuple={
                              'cmd1': 0x01,
                              'cmd2': 0x00
                          },
                          flags=MESSAGE_FLAG_BROADCAST_0X80)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)
    for addr in plm.devices:
        _LOGGER.info('Device: %s', addr)

    _LOGGER.info('Replying with Device Status Record')
    _LOGGER.info('__________________________________')
    msg = StandardSend('4d5e6f', COMMAND_LIGHT_STATUS_REQUEST_0X19_0X00)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False
    msg = StandardSend('4d5e6f',
                       COMMAND_LIGHT_STATUS_REQUEST_0X19_0X00,
                       acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)

    msg = insteonplm.messages.standardReceive.StandardReceive(address='4d5e6f',
                                                              target='1a2b3c',
                                                              commandtuple={
                                                                  'cmd1': 0x17,
                                                                  'cmd2': 0xaa
                                                              },
                                                              flags=0x20)
    plm.data_received(msg.bytes)
    await asyncio.sleep(RECV_MSG_WAIT, loop=loop)

    assert plm.devices['4d5e6f'].states[0x01].value == 0xaa

    _LOGGER.info('Testing device message ACK/NAK handling')
    _LOGGER.info('_______________________________________')
    plm.devices['4d5e6f'].states[0x01].on()
    plm.devices['4d5e6f'].states[0x01].off()
    plm.devices['4d5e6f'].states[0x01].set_level(0xbb)

    # Test that the first ON command is sent
    msg = StandardSend('4d5e6f', COMMAND_LIGHT_ON_0X11_NONE, cmd2=0xff)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False

    # ACK the ON command
    msg = StandardSend('4d5e6f',
                       COMMAND_LIGHT_ON_0X11_NONE,
                       cmd2=0xff,
                       flags=0x00,
                       acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)

    # Let the Direct ACK timeout expire
    await asyncio.sleep(DIRECT_ACK_WAIT_TIMEOUT, loop=loop)

    # Test that the Off command has now sent
    msg = StandardSend('4d5e6f', COMMAND_LIGHT_OFF_0X13_0X00)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False

    # NAK the OFF command and test that the OFF command is resent
    plm.transport.lastmessage = ''
    msg = StandardSend('4d5e6f',
                       COMMAND_LIGHT_OFF_0X13_0X00,
                       acknak=MESSAGE_NAK)
    plm.data_received(msg.bytes)

    msg = StandardSend('4d5e6f', COMMAND_LIGHT_OFF_0X13_0X00)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False

    # Let the ACK/NAK timeout expire and test that the OFF command is resent
    plm.transport.lastmessage = ''
    msg = StandardSend('4d5e6f', COMMAND_LIGHT_OFF_0X13_0X00)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False

    # ACK the OFF command and let the Direct ACK message expire
    msg = StandardSend('4d5e6f',
                       COMMAND_LIGHT_OFF_0X13_0X00,
                       acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)

    # Direct ACK of off message
    msg = StandardReceive(address='4d5e6f',
                          target='010b00',
                          commandtuple=COMMAND_LIGHT_OFF_0X13_0X00,
                          flags=MESSAGE_FLAG_DIRECT_MESSAGE_ACK_0X20)
    plm.data_received(msg.bytes)

    # Test that the second SET_LEVEL command is sent
    msg = StandardSend('4d5e6f', COMMAND_LIGHT_ON_0X11_NONE, cmd2=0xbb)
    cmd_sent = await wait_for_plm_command(plm, msg, loop)
    if not cmd_sent:
        assert False

    # ACK the SET_LEVEL command and let the Direct ACK message expire
    msg = StandardSend('4d5e6f',
                       COMMAND_LIGHT_ON_0X11_NONE,
                       cmd2=0xbb,
                       acknak=MESSAGE_ACK)
    plm.data_received(msg.bytes)

    msg = StandardReceive(address='4d5e6f',
                          target='010b00',
                          commandtuple=COMMAND_LIGHT_ON_0X11_NONE,
                          cmd2=0xbb,
                          flags=MESSAGE_FLAG_DIRECT_MESSAGE_ACK_0X20)
    plm.data_received(msg.bytes)

    await plm.close()
    _LOGGER.error('PLM closed in test_plm')
    await asyncio.sleep(.1, loop=loop)
    open_tasks = asyncio.Task.all_tasks(loop=loop)
    for task in open_tasks:
        if hasattr(task, 'name'):
            _LOGGER.error('Device: %s Task: %s', task.name, task)
        else:
            _LOGGER.error('Task: %s', task)