2021-03-22 18:19:38 +00:00
|
|
|
"""Test Trace websocket API."""
|
2021-03-24 16:56:22 +00:00
|
|
|
import pytest
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2021-03-23 21:53:38 +00:00
|
|
|
from homeassistant.components.trace.const import STORED_TRACES
|
2021-03-12 07:12:26 +00:00
|
|
|
from homeassistant.core import Context
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
from tests.common import assert_lists_same
|
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
def _find_run_id(traces, trace_type, item_id):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Find newest run_id for an script or automation."""
|
2021-03-15 23:51:04 +00:00
|
|
|
for trace in reversed(traces):
|
2021-03-24 16:56:22 +00:00
|
|
|
if trace["domain"] == trace_type and trace["item_id"] == item_id:
|
2021-03-15 23:51:04 +00:00
|
|
|
return trace["run_id"]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
def _find_traces(traces, trace_type, item_id):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Find traces for an script or automation."""
|
2021-03-24 16:56:22 +00:00
|
|
|
return [
|
|
|
|
trace
|
|
|
|
for trace in traces
|
|
|
|
if trace["domain"] == trace_type and trace["item_id"] == item_id
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_get_trace(hass, hass_ws_client, domain, prefix):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Test tracing an script or automation."""
|
2021-03-10 22:42:13 +00:00
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"service": "test.automation"},
|
|
|
|
}
|
|
|
|
moon_config = {
|
|
|
|
"id": "moon",
|
|
|
|
"trigger": [
|
|
|
|
{"platform": "event", "event_type": "test_event2"},
|
|
|
|
{"platform": "event", "event_type": "test_event3"},
|
|
|
|
],
|
|
|
|
"condition": {
|
|
|
|
"condition": "template",
|
|
|
|
"value_template": "{{ trigger.event.event_type=='test_event2' }}",
|
|
|
|
},
|
|
|
|
"action": {"event": "another_event"},
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
|
|
|
moon_config = {"sequence": moon_config["action"]}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-22 12:15:45 +00:00
|
|
|
sun_action = {
|
|
|
|
"limit": 10,
|
|
|
|
"params": {
|
|
|
|
"domain": "test",
|
|
|
|
"service": "automation",
|
|
|
|
"service_data": {},
|
|
|
|
"target": {},
|
|
|
|
},
|
|
|
|
"running_script": False,
|
|
|
|
}
|
|
|
|
moon_action = {"event": "another_event", "event_data": {}}
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: [sun_config, moon_config]}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: {"sun": sun_config, "moon": moon_config}}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
2021-03-16 21:37:26 +00:00
|
|
|
contexts = {}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
2021-03-12 07:12:26 +00:00
|
|
|
context = Context()
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event", context=context)
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun", context=context)
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
run_id = _find_run_id(response["result"], domain, "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Get trace
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/get",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
trace = response["result"]
|
2021-03-29 21:06:49 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert len(trace["trace"]) == 2
|
2021-03-31 12:56:04 +00:00
|
|
|
assert set(trace["trace"]) == {"trigger/0", f"{prefix}/0"}
|
2021-03-29 21:06:49 +00:00
|
|
|
else:
|
|
|
|
assert len(trace["trace"]) == 1
|
|
|
|
assert set(trace["trace"]) == {f"{prefix}/0"}
|
|
|
|
assert len(trace["trace"][f"{prefix}/0"]) == 1
|
|
|
|
assert trace["trace"][f"{prefix}/0"][0]["error"]
|
|
|
|
assert trace["trace"][f"{prefix}/0"][0]["result"] == sun_action
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["config"] == sun_config
|
|
|
|
assert trace["context"]
|
|
|
|
assert trace["error"] == "Unable to find service test.automation"
|
|
|
|
assert trace["state"] == "stopped"
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "sun"
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert trace["context"]["parent_id"] == context.id
|
|
|
|
assert trace["trigger"] == "event 'test_event'"
|
|
|
|
else:
|
|
|
|
assert trace["context"]["id"] == context.id
|
2021-03-16 21:37:26 +00:00
|
|
|
contexts[trace["context"]["id"]] = {
|
|
|
|
"run_id": trace["run_id"],
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": trace["item_id"],
|
2021-03-16 21:37:26 +00:00
|
|
|
}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" automation, with passing condition / run "moon" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
run_id = _find_run_id(response["result"], domain, "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Get trace
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/get",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "moon",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
trace = response["result"]
|
2021-03-29 21:06:49 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert len(trace["trace"]) == 3
|
2021-03-31 12:56:04 +00:00
|
|
|
assert set(trace["trace"]) == {"trigger/0", "condition/0", f"{prefix}/0"}
|
2021-03-29 21:06:49 +00:00
|
|
|
else:
|
|
|
|
assert len(trace["trace"]) == 1
|
|
|
|
assert set(trace["trace"]) == {f"{prefix}/0"}
|
|
|
|
assert len(trace["trace"][f"{prefix}/0"]) == 1
|
|
|
|
assert "error" not in trace["trace"][f"{prefix}/0"][0]
|
|
|
|
assert trace["trace"][f"{prefix}/0"][0]["result"] == moon_action
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["config"] == moon_config
|
|
|
|
assert trace["context"]
|
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-24 16:56:22 +00:00
|
|
|
|
|
|
|
if domain == "automation":
|
2021-03-29 21:06:49 +00:00
|
|
|
assert len(trace["trace"]["condition/0"]) == 1
|
|
|
|
assert trace["trace"]["condition/0"][0]["result"] == {"result": True}
|
2021-03-24 16:56:22 +00:00
|
|
|
assert trace["trigger"] == "event 'test_event2'"
|
2021-03-16 21:37:26 +00:00
|
|
|
contexts[trace["context"]["id"]] = {
|
|
|
|
"run_id": trace["run_id"],
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": trace["item_id"],
|
2021-03-16 21:37:26 +00:00
|
|
|
}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
# Check contexts
|
|
|
|
await client.send_json({"id": next_id(), "type": "trace/contexts"})
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert response["result"] == contexts
|
|
|
|
return
|
|
|
|
|
|
|
|
# Trigger "moon" automation with failing condition
|
2021-03-10 22:42:13 +00:00
|
|
|
hass.bus.async_fire("test_event3")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
run_id = _find_run_id(response["result"], "automation", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Get trace
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/get",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "moon",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
trace = response["result"]
|
2021-03-31 12:56:04 +00:00
|
|
|
assert set(trace["trace"]) == {"trigger/1", "condition/0"}
|
2021-03-29 21:06:49 +00:00
|
|
|
assert len(trace["trace"]["condition/0"]) == 1
|
|
|
|
assert trace["trace"]["condition/0"][0]["result"] == {"result": False}
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["config"] == moon_config
|
|
|
|
assert trace["context"]
|
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["trigger"] == "event 'test_event3'"
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-16 21:37:26 +00:00
|
|
|
contexts[trace["context"]["id"]] = {
|
|
|
|
"run_id": trace["run_id"],
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": trace["item_id"],
|
2021-03-16 21:37:26 +00:00
|
|
|
}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" automation with passing condition
|
2021-03-10 22:42:13 +00:00
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
run_id = _find_run_id(response["result"], "automation", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Get trace
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/get",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "moon",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
trace = response["result"]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert len(trace["trace"]) == 3
|
2021-03-31 12:56:04 +00:00
|
|
|
assert set(trace["trace"]) == {"trigger/0", "condition/0", f"{prefix}/0"}
|
2021-03-29 21:06:49 +00:00
|
|
|
assert len(trace["trace"][f"{prefix}/0"]) == 1
|
|
|
|
assert "error" not in trace["trace"][f"{prefix}/0"][0]
|
|
|
|
assert trace["trace"][f"{prefix}/0"][0]["result"] == moon_action
|
|
|
|
assert len(trace["trace"]["condition/0"]) == 1
|
|
|
|
assert trace["trace"]["condition/0"][0]["result"] == {"result": True}
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["config"] == moon_config
|
|
|
|
assert trace["context"]
|
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["trigger"] == "event 'test_event2'"
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-16 21:37:26 +00:00
|
|
|
contexts[trace["context"]["id"]] = {
|
|
|
|
"run_id": trace["run_id"],
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": trace["item_id"],
|
2021-03-16 21:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check contexts
|
2021-03-23 21:53:38 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/contexts"})
|
2021-03-16 21:37:26 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert response["result"] == contexts
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
@pytest.mark.parametrize("domain", ["automation", "script"])
|
|
|
|
async def test_trace_overflow(hass, hass_ws_client, domain):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Test the number of stored traces per script or automation is limited."""
|
2021-03-10 22:42:13 +00:00
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"event": "some_event"},
|
|
|
|
}
|
|
|
|
moon_config = {
|
|
|
|
"id": "moon",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event2"},
|
|
|
|
"action": {"event": "another_event"},
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
|
|
|
moon_config = {"sequence": moon_config["action"]}
|
|
|
|
|
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: [sun_config, moon_config]}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: {"sun": sun_config, "moon": moon_config}}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-15 23:51:04 +00:00
|
|
|
assert response["result"] == []
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" and "moon" automation / script once
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
assert len(_find_traces(response["result"], domain, "moon")) == 1
|
|
|
|
moon_run_id = _find_run_id(response["result"], domain, "moon")
|
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" enough times to overflow the max number of stored traces
|
2021-03-23 21:53:38 +00:00
|
|
|
for _ in range(STORED_TRACES):
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
moon_traces = _find_traces(response["result"], domain, "moon")
|
2021-03-23 21:53:38 +00:00
|
|
|
assert len(moon_traces) == STORED_TRACES
|
2021-03-15 23:51:04 +00:00
|
|
|
assert moon_traces[0]
|
|
|
|
assert int(moon_traces[0]["run_id"]) == int(moon_run_id) + 1
|
2021-03-23 21:53:38 +00:00
|
|
|
assert int(moon_traces[-1]["run_id"]) == int(moon_run_id) + STORED_TRACES
|
2021-03-24 16:56:22 +00:00
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_list_traces(hass, hass_ws_client, domain, prefix):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Test listing script and automation traces."""
|
2021-03-10 22:42:13 +00:00
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"service": "test.automation"},
|
|
|
|
}
|
|
|
|
moon_config = {
|
|
|
|
"id": "moon",
|
|
|
|
"trigger": [
|
|
|
|
{"platform": "event", "event_type": "test_event2"},
|
|
|
|
{"platform": "event", "event_type": "test_event3"},
|
|
|
|
],
|
|
|
|
"condition": {
|
|
|
|
"condition": "template",
|
|
|
|
"value_template": "{{ trigger.event.event_type=='test_event2' }}",
|
|
|
|
},
|
|
|
|
"action": {"event": "another_event"},
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
|
|
|
moon_config = {"sequence": moon_config["action"]}
|
|
|
|
|
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: [sun_config, moon_config]}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: {"sun": sun_config, "moon": moon_config}}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-15 23:51:04 +00:00
|
|
|
assert response["result"] == []
|
|
|
|
|
|
|
|
await client.send_json(
|
2021-03-24 16:56:22 +00:00
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain, "item_id": "sun"}
|
2021-03-15 23:51:04 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert response["result"] == []
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Get trace
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-15 23:51:04 +00:00
|
|
|
assert len(response["result"]) == 1
|
2021-03-24 16:56:22 +00:00
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
2021-03-15 23:51:04 +00:00
|
|
|
|
|
|
|
await client.send_json(
|
2021-03-24 16:56:22 +00:00
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain, "item_id": "sun"}
|
2021-03-15 23:51:04 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert len(response["result"]) == 1
|
2021-03-24 16:56:22 +00:00
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
2021-03-15 23:51:04 +00:00
|
|
|
|
|
|
|
await client.send_json(
|
2021-03-24 16:56:22 +00:00
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain, "item_id": "moon"}
|
2021-03-15 23:51:04 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert response["result"] == []
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" automation, with passing condition / run "moon" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" automation, with failing condition / run "moon" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event3")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "moon" automation, with passing condition / run "moon" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event2")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "moon")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Get trace
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": domain})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
assert len(_find_traces(response["result"], domain, "moon")) == 3
|
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
|
|
|
trace = _find_traces(response["result"], domain, "sun")[0]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == f"{prefix}/0"
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["error"] == "Unable to find service test.automation"
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["timestamp"]
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "sun"
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert trace["trigger"] == "event 'test_event'"
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
trace = _find_traces(response["result"], domain, "moon")[0]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == f"{prefix}/0"
|
2021-03-10 22:42:13 +00:00
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["timestamp"]
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert trace["trigger"] == "event 'test_event2'"
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
trace = _find_traces(response["result"], domain, "moon")[1]
|
2021-03-10 22:42:13 +00:00
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["timestamp"]
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == "condition/0"
|
2021-03-24 16:56:22 +00:00
|
|
|
assert trace["trigger"] == "event 'test_event3'"
|
|
|
|
else:
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == f"{prefix}/0"
|
2021-03-24 16:56:22 +00:00
|
|
|
|
|
|
|
trace = _find_traces(response["result"], domain, "moon")[2]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == f"{prefix}/0"
|
2021-03-10 22:42:13 +00:00
|
|
|
assert "error" not in trace
|
|
|
|
assert trace["state"] == "stopped"
|
|
|
|
assert trace["timestamp"]
|
2021-03-23 21:53:38 +00:00
|
|
|
assert trace["item_id"] == "moon"
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert trace["trigger"] == "event 'test_event2'"
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-26 17:14:01 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_nested_traces(hass, hass_ws_client, domain, prefix):
|
|
|
|
"""Test nested automation and script traces."""
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": {"service": "script.moon"},
|
|
|
|
}
|
|
|
|
moon_config = {
|
|
|
|
"sequence": {"event": "another_event"},
|
|
|
|
}
|
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
|
|
|
|
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(hass, domain, {domain: [sun_config]})
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, "script", {"script": {"moon": moon_config}}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, domain, {domain: {"sun": sun_config, "moon": moon_config}}
|
|
|
|
)
|
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# List traces
|
2021-03-29 08:06:22 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/list", "domain": "script"})
|
2021-03-26 17:14:01 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-29 08:06:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert len(response["result"]) == 1
|
|
|
|
else:
|
|
|
|
assert len(response["result"]) == 2
|
2021-03-26 17:14:01 +00:00
|
|
|
assert len(_find_traces(response["result"], "script", "moon")) == 1
|
|
|
|
moon_run_id = _find_run_id(response["result"], "script", "moon")
|
2021-03-29 08:06:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
await client.send_json(
|
|
|
|
{"id": next_id(), "type": "trace/list", "domain": "automation"}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert len(response["result"]) == 1
|
|
|
|
assert len(_find_traces(response["result"], domain, "sun")) == 1
|
|
|
|
sun_run_id = _find_run_id(response["result"], domain, "sun")
|
2021-03-26 17:14:01 +00:00
|
|
|
assert sun_run_id != moon_run_id
|
|
|
|
|
|
|
|
# Get trace
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
|
|
|
"type": "trace/get",
|
|
|
|
"domain": domain,
|
|
|
|
"item_id": "sun",
|
|
|
|
"run_id": sun_run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
trace = response["result"]
|
2021-03-29 21:06:49 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert len(trace["trace"]) == 2
|
2021-03-31 12:56:04 +00:00
|
|
|
assert set(trace["trace"]) == {"trigger/0", f"{prefix}/0"}
|
2021-03-29 21:06:49 +00:00
|
|
|
else:
|
|
|
|
assert len(trace["trace"]) == 1
|
|
|
|
assert set(trace["trace"]) == {f"{prefix}/0"}
|
|
|
|
assert len(trace["trace"][f"{prefix}/0"]) == 1
|
|
|
|
child_id = trace["trace"][f"{prefix}/0"][0]["child_id"]
|
2021-03-26 17:14:01 +00:00
|
|
|
assert child_id == {"domain": "script", "item_id": "moon", "run_id": moon_run_id}
|
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_breakpoints(hass, hass_ws_client, domain, prefix):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Test script and automation breakpoints."""
|
2021-03-10 22:42:13 +00:00
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
2021-03-29 21:06:49 +00:00
|
|
|
async def assert_last_step(item_id, expected_action, expected_state):
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json(
|
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
trace = _find_traces(response["result"], domain, item_id)[-1]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == expected_action
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["state"] == expected_state
|
|
|
|
return trace["run_id"]
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": [
|
|
|
|
{"event": "event0"},
|
|
|
|
{"event": "event1"},
|
|
|
|
{"event": "event2"},
|
|
|
|
{"event": "event3"},
|
|
|
|
{"event": "event4"},
|
|
|
|
{"event": "event5"},
|
|
|
|
{"event": "event6"},
|
|
|
|
{"event": "event7"},
|
|
|
|
{"event": "event8"},
|
|
|
|
],
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(hass, domain, {domain: [sun_config]})
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(hass, domain, {domain: {"sun": sun_config}})
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"node": "1",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert not response["success"]
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert response["result"] == []
|
|
|
|
|
|
|
|
subscription_id = next_id()
|
|
|
|
await client.send_json(
|
2021-03-23 21:53:38 +00:00
|
|
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
2021-03-10 22:42:13 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/5",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"})
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
assert_lists_same(
|
|
|
|
response["result"],
|
|
|
|
[
|
2021-03-24 16:56:22 +00:00
|
|
|
{"node": f"{prefix}/1", "run_id": "*", "domain": domain, "item_id": "sun"},
|
|
|
|
{"node": f"{prefix}/5", "run_id": "*", "domain": domain, "item_id": "sun"},
|
2021-03-10 22:42:13 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/1", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/step",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/2", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/2",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/continue",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/5", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/5",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/stop",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
await hass.async_block_till_done()
|
2021-03-29 21:06:49 +00:00
|
|
|
await assert_last_step("sun", f"{prefix}/5", "stopped")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_breakpoints_2(hass, hass_ws_client, domain, prefix):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""Test execution resumes and breakpoints are removed after subscription removed."""
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
2021-03-29 21:06:49 +00:00
|
|
|
async def assert_last_step(item_id, expected_action, expected_state):
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json(
|
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
trace = _find_traces(response["result"], domain, item_id)[-1]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == expected_action
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["state"] == expected_state
|
|
|
|
return trace["run_id"]
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": [
|
|
|
|
{"event": "event0"},
|
|
|
|
{"event": "event1"},
|
|
|
|
{"event": "event2"},
|
|
|
|
{"event": "event3"},
|
|
|
|
{"event": "event4"},
|
|
|
|
{"event": "event5"},
|
|
|
|
{"event": "event6"},
|
|
|
|
{"event": "event7"},
|
|
|
|
{"event": "event8"},
|
|
|
|
],
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(hass, domain, {domain: [sun_config]})
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(hass, domain, {domain: {"sun": sun_config}})
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
|
|
|
subscription_id = next_id()
|
|
|
|
await client.send_json(
|
2021-03-23 21:53:38 +00:00
|
|
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
2021-03-10 22:42:13 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/1", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Unsubscribe - execution should resume
|
|
|
|
await client.send_json(
|
|
|
|
{"id": next_id(), "type": "unsubscribe_events", "subscription": subscription_id}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
await hass.async_block_till_done()
|
2021-03-29 21:06:49 +00:00
|
|
|
await assert_last_step("sun", f"{prefix}/8", "stopped")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Should not be possible to set breakpoints
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"node": "1",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert not response["success"]
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / script, should finish without stopping on breakpoints
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-03-29 21:06:49 +00:00
|
|
|
new_run_id = await assert_last_step("sun", f"{prefix}/8", "stopped")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert new_run_id != run_id
|
|
|
|
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"domain, prefix", [("automation", "action"), ("script", "sequence")]
|
|
|
|
)
|
|
|
|
async def test_breakpoints_3(hass, hass_ws_client, domain, prefix):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""Test breakpoints can be cleared."""
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
def next_id():
|
|
|
|
nonlocal id
|
|
|
|
id += 1
|
|
|
|
return id
|
|
|
|
|
2021-03-29 21:06:49 +00:00
|
|
|
async def assert_last_step(item_id, expected_action, expected_state):
|
2021-03-29 06:09:14 +00:00
|
|
|
await client.send_json(
|
|
|
|
{"id": next_id(), "type": "trace/list", "domain": domain}
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
2021-03-24 16:56:22 +00:00
|
|
|
trace = _find_traces(response["result"], domain, item_id)[-1]
|
2021-03-29 21:06:49 +00:00
|
|
|
assert trace["last_step"] == expected_action
|
2021-03-10 22:42:13 +00:00
|
|
|
assert trace["state"] == expected_state
|
|
|
|
return trace["run_id"]
|
|
|
|
|
|
|
|
sun_config = {
|
|
|
|
"id": "sun",
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event"},
|
|
|
|
"action": [
|
|
|
|
{"event": "event0"},
|
|
|
|
{"event": "event1"},
|
|
|
|
{"event": "event2"},
|
|
|
|
{"event": "event3"},
|
|
|
|
{"event": "event4"},
|
|
|
|
{"event": "event5"},
|
|
|
|
{"event": "event6"},
|
|
|
|
{"event": "event7"},
|
|
|
|
{"event": "event8"},
|
|
|
|
],
|
|
|
|
}
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "script":
|
|
|
|
sun_config = {"sequence": sun_config["action"]}
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
if domain == "automation":
|
|
|
|
assert await async_setup_component(hass, domain, {domain: [sun_config]})
|
|
|
|
else:
|
|
|
|
assert await async_setup_component(hass, domain, {domain: {"sun": sun_config}})
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client()
|
|
|
|
|
|
|
|
subscription_id = next_id()
|
|
|
|
await client.send_json(
|
2021-03-23 21:53:38 +00:00
|
|
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
2021-03-10 22:42:13 +00:00
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/set",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/5",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/1", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/continue",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/5", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/5",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/stop",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
await hass.async_block_till_done()
|
2021-03-29 21:06:49 +00:00
|
|
|
await assert_last_step("sun", f"{prefix}/5", "stopped")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# Clear 1st breakpoint
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": next_id(),
|
2021-03-23 21:53:38 +00:00
|
|
|
"type": "trace/debug/breakpoint/clear",
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/1",
|
2021-03-10 22:42:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await client.receive_json()
|
|
|
|
assert response["success"]
|
|
|
|
|
2021-03-24 16:56:22 +00:00
|
|
|
# Trigger "sun" automation / run "sun" script
|
|
|
|
if domain == "automation":
|
|
|
|
hass.bus.async_fire("test_event")
|
|
|
|
else:
|
|
|
|
await hass.services.async_call("script", "sun")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
response = await client.receive_json()
|
2021-03-29 21:06:49 +00:00
|
|
|
run_id = await assert_last_step("sun", f"{prefix}/5", "running")
|
2021-03-10 22:42:13 +00:00
|
|
|
assert response["event"] == {
|
2021-03-24 16:56:22 +00:00
|
|
|
"domain": domain,
|
2021-03-23 21:53:38 +00:00
|
|
|
"item_id": "sun",
|
2021-03-24 16:56:22 +00:00
|
|
|
"node": f"{prefix}/5",
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
}
|