Fix repeat action when variables present (#38237)

This commit is contained in:
Phil Bruckner authored and Franck Nijhof committed 2020-07-28 18:58:10 +02:00
1 parent e89c475856
commit bdab437574
2 files changed
+70 -26

No files matched your search

+32 -6
View File
@@ -4,7 +4,19 @@ from datetime import datetime
from functools import partial
import itertools
import logging
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple
from types import MappingProxyType
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Sequence,
Set,
Tuple,
Union,
cast,
)
from async_timeout import timeout
import voluptuous as vol
@@ -134,13 +146,13 @@ class _ScriptRun:
self,
hass: HomeAssistant,
script: "Script",
variables: Optional[Sequence],
variables: Dict[str, Any],
context: Optional[Context],
log_exceptions: bool,
) -> None:
self._hass = hass
self._script = script
self._variables = variables or {}
self._variables = variables
self._context = context
self._log_exceptions = log_exceptions
self._step = -1
@@ -595,6 +607,9 @@ async def _async_stop_scripts_at_shutdown(hass, event):
)
_VarsType = Union[Dict[str, Any], MappingProxyType]
class Script:
"""Representation of a script."""
@@ -617,6 +632,7 @@ class Script:
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, partial(_async_stop_scripts_at_shutdown, hass)
)
self._top_level = top_level
if top_level:
all_scripts.append(
{"instance": self, "started_before_shutdown": not hass.is_stopping}
@@ -732,14 +748,16 @@ class Script:
self._referenced_entities = referenced
return referenced
def run(self, variables=None, context=None):
def run(
self, variables: Optional[_VarsType] = None, context: Optional[Context] = None
) -> None:
"""Run script."""
asyncio.run_coroutine_threadsafe(
self.async_run(variables, context), self._hass.loop
).result()
async def async_run(
self, variables: Optional[Sequence] = None, context: Optional[Context] = None
self, variables: Optional[_VarsType] = None, context: Optional[Context] = None
) -> None:
"""Run script."""
if self.is_running:
@@ -753,11 +771,19 @@ class Script:
self._log("Maximum number of runs exceeded", level=logging.WARNING)
return
# If this is a top level Script then make a copy of the variables in case they
# are read-only, but more importantly, so as not to leak any variables created
# during the run back to the caller.
if self._top_level:
variables = dict(variables) if variables is not None else {}
if self.script_mode != SCRIPT_MODE_QUEUED:
cls = _ScriptRun
else:
cls = _QueuedScriptRun
run = cls(self._hass, self, variables, context, self._log_exceptions)
run = cls(
self._hass, self, cast(dict, variables), context, self._log_exceptions
)
self._runs.append(run)
try: