Stop automation runs when turned off or reloaded (#38174)
* Add automation turn off / reload test * Stop automation runs when turned off or reloaded
This commit is contained in:
parent
662d79eb86
commit
bbc8748e3b
2 changed files with 58 additions and 1 deletions
|
@ -467,6 +467,8 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
|
||||||
self._async_detach_triggers()
|
self._async_detach_triggers()
|
||||||
self._async_detach_triggers = None
|
self._async_detach_triggers = None
|
||||||
|
|
||||||
|
await self.action_script.async_stop()
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def _async_attach_triggers(
|
async def _async_attach_triggers(
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""The tests for the automation component."""
|
"""The tests for the automation component."""
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import logbook
|
from homeassistant.components import logbook
|
||||||
|
@ -12,10 +14,11 @@ from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_NAME,
|
ATTR_NAME,
|
||||||
EVENT_HOMEASSISTANT_STARTED,
|
EVENT_HOMEASSISTANT_STARTED,
|
||||||
|
SERVICE_TURN_OFF,
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, CoreState, State
|
from homeassistant.core import Context, CoreState, State, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
@ -553,6 +556,58 @@ async def test_reload_config_handles_load_fails(hass, calls):
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("service", ["turn_off", "reload"])
|
||||||
|
async def test_automation_stops(hass, calls, service):
|
||||||
|
"""Test that turning off / reloading an automation stops any running actions."""
|
||||||
|
entity_id = "automation.hello"
|
||||||
|
test_entity = "test.entity"
|
||||||
|
|
||||||
|
config = {
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"alias": "hello",
|
||||||
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||||
|
"action": [
|
||||||
|
{"event": "running"},
|
||||||
|
{"wait_template": "{{ is_state('test.entity', 'goodbye') }}"},
|
||||||
|
{"service": "test.automation"},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert await async_setup_component(hass, automation.DOMAIN, config,)
|
||||||
|
|
||||||
|
running = asyncio.Event()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def running_cb(event):
|
||||||
|
running.set()
|
||||||
|
|
||||||
|
hass.bus.async_listen_once("running", running_cb)
|
||||||
|
hass.states.async_set(test_entity, "hello")
|
||||||
|
|
||||||
|
hass.bus.async_fire("test_event")
|
||||||
|
await running.wait()
|
||||||
|
|
||||||
|
if service == "turn_off":
|
||||||
|
await hass.services.async_call(
|
||||||
|
automation.DOMAIN,
|
||||||
|
SERVICE_TURN_OFF,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
with patch(
|
||||||
|
"homeassistant.config.load_yaml_config_file",
|
||||||
|
autospec=True,
|
||||||
|
return_value=config,
|
||||||
|
):
|
||||||
|
await common.async_reload(hass)
|
||||||
|
|
||||||
|
hass.states.async_set(test_entity, "goodbye")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(calls) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_automation_restore_state(hass):
|
async def test_automation_restore_state(hass):
|
||||||
"""Ensure states are restored on startup."""
|
"""Ensure states are restored on startup."""
|
||||||
time = dt_util.utcnow()
|
time = dt_util.utcnow()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue