Add sequence action for automations & scripts (#117690)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Franck Nijhof
2024-05-24 09:34:49 +02:00
committed by GitHub
parent e70d8aec96
commit 9224997411
3 changed files with 148 additions and 5 deletions

View File

@@ -3538,6 +3538,103 @@ async def test_if_condition_validation(
)
async def test_sequence(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
"""Test sequence action."""
events = async_capture_events(hass, "test_event")
sequence = cv.SCRIPT_SCHEMA(
[
{
"alias": "Sequential group",
"sequence": [
{
"alias": "sequence group, action 1",
"event": "test_event",
"event_data": {
"sequence": "group",
"action": "1",
"what": "{{ what }}",
},
},
{
"alias": "sequence group, action 2",
"event": "test_event",
"event_data": {
"sequence": "group",
"action": "2",
"what": "{{ what }}",
},
},
],
},
{
"alias": "action 2",
"event": "test_event",
"event_data": {"action": "2", "what": "{{ what }}"},
},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
await script_obj.async_run(MappingProxyType({"what": "world"}), Context())
assert len(events) == 3
assert events[0].data == {
"sequence": "group",
"action": "1",
"what": "world",
}
assert events[1].data == {
"sequence": "group",
"action": "2",
"what": "world",
}
assert events[2].data == {
"action": "2",
"what": "world",
}
assert (
"Test Name: Sequential group: Executing step sequence group, action 1"
in caplog.text
)
assert (
"Test Name: Sequential group: Executing step sequence group, action 2"
in caplog.text
)
assert "Test Name: Executing step action 2" in caplog.text
expected_trace = {
"0": [{"variables": {"what": "world"}}],
"0/sequence/0": [
{
"result": {
"event": "test_event",
"event_data": {"sequence": "group", "action": "1", "what": "world"},
},
}
],
"0/sequence/1": [
{
"result": {
"event": "test_event",
"event_data": {"sequence": "group", "action": "2", "what": "world"},
},
}
],
"1": [
{
"result": {
"event": "test_event",
"event_data": {"action": "2", "what": "world"},
},
}
],
}
assert_action_trace(expected_trace)
async def test_parallel(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
"""Test parallel action."""
events = async_capture_events(hass, "test_event")
@@ -5167,6 +5264,9 @@ async def test_validate_action_config(
cv.SCRIPT_ACTION_PARALLEL: {
"parallel": [templated_device_action("parallel_event")],
},
cv.SCRIPT_ACTION_SEQUENCE: {
"sequence": [templated_device_action("sequence_event")],
},
cv.SCRIPT_ACTION_SET_CONVERSATION_RESPONSE: {
"set_conversation_response": "Hello world"
},
@@ -5179,6 +5279,7 @@ async def test_validate_action_config(
cv.SCRIPT_ACTION_WAIT_FOR_TRIGGER: None,
cv.SCRIPT_ACTION_IF: None,
cv.SCRIPT_ACTION_PARALLEL: None,
cv.SCRIPT_ACTION_SEQUENCE: None,
}
for key in cv.ACTION_TYPE_SCHEMAS: