async def test_message_to_topic(self): """Test converting a message to a topic.""" async with async_protocol_manager() as protocol: tests = await import_commands() for test_info in tests: self._topic = None address = repr(random_address()) curr_test = tests[test_info] if curr_test.get("address") is not None: curr_test["address"] = address msgs = [create_message(curr_test)] curr_topic = curr_test["topic"].format(address) subscribe_topic(self.capture_topic, curr_topic) send_data(msgs, protocol.read_queue) try: await asyncio.sleep(0.1) assert self._topic.name == curr_topic except asyncio.TimeoutError: raise AssertionError( "Failed timed out {} with test topic {}".format( test_info, curr_test.get("topic") ) ) except (AssertionError, AttributeError): raise AssertionError( "Failed test {} with test topic {}".format( test_info, curr_test.get("topic") ) ) finally: unsubscribe_topic(self.capture_topic, curr_topic)
async def test_inbound(self): """Test direct command.""" async with async_protocol_manager(auto_ack=False) as protocol: tests = await import_commands() subscribe_topic(self.async_validate_values, pub.ALL_TOPICS) for test_info in tests: self._current_test = test_info test_command = tests[test_info] cmd_class = test_command.get("command") params = test_command.get("params") inbound_message = test_command.get("message") if params.get("address") == "": params["address"] = random_address() inbound_message["address"] = params["address"] self._assert_tests = test_command.get("assert_tests") obj = get_class_or_method(commands, cmd_class) cmd = obj(**params) inbound_data_item = create_message(inbound_message, 0) self._call_count = 0 send_data([inbound_data_item], protocol.read_queue) await sleep(0.1) assert self._call_count == 1 assert self._assert_result
def setUp(self): """Set up the test.""" self.call_count = 0 set_log_levels( logger="info", logger_pyinsteon="info", logger_messages="info", logger_topics=False, ) subscribe_topic(self.handle_topics, pub.ALL_TOPICS)
async def test_command(self): """Test direct command.""" async with async_protocol_manager() as protocol: msgs = [] def listen_for_ack(): send_data(msgs, protocol.read_queue) tests = await import_commands() subscribe_topic(self.validate_values, pub.ALL_TOPICS) subscribe_topic(listen_for_ack, "ack") for test_info in tests: address = random_address() self._current_test = test_info test_command = tests[test_info] command = test_command.get("command") cmd_class = command.get("class") params = command.get("params") if params.get("address"): params["address"] = address send_params = command.get("send_params") test_response = test_command.get("response") obj = get_class_or_method(commands, cmd_class) cmd = obj(**params) messages = test_command.get("messages") msgs = [] for msg_dict in messages: #msg_dict = messages[message] msg_dict["address"] = address msgs.append(create_message(msg_dict)) self._assert_tests = test_command.get("assert_tests") self._call_count = 0 try: response = await cmd.async_send(**send_params) except Exception as ex: raise Exception("Failed test {} with error: {}".format( self._current_test, str(ex))) try: if test_response: assert int(response) == test_response if self._assert_tests: call_count = test_command.get("call_count", 1) assert self._call_count == call_count except AssertionError: raise AssertionError( "Failed test: {} command response: {} call count {}". format(self._current_test, response, self._call_count)) await sleep(0.1) assert self._assert_result
async def websocket_notify_on_aldb_status( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Tell Insteon a new ALDB record was added.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return @callback def record_added(controller, responder, group): """Forward ALDB events to websocket.""" forward_data = {"type": "record_loaded"} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def aldb_loaded(): """Forward ALDB loaded event to websocket.""" forward_data = { "type": "status_changed", "is_loading": device.aldb.status == ALDBStatus.LOADING, } connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def async_cleanup() -> None: """Remove signal listeners.""" unsubscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") unsubscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") unsubscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") forward_data = {"type": "unsubscribed"} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) connection.subscriptions[msg["id"]] = async_cleanup subscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") subscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") subscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") connection.send_result(msg[ID])
def aldb_loaded(): """Forward ALDB loaded event to websocket.""" forward_data = { "type": "status_changed", "is_loading": device.aldb.status == ALDBStatus.LOADING, } connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def async_cleanup() -> None: """Remove signal listeners.""" unsubscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") unsubscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") unsubscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") forward_data = {"type": "unsubscribed"} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) connection.subscriptions[msg["id"]] = async_cleanup subscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") subscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") subscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") connection.send_result(msg[ID])
def subscribe(self, listener, force_strong_ref=False): """Mock the subscribe function.""" subscribe_topic(listener, DEVICE_LIST_CHANGED)
def subscribe(self, listener): """Mock the subscribe function.""" subscribe_topic(listener, DEVICE_LIST_CHANGED)