Restore last_triggered state in scripts (#55071)

This commit is contained in:
Franck Nijhof 2021-08-23 20:05:29 +02:00 committed by GitHub
parent a3ff05f367
commit 2cdaf632a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 3 deletions

View file

@ -15,16 +15,25 @@ from homeassistant.const import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
)
from homeassistant.core import (
Context,
CoreState,
HomeAssistant,
State,
callback,
split_entity_id,
)
from homeassistant.core import Context, callback, split_entity_id
from homeassistant.exceptions import ServiceNotFound
from homeassistant.helpers import template
from homeassistant.helpers.event import async_track_state_change
from homeassistant.helpers.service import async_get_all_descriptions
from homeassistant.loader import bind_hass
from homeassistant.setup import async_setup_component, setup_component
import homeassistant.util.dt as dt_util
from tests.common import async_mock_service, get_test_home_assistant
from tests.common import async_mock_service, get_test_home_assistant, mock_restore_cache
from tests.components.logbook.test_init import MockLazyEventPartialState
ENTITY_ID = "script.test"
@ -84,6 +93,7 @@ class TestScriptComponent(unittest.TestCase):
def test_passing_variables(self):
"""Test different ways of passing in variables."""
mock_restore_cache(self.hass, ())
calls = []
context = Context()
@ -796,3 +806,39 @@ async def test_script_this_var_always(hass, caplog):
# Verify this available to all templates
assert mock_calls[0].data.get("this_template") == "script.script1"
assert "Error rendering variables" not in caplog.text
async def test_script_restore_last_triggered(hass: HomeAssistant) -> None:
"""Test if last triggered is restored on start."""
time = dt_util.utcnow()
mock_restore_cache(
hass,
(
State("script.no_last_triggered", STATE_OFF),
State("script.last_triggered", STATE_OFF, {"last_triggered": time}),
),
)
hass.state = CoreState.starting
assert await async_setup_component(
hass,
"script",
{
"script": {
"no_last_triggered": {
"sequence": [{"delay": {"seconds": 5}}],
},
"last_triggered": {
"sequence": [{"delay": {"seconds": 5}}],
},
},
},
)
state = hass.states.get("script.no_last_triggered")
assert state
assert state.attributes["last_triggered"] is None
state = hass.states.get("script.last_triggered")
assert state
assert state.attributes["last_triggered"] == time