Allow templates for enabling actions (#117049)

* Allow templates for enabling automation actions

* Use `cv.template` instead of `cv.template_complex`

* Rename test function
This commit is contained in:
Matthias Alphart 2024-05-15 21:03:52 +02:00 committed by GitHub
parent 076f57ee07
commit ec4c8ae228
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 10 deletions

View file

@ -89,6 +89,7 @@ from .condition import ConditionCheckerType, trace_condition_function
from .dispatcher import async_dispatcher_connect, async_dispatcher_send_internal
from .event import async_call_later, async_track_template
from .script_variables import ScriptVariables
from .template import Template
from .trace import (
TraceElement,
async_trace_path,
@ -500,12 +501,24 @@ class _ScriptRun:
action = cv.determine_script_action(self._action)
if not self._action.get(CONF_ENABLED, True):
self._log(
"Skipped disabled step %s", self._action.get(CONF_ALIAS, action)
)
trace_set_result(enabled=False)
return
if CONF_ENABLED in self._action:
enabled = self._action[CONF_ENABLED]
if isinstance(enabled, Template):
try:
enabled = enabled.async_render(limited=True)
except exceptions.TemplateError as ex:
self._handle_exception(
ex,
continue_on_error,
self._log_exceptions or log_exceptions,
)
if not enabled:
self._log(
"Skipped disabled step %s",
self._action.get(CONF_ALIAS, action),
)
trace_set_result(enabled=False)
return
handler = f"_async_{action}_step"
try: