Include script script_execution in script and automation traces (#48576)
This commit is contained in:
parent
9d085778c2
commit
9f481e1642
6 changed files with 228 additions and 20 deletions
|
@ -1,9 +1,11 @@
|
|||
"""Test Trace websocket API."""
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components.trace.const import STORED_TRACES
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.helpers.typing import UNDEFINED
|
||||
|
||||
from tests.common import assert_lists_same
|
||||
|
@ -170,6 +172,7 @@ async def test_get_trace(
|
|||
assert trace["context"]
|
||||
assert trace["error"] == "Unable to find service test.automation"
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == "error"
|
||||
assert trace["item_id"] == "sun"
|
||||
assert trace["context"][context_key] == context.id
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[0]
|
||||
|
@ -210,6 +213,7 @@ async def test_get_trace(
|
|||
assert trace["context"]
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == "finished"
|
||||
assert trace["item_id"] == "moon"
|
||||
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[1]
|
||||
|
@ -260,6 +264,7 @@ async def test_get_trace(
|
|||
assert trace["context"]
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == "failed_conditions"
|
||||
assert trace["trigger"] == "event 'test_event3'"
|
||||
assert trace["item_id"] == "moon"
|
||||
contexts[trace["context"]["id"]] = {
|
||||
|
@ -301,6 +306,7 @@ async def test_get_trace(
|
|||
assert trace["context"]
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == "finished"
|
||||
assert trace["trigger"] == "event 'test_event2'"
|
||||
assert trace["item_id"] == "moon"
|
||||
contexts[trace["context"]["id"]] = {
|
||||
|
@ -391,7 +397,7 @@ async def test_trace_overflow(hass, hass_ws_client, domain):
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"domain, prefix, trigger, last_step",
|
||||
"domain, prefix, trigger, last_step, script_execution",
|
||||
[
|
||||
(
|
||||
"automation",
|
||||
|
@ -403,16 +409,20 @@ async def test_trace_overflow(hass, hass_ws_client, domain):
|
|||
"event 'test_event2'",
|
||||
],
|
||||
["{prefix}/0", "{prefix}/0", "condition/0", "{prefix}/0"],
|
||||
["error", "finished", "failed_conditions", "finished"],
|
||||
),
|
||||
(
|
||||
"script",
|
||||
"sequence",
|
||||
[UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED],
|
||||
["{prefix}/0", "{prefix}/0", "{prefix}/0", "{prefix}/0"],
|
||||
["error", "finished", "finished", "finished"],
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_step):
|
||||
async def test_list_traces(
|
||||
hass, hass_ws_client, domain, prefix, trigger, last_step, script_execution
|
||||
):
|
||||
"""Test listing script and automation traces."""
|
||||
id = 1
|
||||
|
||||
|
@ -458,7 +468,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
await _run_automation_or_script(hass, domain, sun_config, "test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Get trace
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
|
@ -492,7 +502,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
await _run_automation_or_script(hass, domain, moon_config, "test_event2")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Get trace
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
|
@ -502,6 +512,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
assert trace["last_step"] == last_step[0].format(prefix=prefix)
|
||||
assert trace["error"] == "Unable to find service test.automation"
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == script_execution[0]
|
||||
assert trace["timestamp"]
|
||||
assert trace["item_id"] == "sun"
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[0]
|
||||
|
@ -510,6 +521,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
assert trace["last_step"] == last_step[1].format(prefix=prefix)
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == script_execution[1]
|
||||
assert trace["timestamp"]
|
||||
assert trace["item_id"] == "moon"
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[1]
|
||||
|
@ -518,6 +530,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
assert trace["last_step"] == last_step[2].format(prefix=prefix)
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == script_execution[2]
|
||||
assert trace["timestamp"]
|
||||
assert trace["item_id"] == "moon"
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[2]
|
||||
|
@ -526,6 +539,7 @@ async def test_list_traces(hass, hass_ws_client, domain, prefix, trigger, last_s
|
|||
assert trace["last_step"] == last_step[3].format(prefix=prefix)
|
||||
assert "error" not in trace
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == script_execution[3]
|
||||
assert trace["timestamp"]
|
||||
assert trace["item_id"] == "moon"
|
||||
assert trace.get("trigger", UNDEFINED) == trigger[3]
|
||||
|
@ -1006,3 +1020,145 @@ async def test_breakpoints_3(hass, hass_ws_client, domain, prefix):
|
|||
"node": f"{prefix}/5",
|
||||
"run_id": run_id,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script_mode,max_runs,script_execution",
|
||||
[
|
||||
({"mode": "single"}, 1, "failed_single"),
|
||||
({"mode": "parallel", "max": 2}, 2, "failed_max_runs"),
|
||||
],
|
||||
)
|
||||
async def test_script_mode(
|
||||
hass, hass_ws_client, script_mode, max_runs, script_execution
|
||||
):
|
||||
"""Test overlapping runs with max_runs > 1."""
|
||||
id = 1
|
||||
|
||||
def next_id():
|
||||
nonlocal id
|
||||
id += 1
|
||||
return id
|
||||
|
||||
flag = asyncio.Event()
|
||||
|
||||
@callback
|
||||
def _handle_event(_):
|
||||
flag.set()
|
||||
|
||||
event = "test_event"
|
||||
script_config = {
|
||||
"script1": {
|
||||
"sequence": [
|
||||
{"event": event, "event_data": {"value": 1}},
|
||||
{"wait_template": "{{ states.switch.test.state == 'off' }}"},
|
||||
{"event": event, "event_data": {"value": 2}},
|
||||
],
|
||||
**script_mode,
|
||||
},
|
||||
}
|
||||
client = await hass_ws_client()
|
||||
hass.bus.async_listen(event, _handle_event)
|
||||
assert await async_setup_component(hass, "script", {"script": script_config})
|
||||
|
||||
for _ in range(max_runs):
|
||||
hass.states.async_set("switch.test", "on")
|
||||
await hass.services.async_call("script", "script1")
|
||||
await asyncio.wait_for(flag.wait(), 1)
|
||||
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
traces = _find_traces(response["result"], "script", "script1")
|
||||
assert len(traces) == max_runs
|
||||
for trace in traces:
|
||||
assert trace["state"] == "running"
|
||||
|
||||
# Start additional run of script while first runs are suspended in wait_template.
|
||||
|
||||
flag.clear()
|
||||
await hass.services.async_call("script", "script1")
|
||||
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
traces = _find_traces(response["result"], "script", "script1")
|
||||
assert len(traces) == max_runs + 1
|
||||
assert traces[-1]["state"] == "stopped"
|
||||
assert traces[-1]["script_execution"] == script_execution
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"script_mode,script_execution",
|
||||
[("restart", "cancelled"), ("parallel", "finished")],
|
||||
)
|
||||
async def test_script_mode_2(hass, hass_ws_client, script_mode, script_execution):
|
||||
"""Test overlapping runs with max_runs > 1."""
|
||||
id = 1
|
||||
|
||||
def next_id():
|
||||
nonlocal id
|
||||
id += 1
|
||||
return id
|
||||
|
||||
flag = asyncio.Event()
|
||||
|
||||
@callback
|
||||
def _handle_event(_):
|
||||
flag.set()
|
||||
|
||||
event = "test_event"
|
||||
script_config = {
|
||||
"script1": {
|
||||
"sequence": [
|
||||
{"event": event, "event_data": {"value": 1}},
|
||||
{"wait_template": "{{ states.switch.test.state == 'off' }}"},
|
||||
{"event": event, "event_data": {"value": 2}},
|
||||
],
|
||||
"mode": script_mode,
|
||||
}
|
||||
}
|
||||
client = await hass_ws_client()
|
||||
hass.bus.async_listen(event, _handle_event)
|
||||
assert await async_setup_component(hass, "script", {"script": script_config})
|
||||
|
||||
hass.states.async_set("switch.test", "on")
|
||||
await hass.services.async_call("script", "script1")
|
||||
await asyncio.wait_for(flag.wait(), 1)
|
||||
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
trace = _find_traces(response["result"], "script", "script1")[0]
|
||||
assert trace["state"] == "running"
|
||||
|
||||
# Start second run of script while first run is suspended in wait_template.
|
||||
|
||||
flag.clear()
|
||||
await hass.services.async_call("script", "script1")
|
||||
await asyncio.wait_for(flag.wait(), 1)
|
||||
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
trace = _find_traces(response["result"], "script", "script1")[1]
|
||||
assert trace["state"] == "running"
|
||||
|
||||
# Let both scripts finish
|
||||
hass.states.async_set("switch.test", "off")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# List traces
|
||||
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
trace = _find_traces(response["result"], "script", "script1")[0]
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == script_execution
|
||||
trace = _find_traces(response["result"], "script", "script1")[1]
|
||||
assert trace["state"] == "stopped"
|
||||
assert trace["script_execution"] == "finished"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue