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

@ -5764,8 +5764,9 @@ async def test_continue_on_error_unknown_error(hass: HomeAssistant) -> None:
)
@pytest.mark.parametrize("enabled_value", [False, "{{ 1 == 9 }}"])
async def test_disabled_actions(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, enabled_value: bool | str
) -> None:
"""Test disabled action steps."""
events = async_capture_events(hass, "test_event")
@ -5782,10 +5783,14 @@ async def test_disabled_actions(
{"event": "test_event"},
{
"alias": "Hello",
"enabled": False,
"enabled": enabled_value,
"service": "broken.service",
},
{"alias": "World", "enabled": False, "event": "test_event"},
{
"alias": "World",
"enabled": enabled_value,
"event": "test_event",
},
{"event": "test_event"},
]
)
@ -5807,6 +5812,37 @@ 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."""
await async_setup_component(hass, "homeassistant", {})
event = "test_event"
events = async_capture_events(hass, event)
sequence = cv.SCRIPT_SCHEMA(
[
{
"event": event,
"enabled": "{{ states('sensor.limited') }}",
}
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(exceptions.TemplateError):
await script_obj.async_run(context=Context())
assert len(events) == 0
assert not script_obj.is_running
expected_trace = {
"0": [
{
"error": "TemplateError: Use of 'states' is not supported in limited templates"
}
],
}
assert_action_trace(expected_trace, expected_script_execution="error")
async def test_condition_and_shorthand(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: