def create_message(msg_dict): """Create a message from a dictionary.""" address = msg_dict.get("address") flags = int.from_bytes(unhexlify(msg_dict.get("flags")), "big") cmd1 = int.from_bytes(unhexlify(msg_dict.get("cmd1")), "big") cmd2 = int.from_bytes(unhexlify(msg_dict.get("cmd2")), "big") if msg_dict.get("user_data") == "": user_data = None else: user_data = int.from_bytes(unhexlify(msg_dict.get("user_data")), "big") if msg_dict.get("ack") == "": ack = None else: ack = int.from_bytes(unhexlify(msg_dict.get("ack")), "big") if msg_dict.get("target") == "": target = None else: target = msg_dict.get("target") msg = create_std_ext_msg( address, flags, cmd1, cmd2, user_data=user_data, target=target, ack=ack ) return DataItem(msg, 0.02)
async def test_receive_on_msg(self): """Test receiving an ON message.""" topic_lock = asyncio.Lock() async with async_protocol_manager() as protocol: last_topic = None def topic_received(cmd1, cmd2, target, user_data, hops_left, topic=pub.AUTO_TOPIC): """Receive the OFF topic for a device.""" nonlocal last_topic last_topic = topic.name if topic_lock.locked(): topic_lock.release() address = random_address() byte_data = create_std_ext_msg(address, 0x80, 0x11, 0xFF, target=Address("000001")) expected_topic = "{}.{}.on.broadcast".format(address.id, 1) pub.subscribe(topic_received, expected_topic) on_cmd = DataItem(byte_data, 0) data = [on_cmd] await topic_lock.acquire() send_data(data, protocol.read_queue) try: await asyncio.wait_for(topic_lock.acquire(), 2) assert last_topic == expected_topic except asyncio.TimeoutError: assert expected_topic is None if topic_lock.locked(): topic_lock.release()