diff --git a/docs/examples/index.md b/docs/examples/index.md index 3bbad81b..23e8be7a 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -41,15 +41,15 @@ bedroom_speaker: Controlling a light (just on/off) with E1743 with ZHA: ```yaml -bedroom_speaker: +bedroom_light: module: controllerx class: E1743Controller controller: 00:67:88:56:06:78:9b:3f integration: zha light: light.simple_light actions: - - on - - off + - "on" + - "off" ``` Controlling two lights with Aqara double key wireless switch (z2m): diff --git a/tests/integ_tests/actions_attribute/actions_attribute_on_off_test.yaml b/tests/integ_tests/actions_attribute/actions_attribute_on_off_test.yaml new file mode 100644 index 00000000..11a362e4 --- /dev/null +++ b/tests/integ_tests/actions_attribute/actions_attribute_on_off_test.yaml @@ -0,0 +1,12 @@ +entity_state_attributes: + supported_features: 191 +entity_state: "on" +fired_actions: ["on", 450, "off"] +expected_calls: + - service: light/turn_on + data: + entity_id: light.simple_light + transition: 0.3 + - service: light/turn_off + data: + entity_id: light.simple_light diff --git a/tests/integ_tests/actions_attribute/actions_attribute_rest_test.yaml b/tests/integ_tests/actions_attribute/actions_attribute_rest_test.yaml new file mode 100644 index 00000000..f253b521 --- /dev/null +++ b/tests/integ_tests/actions_attribute/actions_attribute_rest_test.yaml @@ -0,0 +1,2 @@ +fired_actions: ["brightness_up", "brightness_down", "brightness_stop"] +expected_calls_count: 0 diff --git a/tests/integ_tests/actions_attribute/config.yaml b/tests/integ_tests/actions_attribute/config.yaml new file mode 100644 index 00000000..2e0f55f4 --- /dev/null +++ b/tests/integ_tests/actions_attribute/config.yaml @@ -0,0 +1,9 @@ +bedroom_light: + module: controllerx + class: E1743Controller + controller: 00:67:88:56:06:78:9b:3f + integration: zha + light: light.simple_light + actions: + - "on" + - "off" \ No newline at end of file diff --git a/tests/integ_tests/example_config/example_config_toggle_colortemp_test.yaml b/tests/integ_tests/example_config/example_config_toggle_colortemp_test.yaml new file mode 100644 index 00000000..5ab7af0d --- /dev/null +++ b/tests/integ_tests/example_config/example_config_toggle_colortemp_test.yaml @@ -0,0 +1,15 @@ +entity_state_attributes: + supported_features: 175 +entity_state: "on" +fired_actions: ["toggle", 1000, "toggle_hold"] +expected_calls: + - service: light/toggle + data: + entity_id: light.bedroom + - service: light/turn_on + data: + entity_id: light.bedroom + color_temp: 370 + brightness: 255 + transition: 0.3 +expected_calls_count: 2 \ No newline at end of file diff --git a/tests/integ_tests/example_config/example_config_toggle_test.yaml b/tests/integ_tests/example_config/example_config_toggle_xycolor_test.yaml similarity index 100% rename from tests/integ_tests/example_config/example_config_toggle_test.yaml rename to tests/integ_tests/example_config/example_config_toggle_xycolor_test.yaml diff --git a/tests/integ_tests/example_config/no_call_services_in_initialize_test.yaml b/tests/integ_tests/example_config/no_call_services_in_initialize_test.yaml new file mode 100644 index 00000000..1651a811 --- /dev/null +++ b/tests/integ_tests/example_config/no_call_services_in_initialize_test.yaml @@ -0,0 +1 @@ +expected_calls_count: 0 \ No newline at end of file diff --git a/tests/integ_tests/integ_test.py b/tests/integ_tests/integ_test.py index 09028919..612295dd 100644 --- a/tests/integ_tests/integ_test.py +++ b/tests/integ_tests/integ_test.py @@ -24,32 +24,39 @@ def read_config_yaml(file_name): return list(data.values())[0] +def get_controller(module_name, class_name): + module = importlib.import_module(module_name) + class_ = getattr(module, class_name) + return class_() + + +def get_fake_entity_states(entity_state, entity_state_attributes): + async def inner(entity_id, attribute=None): + if attribute is not None and attribute in entity_state_attributes: + return entity_state_attributes[attribute] + return entity_state + + return inner + + integration_tests = get_integ_tests() @pytest.mark.asyncio @pytest.mark.parametrize("config_file, data", integration_tests) async def test_example_config(hass_mock, mocker, config_file, data): - entity_state_attributes = data["entity_state_attributes"] - entity_state = data["entity_state"] - fired_actions = data["fired_actions"] - expected_calls = data["expected_calls"] + 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) - module_name = config["module"] - class_name = config["class"] - module = importlib.import_module(module_name) - class_ = getattr(module, class_name) - controller = class_() + controller = get_controller(config["module"], config["class"]) controller.args = config - async def fake_supported_features(entity_id, attribute=None): - if attribute is not None and attribute in entity_state_attributes: - return entity_state_attributes[attribute] - return entity_state - - mocker.patch.object(controller, "get_entity_state", fake_supported_features) + 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() @@ -64,7 +71,8 @@ async def fake_supported_features(entity_id, attribute=None): elif isinstance(action, int): await asyncio.sleep(action / 1000) - await asyncio.wait(tasks) # Finish pending tasks + if tasks: # Finish pending tasks if any + await asyncio.wait(tasks) assert call_service_stub.call_count == expected_calls_count calls = [mocker.call(call["service"], **call["data"]) for call in expected_calls] call_service_stub.assert_has_calls(calls)