From 8837ed35cd6697b3e5ec4496a3400a14a70843b6 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 26 Sep 2020 19:28:26 +0200 Subject: [PATCH] Use direct service calls in tests instead of automation common (#40623) * Use direct service calls in tests instead of automation common * Remove automation common test helpers --- tests/components/automation/common.py | 56 ------------- tests/components/automation/test_init.py | 81 ++++++++++++------- tests/components/geo_location/test_trigger.py | 10 ++- .../homeassistant/triggers/test_event.py | 20 +++-- .../triggers/test_numeric_state.py | 20 +++-- .../homeassistant/triggers/test_state.py | 18 +++-- .../triggers/test_time_pattern.py | 10 ++- tests/components/mqtt/test_trigger.py | 12 ++- tests/components/sun/test_trigger.py | 26 ++++-- tests/components/template/test_trigger.py | 18 +++-- tests/components/zone/test_trigger.py | 10 ++- 11 files changed, 156 insertions(+), 125 deletions(-) delete mode 100644 tests/components/automation/common.py diff --git a/tests/components/automation/common.py b/tests/components/automation/common.py deleted file mode 100644 index 26521f76d31..00000000000 --- a/tests/components/automation/common.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Collection of helper methods. - -All containing methods are legacy helpers that should not be used by new -components. Instead call the service directly. -""" -from homeassistant.components.automation import ( - CONF_SKIP_CONDITION, - DOMAIN, - SERVICE_TRIGGER, -) -from homeassistant.const import ( - ATTR_ENTITY_ID, - ENTITY_MATCH_ALL, - SERVICE_RELOAD, - SERVICE_TOGGLE, - SERVICE_TURN_OFF, - SERVICE_TURN_ON, -) -from homeassistant.loader import bind_hass - - -@bind_hass -async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL): - """Turn on specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data) - - -@bind_hass -async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL): - """Turn off specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data) - - -@bind_hass -async def async_toggle(hass, entity_id=ENTITY_MATCH_ALL): - """Toggle specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data) - - -@bind_hass -async def async_trigger(hass, entity_id=ENTITY_MATCH_ALL, skip_condition=True): - """Trigger specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - data[CONF_SKIP_CONDITION] = skip_condition - await hass.services.async_call(DOMAIN, SERVICE_TRIGGER, data) - - -@bind_hass -async def async_reload(hass, context=None): - """Reload the automation from config.""" - await hass.services.async_call( - DOMAIN, SERVICE_RELOAD, blocking=True, context=context - ) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 9c38574945d..1cdcfc11dfb 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -10,12 +10,16 @@ from homeassistant.components.automation import ( DOMAIN, EVENT_AUTOMATION_RELOADED, EVENT_AUTOMATION_TRIGGERED, + SERVICE_TRIGGER, ) from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_NAME, EVENT_HOMEASSISTANT_STARTED, + SERVICE_RELOAD, + SERVICE_TOGGLE, SERVICE_TURN_OFF, + SERVICE_TURN_ON, STATE_OFF, STATE_ON, ) @@ -26,7 +30,6 @@ import homeassistant.util.dt as dt_util from tests.async_mock import Mock, patch from tests.common import assert_setup_component, async_mock_service, mock_restore_cache -from tests.components.automation import common from tests.components.logbook.test_init import MockLazyEventPartialState @@ -402,45 +405,59 @@ async def test_services(hass, calls): await hass.async_block_till_done() assert len(calls) == 1 - await common.async_turn_off(hass, entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: entity_id, + }, + blocking=True, + ) assert not automation.is_on(hass, entity_id) hass.bus.async_fire("test_event") await hass.async_block_till_done() assert len(calls) == 1 - await common.async_toggle(hass, entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) assert automation.is_on(hass, entity_id) hass.bus.async_fire("test_event") await hass.async_block_till_done() assert len(calls) == 2 - await common.async_toggle(hass, entity_id) - await hass.async_block_till_done() - + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TOGGLE, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) assert not automation.is_on(hass, entity_id) hass.bus.async_fire("test_event") await hass.async_block_till_done() assert len(calls) == 2 - await common.async_toggle(hass, entity_id) - await hass.async_block_till_done() - - await common.async_trigger(hass, entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + await hass.services.async_call( + automation.DOMAIN, SERVICE_TRIGGER, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) assert len(calls) == 3 - await common.async_turn_off(hass, entity_id) - await hass.async_block_till_done() - await common.async_trigger(hass, entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + await hass.services.async_call( + automation.DOMAIN, SERVICE_TRIGGER, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) assert len(calls) == 4 - await common.async_turn_on(hass, entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) assert automation.is_on(hass, entity_id) @@ -492,10 +509,18 @@ async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_onl }, ): with pytest.raises(Unauthorized): - await common.async_reload(hass, Context(user_id=hass_read_only_user.id)) - await hass.async_block_till_done() - await common.async_reload(hass, Context(user_id=hass_admin_user.id)) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_RELOAD, + context=Context(user_id=hass_read_only_user.id), + blocking=True, + ) + await hass.services.async_call( + automation.DOMAIN, + SERVICE_RELOAD, + context=Context(user_id=hass_admin_user.id), + blocking=True, + ) # De-flake ?! await hass.async_block_till_done() @@ -547,8 +572,7 @@ async def test_reload_config_when_invalid_config(hass, calls): autospec=True, return_value={automation.DOMAIN: "not valid"}, ): - await common.async_reload(hass) - await hass.async_block_till_done() + await hass.services.async_call(automation.DOMAIN, SERVICE_RELOAD, blocking=True) assert hass.states.get("automation.hello") is None @@ -585,8 +609,7 @@ async def test_reload_config_handles_load_fails(hass, calls): "homeassistant.config.load_yaml_config_file", side_effect=HomeAssistantError("bla"), ): - await common.async_reload(hass) - await hass.async_block_till_done() + await hass.services.async_call(automation.DOMAIN, SERVICE_RELOAD, blocking=True) assert hass.states.get("automation.hello") is not None @@ -646,7 +669,9 @@ async def test_automation_stops(hass, calls, service): autospec=True, return_value=config, ): - await common.async_reload(hass) + await hass.services.async_call( + automation.DOMAIN, SERVICE_RELOAD, blocking=True + ) hass.states.async_set(test_entity, "goodbye") await hass.async_block_till_done() diff --git a/tests/components/geo_location/test_trigger.py b/tests/components/geo_location/test_trigger.py index 99ace50e77d..8bf1c6abe15 100644 --- a/tests/components/geo_location/test_trigger.py +++ b/tests/components/geo_location/test_trigger.py @@ -2,11 +2,11 @@ import pytest from homeassistant.components import automation, zone +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context from homeassistant.setup import async_setup_component from tests.common import async_mock_service, mock_component -from tests.components.automation import common @pytest.fixture @@ -98,8 +98,12 @@ async def test_if_fires_on_zone_enter(hass, calls): ) await hass.async_block_till_done() - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.states.async_set( "geo_location.entity", diff --git a/tests/components/homeassistant/triggers/test_event.py b/tests/components/homeassistant/triggers/test_event.py index 84b7e725f0a..0e4c2674b1b 100644 --- a/tests/components/homeassistant/triggers/test_event.py +++ b/tests/components/homeassistant/triggers/test_event.py @@ -2,11 +2,11 @@ import pytest import homeassistant.components.automation as automation +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context from homeassistant.setup import async_setup_component from tests.common import async_mock_service, mock_component -from tests.components.automation import common @pytest.fixture @@ -38,11 +38,15 @@ async def test_if_fires_on_event(hass, calls): hass.bus.async_fire("test_event", context=context) await hass.async_block_till_done() - assert 1 == len(calls) + assert len(calls) == 1 assert calls[0].context.parent_id == context.id - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.bus.async_fire("test_event") await hass.async_block_till_done() @@ -66,8 +70,12 @@ async def test_if_fires_on_event_extra_data(hass, calls): await hass.async_block_till_done() assert len(calls) == 1 - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.bus.async_fire("test_event") await hass.async_block_till_done() diff --git a/tests/components/homeassistant/triggers/test_numeric_state.py b/tests/components/homeassistant/triggers/test_numeric_state.py index 932dde91120..09a13f95603 100644 --- a/tests/components/homeassistant/triggers/test_numeric_state.py +++ b/tests/components/homeassistant/triggers/test_numeric_state.py @@ -8,6 +8,7 @@ import homeassistant.components.automation as automation from homeassistant.components.homeassistant.triggers import ( numeric_state as numeric_state_trigger, ) +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -19,7 +20,6 @@ from tests.common import ( async_mock_service, mock_component, ) -from tests.components.automation import common @pytest.fixture @@ -59,8 +59,13 @@ async def test_if_fires_on_entity_change_below(hass, calls): # Set above 12 so the automation will fire again hass.states.async_set("test.entity", 12) - await common.async_turn_off(hass) - await hass.async_block_till_done() + + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.states.async_set("test.entity", 9) await hass.async_block_till_done() assert len(calls) == 1 @@ -863,9 +868,12 @@ async def test_if_not_fires_on_entities_change_with_for_after_stop(hass, calls): hass.states.async_set("test.entity_1", 9) hass.states.async_set("test.entity_2", 9) await hass.async_block_till_done() - await common.async_turn_off(hass) - await hass.async_block_till_done() - + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) await hass.async_block_till_done() assert len(calls) == 1 diff --git a/tests/components/homeassistant/triggers/test_state.py b/tests/components/homeassistant/triggers/test_state.py index 990fc9cc956..688115dc400 100644 --- a/tests/components/homeassistant/triggers/test_state.py +++ b/tests/components/homeassistant/triggers/test_state.py @@ -5,6 +5,7 @@ import pytest import homeassistant.components.automation as automation from homeassistant.components.homeassistant.triggers import state as state_trigger +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -16,7 +17,6 @@ from tests.common import ( async_mock_service, mock_component, ) -from tests.components.automation import common @pytest.fixture @@ -70,8 +70,12 @@ async def test_if_fires_on_entity_change(hass, calls): assert calls[0].context.parent_id == context.id assert calls[0].data["some"] == "state - test.entity - hello - world - None" - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.states.async_set("test.entity", "planet") await hass.async_block_till_done() assert len(calls) == 1 @@ -394,8 +398,12 @@ async def test_if_not_fires_on_entities_change_with_for_after_stop(hass, calls): hass.states.async_set("test.entity_1", "world") hass.states.async_set("test.entity_2", "world") await hass.async_block_till_done() - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) await hass.async_block_till_done() diff --git a/tests/components/homeassistant/triggers/test_time_pattern.py b/tests/components/homeassistant/triggers/test_time_pattern.py index 3d32748c176..f428bcf29bc 100644 --- a/tests/components/homeassistant/triggers/test_time_pattern.py +++ b/tests/components/homeassistant/triggers/test_time_pattern.py @@ -6,12 +6,12 @@ import voluptuous as vol import homeassistant.components.automation as automation import homeassistant.components.homeassistant.triggers.time_pattern as time_pattern +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.async_mock import patch from tests.common import async_fire_time_changed, async_mock_service, mock_component -from tests.components.automation import common @pytest.fixture @@ -55,8 +55,12 @@ async def test_if_fires_when_hour_matches(hass, calls): await hass.async_block_till_done() assert len(calls) == 1 - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_time_changed(hass, now.replace(year=now.year + 1, hour=0)) await hass.async_block_till_done() diff --git a/tests/components/mqtt/test_trigger.py b/tests/components/mqtt/test_trigger.py index f0dd76ff1b4..13e96c69adc 100644 --- a/tests/components/mqtt/test_trigger.py +++ b/tests/components/mqtt/test_trigger.py @@ -4,10 +4,10 @@ from unittest import mock import pytest import homeassistant.components.automation as automation +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.setup import async_setup_component from tests.common import async_fire_mqtt_message, async_mock_service, mock_component -from tests.components.automation import common @pytest.fixture @@ -44,11 +44,15 @@ async def test_if_fires_on_topic_match(hass, calls): async_fire_mqtt_message(hass, "test-topic", '{ "hello": "world" }') await hass.async_block_till_done() - assert 1 == len(calls) + assert len(calls) == 1 assert 'mqtt - test-topic - { "hello": "world" } - world' == calls[0].data["some"] - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_mqtt_message(hass, "test-topic", "test_payload") await hass.async_block_till_done() assert len(calls) == 1 diff --git a/tests/components/sun/test_trigger.py b/tests/components/sun/test_trigger.py index f730dae3cf1..2cb21051200 100644 --- a/tests/components/sun/test_trigger.py +++ b/tests/components/sun/test_trigger.py @@ -5,13 +5,19 @@ import pytest from homeassistant.components import sun import homeassistant.components.automation as automation -from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET +from homeassistant.const import ( + ATTR_ENTITY_ID, + ENTITY_MATCH_ALL, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + SUN_EVENT_SUNRISE, + SUN_EVENT_SUNSET, +) from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.async_mock import patch from tests.common import async_fire_time_changed, async_mock_service, mock_component -from tests.components.automation import common ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE @@ -54,16 +60,24 @@ async def test_sunset_trigger(hass, calls, legacy_patchable_time): }, ) - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_time_changed(hass, trigger_time) await hass.async_block_till_done() assert len(calls) == 0 with patch("homeassistant.util.dt.utcnow", return_value=now): - await common.async_turn_on(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) async_fire_time_changed(hass, trigger_time) await hass.async_block_till_done() diff --git a/tests/components/template/test_trigger.py b/tests/components/template/test_trigger.py index 300173fdadf..4bda4dc23ca 100644 --- a/tests/components/template/test_trigger.py +++ b/tests/components/template/test_trigger.py @@ -6,6 +6,7 @@ import pytest import homeassistant.components.automation as automation from homeassistant.components.template import trigger as template_trigger +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context, callback from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -16,7 +17,6 @@ from tests.common import ( async_mock_service, mock_component, ) -from tests.components.automation import common @pytest.fixture @@ -52,8 +52,12 @@ async def test_if_fires_on_change_bool(hass, calls): await hass.async_block_till_done() assert len(calls) == 1 - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.states.async_set("test.entity", "planet") await hass.async_block_till_done() @@ -698,8 +702,12 @@ async def test_if_not_fires_when_turned_off_with_for(hass, calls): async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=4)) await hass.async_block_till_done() assert len(calls) == 0 - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) assert len(calls) == 0 async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=6)) await hass.async_block_till_done() diff --git a/tests/components/zone/test_trigger.py b/tests/components/zone/test_trigger.py index e80f70b10fe..0477f9bead7 100644 --- a/tests/components/zone/test_trigger.py +++ b/tests/components/zone/test_trigger.py @@ -2,11 +2,11 @@ import pytest from homeassistant.components import automation, zone +from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.core import Context from homeassistant.setup import async_setup_component from tests.common import async_mock_service, mock_component -from tests.components.automation import common @pytest.fixture @@ -91,8 +91,12 @@ async def test_if_fires_on_zone_enter(hass, calls): ) await hass.async_block_till_done() - await common.async_turn_off(hass) - await hass.async_block_till_done() + await hass.services.async_call( + automation.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) hass.states.async_set( "test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}