1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add support for "alias" in script steps device, device_condition, and conditions (#46647)

Co-authored-by: Donnie <donniekarnsinsb@hotmail.com>
This commit is contained in:
Erik Montnemery
2021-02-21 04:21:09 +01:00
committed by GitHub
parent 3ad207a499
commit 2d70806035
5 changed files with 146 additions and 72 deletions

View File

@@ -18,7 +18,7 @@ from typing import (
cast,
)
from async_timeout import timeout
import async_timeout
import voluptuous as vol
from homeassistant import exceptions
@@ -235,6 +235,13 @@ class _ScriptRun:
msg, *args, level=level, **kwargs
)
def _step_log(self, default_message, timeout=None):
self._script.last_action = self._action.get(CONF_ALIAS, default_message)
_timeout = (
"" if timeout is None else f" (timeout: {timedelta(seconds=timeout)})"
)
self._log("Executing step %s%s", self._script.last_action, _timeout)
async def async_run(self) -> None:
"""Run script."""
try:
@@ -327,13 +334,12 @@ class _ScriptRun:
"""Handle delay."""
delay = self._get_pos_time_period_template(CONF_DELAY)
self._script.last_action = self._action.get(CONF_ALIAS, f"delay {delay}")
self._log("Executing step %s", self._script.last_action)
self._step_log(f"delay {delay}")
delay = delay.total_seconds()
self._changed()
try:
async with timeout(delay):
async with async_timeout.timeout(delay):
await self._stop.wait()
except asyncio.TimeoutError:
pass
@@ -341,18 +347,13 @@ class _ScriptRun:
async def _async_wait_template_step(self):
"""Handle a wait template."""
if CONF_TIMEOUT in self._action:
delay = self._get_pos_time_period_template(CONF_TIMEOUT).total_seconds()
timeout = self._get_pos_time_period_template(CONF_TIMEOUT).total_seconds()
else:
delay = None
timeout = None
self._script.last_action = self._action.get(CONF_ALIAS, "wait template")
self._log(
"Executing step %s%s",
self._script.last_action,
"" if delay is None else f" (timeout: {timedelta(seconds=delay)})",
)
self._step_log("wait template", timeout)
self._variables["wait"] = {"remaining": delay, "completed": False}
self._variables["wait"] = {"remaining": timeout, "completed": False}
wait_template = self._action[CONF_WAIT_TEMPLATE]
wait_template.hass = self._hass
@@ -366,7 +367,7 @@ class _ScriptRun:
def async_script_wait(entity_id, from_s, to_s):
"""Handle script after template condition is true."""
self._variables["wait"] = {
"remaining": to_context.remaining if to_context else delay,
"remaining": to_context.remaining if to_context else timeout,
"completed": True,
}
done.set()
@@ -382,7 +383,7 @@ class _ScriptRun:
self._hass.async_create_task(flag.wait()) for flag in (self._stop, done)
]
try:
async with timeout(delay) as to_context:
async with async_timeout.timeout(timeout) as to_context:
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
except asyncio.TimeoutError as ex:
if not self._action.get(CONF_CONTINUE_ON_TIMEOUT, True):
@@ -431,8 +432,7 @@ class _ScriptRun:
async def _async_call_service_step(self):
"""Call the service specified in the action."""
self._script.last_action = self._action.get(CONF_ALIAS, "call service")
self._log("Executing step %s", self._script.last_action)
self._step_log("call service")
params = service.async_prepare_call_from_config(
self._hass, self._action, self._variables
@@ -467,8 +467,7 @@ class _ScriptRun:
async def _async_device_step(self):
"""Perform the device automation specified in the action."""
self._script.last_action = self._action.get(CONF_ALIAS, "device automation")
self._log("Executing step %s", self._script.last_action)
self._step_log("device automation")
platform = await device_automation.async_get_device_automation_platform(
self._hass, self._action[CONF_DOMAIN], "action"
)
@@ -478,8 +477,7 @@ class _ScriptRun:
async def _async_scene_step(self):
"""Activate the scene specified in the action."""
self._script.last_action = self._action.get(CONF_ALIAS, "activate scene")
self._log("Executing step %s", self._script.last_action)
self._step_log("activate scene")
await self._hass.services.async_call(
scene.DOMAIN,
SERVICE_TURN_ON,
@@ -490,10 +488,7 @@ class _ScriptRun:
async def _async_event_step(self):
"""Fire an event."""
self._script.last_action = self._action.get(
CONF_ALIAS, self._action[CONF_EVENT]
)
self._log("Executing step %s", self._script.last_action)
self._step_log(self._action.get(CONF_ALIAS, self._action[CONF_EVENT]))
event_data = {}
for conf in [CONF_EVENT_DATA, CONF_EVENT_DATA_TEMPLATE]:
if conf not in self._action:
@@ -627,25 +622,20 @@ class _ScriptRun:
async def _async_wait_for_trigger_step(self):
"""Wait for a trigger event."""
if CONF_TIMEOUT in self._action:
delay = self._get_pos_time_period_template(CONF_TIMEOUT).total_seconds()
timeout = self._get_pos_time_period_template(CONF_TIMEOUT).total_seconds()
else:
delay = None
timeout = None
self._script.last_action = self._action.get(CONF_ALIAS, "wait for trigger")
self._log(
"Executing step %s%s",
self._script.last_action,
"" if delay is None else f" (timeout: {timedelta(seconds=delay)})",
)
self._step_log("wait for trigger", timeout)
variables = {**self._variables}
self._variables["wait"] = {"remaining": delay, "trigger": None}
self._variables["wait"] = {"remaining": timeout, "trigger": None}
done = asyncio.Event()
async def async_done(variables, context=None):
self._variables["wait"] = {
"remaining": to_context.remaining if to_context else delay,
"remaining": to_context.remaining if to_context else timeout,
"trigger": variables["trigger"],
}
done.set()
@@ -671,7 +661,7 @@ class _ScriptRun:
self._hass.async_create_task(flag.wait()) for flag in (self._stop, done)
]
try:
async with timeout(delay) as to_context:
async with async_timeout.timeout(timeout) as to_context:
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
except asyncio.TimeoutError as ex:
if not self._action.get(CONF_CONTINUE_ON_TIMEOUT, True):
@@ -685,8 +675,7 @@ class _ScriptRun:
async def _async_variables_step(self):
"""Set a variable value."""
self._script.last_action = self._action.get(CONF_ALIAS, "setting variables")
self._log("Executing step %s", self._script.last_action)
self._step_log("setting variables")
self._variables = self._action[CONF_VARIABLES].async_render(
self._hass, self._variables, render_as_defaults=False
)
@@ -1111,10 +1100,11 @@ class Script:
await self._async_get_condition(config)
for config in choice.get(CONF_CONDITIONS, [])
]
choice_name = choice.get(CONF_ALIAS, f"choice {idx}")
sub_script = Script(
self._hass,
choice[CONF_SEQUENCE],
f"{self.name}: {step_name}: choice {idx}",
f"{self.name}: {step_name}: {choice_name}",
self.domain,
running_description=self.running_description,
script_mode=SCRIPT_MODE_PARALLEL,