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
Beispiel #2
0
def test_getIMInfo():
    """Test GetIMInfo."""
    addr = bytearray([0x11, 0x22, 0x33])
    cat = 0x44
    subcat = 0x55
    firmware = 0x66
    ack = 0x06
    nak = 0x15

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

    msg = GetImInfo(addr, cat, subcat, firmware, ack)
    assert msg.hex == hexmsg(0x02, 0x60, addr, cat, subcat, firmware, ack)
    assert msg.isack
    assert not msg.isnak
    assert len(msg.hex) / 2 == msg.receivedSize

    msg = GetImInfo(addr, cat, subcat, firmware, nak)
    assert msg.hex == hexmsg(0x02, 0x60, addr, cat, subcat, firmware, nak)
    assert not msg.isack
    assert msg.isnak
    assert len(msg.hex) / 2 == msg.receivedSize
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
0
async def do_plm_x10(loop):
    """Asyncio coroutine to test the PLM X10 message handling."""
    _LOGGER.setLevel(logging.DEBUG)
    _LOGGER.info('Connecting to Insteon PLM')

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

    cb = MockCallbacks()

    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)

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

    _LOGGER.info('Add X10 device and receive messages')
    _LOGGER.info('____________________________________')
    housecode = 'C'
    unitcode = 9
    plm.add_x10_device(housecode, unitcode, 'OnOff')
    x10_id = Address.x10(housecode, unitcode).id
    plm.devices[x10_id].states[0x01].register_updates(cb.callbackmethod1)

    msg = X10Received.unit_code_msg(housecode, unitcode)
    plm.data_received(msg.bytes)
    await asyncio.sleep(.1, loop=loop)
    msg = X10Received.command_msg(housecode, X10_COMMAND_ON)
    plm.data_received(msg.bytes)
    await asyncio.sleep(.1, loop=loop)
    assert cb.callbackvalue1 == 0xff

    msg = X10Received.unit_code_msg(housecode, unitcode)
    plm.data_received(msg.bytes)
    await asyncio.sleep(.1, loop=loop)
    msg = X10Received.command_msg(housecode, X10_COMMAND_OFF)
    plm.data_received(msg.bytes)
    await asyncio.sleep(.1, loop=loop)
    assert cb.callbackvalue1 == 0x00

    _LOGGER.info('Close PLM')
    _LOGGER.info('_________')

    await plm.close()
    _LOGGER.error('PLM closed in test_x10')
    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)
Beispiel #7
0
 def _get_plm_info(self):
     """Request PLM Info."""
     _LOGGER.info('Requesting Insteon Modem Info')
     msg = GetImInfo()
     self.send_msg(msg, wait_nak=True, wait_timeout=.5)
Beispiel #8
0
 def _get_plm_info(self):
     """Request PLM Info."""
     self.log.info('Requesting PLM Info')
     msg = GetImInfo()
     self.send_msg(msg, wait_nak=True, wait_timeout=.5)