Allow full templates in the enabled option of script steps (#187013)

This commit is contained in:
Franck Nijhof
2026-10-18 21:45:14 -04:00
committed by GitHub
parent 29c64760cc
commit 4b864ebbc7
2 changed files with 136 additions and 7 deletions
+6 -2
View File
@@ -564,7 +564,7 @@ class _ScriptRun:
enabled = self._action[CONF_ENABLED]
if isinstance(enabled, Template):
try:
enabled = enabled.async_render(limited=True)
enabled = enabled.async_render(self._variables)
except exceptions.TemplateError as ex:
self._handle_exception(
trace_element,
@@ -803,7 +803,11 @@ class _ScriptRun:
self._script.last_action = self._action.get(
CONF_ALIAS, self._action[CONF_CONDITION]
)
cond = await self._async_get_condition(self._action)
# The enabled option was already rendered with the run variables when
# this step started, don't let the condition render it again.
cond = await self._async_get_condition(
{key: value for key, value in self._action.items() if key != CONF_ENABLED}
)
try:
trace_element = trace_stack_top(trace_stack_cv)
if trace_element:
+130 -5
View File
@@ -6693,20 +6693,143 @@ async def test_disabled_actions(
)
async def test_enabled_error_non_limited_template(hass: HomeAssistant) -> None:
"""Test that a script aborts when an action enabled uses non-limited template."""
async def test_enabled_template_uses_state(hass: HomeAssistant) -> None:
"""Test an action enabled template is rendered with full access to states."""
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA(
[
{
"event": event,
"enabled": "{{ states('sensor.limited') }}",
"enabled": "{{ states('input_boolean.run_step') == 'on' }}",
}
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
hass.states.async_set("input_boolean.run_step", "off")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()
assert len(events) == 0
assert_action_trace({"0": [{"result": {"enabled": False}}]})
# The template is rendered again on each run, so a state change is picked up
hass.states.async_set("input_boolean.run_step", "on")
await script_obj.async_run(context=Context())
await hass.async_block_till_done()
assert len(events) == 1
assert_action_trace(
{"0": [{"result": {"event": event, "event_data": {}}}]},
)
async def test_enabled_template_uses_variables(hass: HomeAssistant) -> None:
"""Test an action enabled template is rendered with the run variables."""
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA(
[
{"variables": {"from_sequence": True}},
{
"alias": "Sequence variable",
"event": event,
"event_data": {"step": "sequence"},
"enabled": "{{ from_sequence }}",
},
{
"alias": "Run variable",
"event": event,
"event_data": {"step": "run"},
"enabled": "{{ from_run }}",
},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(run_variables={"from_run": False}, context=Context())
await hass.async_block_till_done()
assert [event.data["step"] for event in events] == ["sequence"]
await script_obj.async_run(run_variables={"from_run": True}, context=Context())
await hass.async_block_till_done()
assert [event.data["step"] for event in events] == ["sequence", "sequence", "run"]
async def test_enabled_template_on_condition_step(hass: HomeAssistant) -> None:
"""Test the enabled template of a condition step uses states and variables."""
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA(
[
{
"condition": "template",
"value_template": "{{ condition_passes }}",
"enabled": "{{ check_enabled and is_state('input_boolean.check', 'on') }}",
},
{"event": event},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
hass.states.async_set("input_boolean.check", "on")
# Enabled and passing, so the run continues
await script_obj.async_run(
run_variables={"check_enabled": True, "condition_passes": True},
context=Context(),
)
await hass.async_block_till_done()
assert len(events) == 1
# Enabled and failing, so the run stops
await script_obj.async_run(
run_variables={"check_enabled": True, "condition_passes": False},
context=Context(),
)
await hass.async_block_till_done()
assert len(events) == 1
# Disabled by a variable, so the failing condition is skipped
await script_obj.async_run(
run_variables={"check_enabled": False, "condition_passes": False},
context=Context(),
)
await hass.async_block_till_done()
assert len(events) == 2
# Disabled by a state change, so the failing condition is skipped
hass.states.async_set("input_boolean.check", "off")
await script_obj.async_run(
run_variables={"check_enabled": True, "condition_passes": False},
context=Context(),
)
await hass.async_block_till_done()
assert len(events) == 3
async def test_enabled_template_error(hass: HomeAssistant) -> None:
"""Test that a script aborts when an action enabled template fails to render."""
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA(
[
{
"event": event,
"enabled": "{{ states('sensor.missing') | float }}",
"continue_on_error": True,
},
{"event": event},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(exceptions.TemplateError):
await script_obj.async_run(context=Context())
@@ -6717,8 +6840,10 @@ async def test_enabled_error_non_limited_template(hass: HomeAssistant) -> None:
"0": [
{
"error": (
"TemplateError: Use of 'states' is not"
" supported in limited templates"
"ValueError: Template error: float got"
" invalid input 'unknown' when rendering template"
" '{{ states('sensor.missing') | float }}' but no default"
" was specified"
)
}
],