From a7d327afa210fbe19d4a16849444226a2f07c4a3 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 19 Jun 2023 23:05:20 +0200 Subject: [PATCH] Improve test coverage of automation extraction functions (#94878) --- tests/components/automation/test_init.py | 93 +++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 3859e0c857c..d1eb93771da 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -1537,6 +1537,27 @@ async def test_automation_restore_last_triggered_with_initial_state( assert state.attributes["last_triggered"] == time +async def test_extraction_functions_not_setup(hass: HomeAssistant) -> None: + """Test extraction functions when automation is not setup.""" + assert automation.automations_with_area(hass, "area-in-both") == [] + assert automation.areas_in_automation(hass, "automation.test") == [] + assert automation.automations_with_blueprint(hass, "blabla.yaml") == [] + assert automation.blueprint_in_automation(hass, "automation.test") is None + assert automation.automations_with_device(hass, "device-in-both") == [] + assert automation.devices_in_automation(hass, "automation.test") == [] + assert automation.automations_with_entity(hass, "light.in_both") == [] + assert automation.entities_in_automation(hass, "automation.test") == [] + + +async def test_extraction_functions_unknown_automation(hass: HomeAssistant) -> None: + """Test extraction functions for an unknown automation.""" + assert await async_setup_component(hass, DOMAIN, {}) + assert automation.areas_in_automation(hass, "automation.unknown") == [] + assert automation.blueprint_in_automation(hass, "automation.unknown") is None + assert automation.devices_in_automation(hass, "automation.unknown") == [] + assert automation.entities_in_automation(hass, "automation.unknown") == [] + + async def test_extraction_functions(hass: HomeAssistant) -> None: """Test extraction functions.""" await async_setup_component(hass, "homeassistant", {}) @@ -1604,6 +1625,10 @@ async def test_extraction_functions(hass: HomeAssistant) -> None: "entity_id": "light.bla", "type": "turn_on", }, + { + "service": "test.test", + "target": {"area_id": "area-in-both"}, + }, ], }, { @@ -1635,7 +1660,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None: { "platform": "event", "event_type": "esphome.button_pressed", - "event_data": {"device_id": ["device-trigger-event"]}, + "event_data": {"device_id": ["device-trigger-event2"]}, }, # device_id is not a string { @@ -1676,6 +1701,55 @@ async def test_extraction_functions(hass: HomeAssistant) -> None: }, ], }, + { + "alias": "test3", + "trigger": [ + { + "platform": "event", + "event_type": "esphome.button_pressed", + "event_data": {"area_id": "area-trigger-event"}, + }, + # area_id is a list of strings (not supported) + { + "platform": "event", + "event_type": "esphome.button_pressed", + "event_data": {"area_id": ["area-trigger-event2"]}, + }, + # area_id is not a string + { + "platform": "event", + "event_type": "esphome.button_pressed", + "event_data": {"area_id": 123}, + }, + ], + "condition": { + "condition": "device", + "device_id": "condition-device", + "domain": "light", + "type": "is_on", + "entity_id": "light.bla", + }, + "action": [ + { + "service": "test.script", + "data": {"entity_id": "light.in_both"}, + }, + { + "condition": "state", + "entity_id": "sensor.condition", + "state": "100", + }, + {"scene": "scene.hello"}, + { + "service": "test.test", + "target": {"area_id": "area-in-both"}, + }, + { + "service": "test.test", + "target": {"area_id": "area-in-last"}, + }, + ], + }, ] }, ) @@ -1683,6 +1757,7 @@ async def test_extraction_functions(hass: HomeAssistant) -> None: assert set(automation.automations_with_entity(hass, "light.in_both")) == { "automation.test1", "automation.test2", + "automation.test3", } assert set(automation.entities_in_automation(hass, "automation.test1")) == { "calendar.trigger_calendar", @@ -1707,6 +1782,15 @@ async def test_extraction_functions(hass: HomeAssistant) -> None: "device-trigger-tag2", "device-trigger-tag3", } + assert set(automation.automations_with_area(hass, "area-in-both")) == { + "automation.test1", + "automation.test3", + } + assert set(automation.areas_in_automation(hass, "automation.test3")) == { + "area-in-both", + "area-in-last", + } + assert automation.blueprint_in_automation(hass, "automation.test3") is None async def test_logbook_humanify_automation_triggered_event(hass: HomeAssistant) -> None: @@ -1980,6 +2064,13 @@ async def test_blueprint_automation(hass: HomeAssistant, calls) -> None: assert automation.entities_in_automation(hass, "automation.automation_0") == [ "light.kitchen" ] + assert ( + automation.blueprint_in_automation(hass, "automation.automation_0") + == "test_event_service.yaml" + ) + assert automation.automations_with_blueprint(hass, "test_event_service.yaml") == [ + "automation.automation_0" + ] @pytest.mark.parametrize(