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
This commit is contained in:
Franck Nijhof 2020-09-26 19:28:26 +02:00 committed by GitHub
parent 35533407fe
commit 8837ed35cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 156 additions and 125 deletions

View File

@ -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
)

View File

@ -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()

View File

@ -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",

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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}