async def test_integ_configs(mocker: MockerFixture, config_file: str, data: Dict[str, Any]): entity_state_attributes = data.get("entity_state_attributes", {}) entity_state = data.get("entity_state", None) fired_actions = data.get("fired_actions", []) render_template_response = data.get("render_template_response") extra = data.get("extra") expected_calls = data.get("expected_calls", []) expected_calls_count = data.get("expected_calls_count", len(expected_calls)) config = read_config_yaml(config_file) controller = get_controller(config["module"], config["class"]) if controller is None: raise ValueError( f"`{config['class']}` class controller does not exist") controller.args = config if render_template_response is not None: mocker.patch.object(controller, "_render_template", return_value=render_template_response) if isinstance(controller, TypeController): fake_entity_states = get_fake_entity_states(entity_state, entity_state_attributes) mocker.patch.object(controller, "get_entity_state", fake_entity_states) call_service_stub = mocker.patch.object(Hass, "call_service") await controller.initialize() for idx, action in enumerate(fired_actions): if any(isinstance(action, type_) for type_ in (str, int)): coroutine = controller.handle_action(action, extra=extra) if idx == len(fired_actions) - 1: await coroutine else: asyncio.ensure_future(coroutine) elif isinstance(action, float): await asyncio.sleep(action) pending = asyncio.Task.all_tasks() # We exclude the current function we are executing pending = { task for task in pending if task._coro.__name__ != "test_integ_configs" # type: ignore } if pending: # Finish pending tasks if any await asyncio.wait(pending) assert call_service_stub.call_count == expected_calls_count calls = [ mocker.call(controller, call["service"], **call.get("data", {})) for call in expected_calls ] call_service_stub.assert_has_calls(calls)
def test_devices(device_class: Type[Controller]): device = device_class() # type: ignore # We first check that all devices are importable from controllerx module device_from_controllerx = get_controller("controllerx", device_class.__name__) assert ( device_from_controllerx is not None ), f"'{device_class.__name__}' not importable from controllerx.py" predefined_actions_mapping = device.get_predefined_actions_mapping() possible_actions = predefined_actions_mapping.keys() integration_mappings_funcs: List[Callable[[], Optional[DefaultActionsMapping]]] = [ device.get_z2m_actions_mapping, device.get_deconz_actions_mapping, device.get_zha_actions_mapping, ] for func in integration_mappings_funcs: mappings = func() check_mapping(mappings, possible_actions, device)
def test_devices(hass_mock, device_class): device = device_class() # We first check that all devices are importable from controllerx module device_from_controllerx = get_controller("controllerx", device_class.__name__) assert device_from_controllerx is not None type_actions_mapping = device.get_type_actions_mapping() if type_actions_mapping is None: return possible_actions = list(type_actions_mapping.keys()) integration_mappings_funcs = [ device.get_z2m_actions_mapping, device.get_deconz_actions_mapping, device.get_zha_actions_mapping, ] for func in integration_mappings_funcs: mappings = func() check_mapping(mappings, possible_actions, device)
async def test_integ_configs(hass_mock, mocker, config_file, data): entity_state_attributes = data.get("entity_state_attributes", {}) entity_state = data.get("entity_state", None) fired_actions = data.get("fired_actions", []) expected_calls = data.get("expected_calls", []) expected_calls_count = data.get("expected_calls_count", len(expected_calls)) config = read_config_yaml(config_file) controller = get_controller(config["module"], config["class"]) controller.args = config fake_entity_states = get_fake_entity_states(entity_state, entity_state_attributes) mocker.patch.object(controller, "get_entity_state", fake_entity_states) call_service_stub = mocker.patch.object(controller, "call_service") await controller.initialize() for idx, action in enumerate(fired_actions): if any(isinstance(action, type_) for type_ in (str, int)): if idx == len(fired_actions) - 1: await controller.handle_action(action) else: asyncio.ensure_future(controller.handle_action(action)) elif isinstance(action, float): await asyncio.sleep(action) pending = asyncio.Task.all_tasks() # We exclude the current function we are executing pending = { task for task in pending if task._coro.__name__ != "test_integ_configs" } if pending: # Finish pending tasks if any await asyncio.wait(pending) assert call_service_stub.call_count == expected_calls_count calls = [ mocker.call(call["service"], **call.get("data", {})) for call in expected_calls ] call_service_stub.assert_has_calls(calls)