mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Refactor tracing: Prepare for tracing of scripts (#48231)
This commit is contained in:
parent
195d4de6cd
commit
9f8b697e64
@ -23,7 +23,7 @@ class AutomationTrace:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
unique_id: str | None,
|
key: tuple[str, str],
|
||||||
config: dict[str, Any],
|
config: dict[str, Any],
|
||||||
context: Context,
|
context: Context,
|
||||||
):
|
):
|
||||||
@ -37,7 +37,7 @@ class AutomationTrace:
|
|||||||
self.run_id: str = str(next(self._run_ids))
|
self.run_id: str = str(next(self._run_ids))
|
||||||
self._timestamp_finish: dt.datetime | None = None
|
self._timestamp_finish: dt.datetime | None = None
|
||||||
self._timestamp_start: dt.datetime = dt_util.utcnow()
|
self._timestamp_start: dt.datetime = dt_util.utcnow()
|
||||||
self._unique_id: str | None = unique_id
|
self._key: tuple[str, str] = key
|
||||||
self._variables: dict[str, Any] | None = None
|
self._variables: dict[str, Any] | None = None
|
||||||
|
|
||||||
def set_action_trace(self, trace: dict[str, Deque[TraceElement]]) -> None:
|
def set_action_trace(self, trace: dict[str, Deque[TraceElement]]) -> None:
|
||||||
@ -104,7 +104,6 @@ class AutomationTrace:
|
|||||||
trigger = self._variables.get("trigger", {}).get("description")
|
trigger = self._variables.get("trigger", {}).get("description")
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"automation_id": self._unique_id,
|
|
||||||
"last_action": last_action,
|
"last_action": last_action,
|
||||||
"last_condition": last_condition,
|
"last_condition": last_condition,
|
||||||
"run_id": self.run_id,
|
"run_id": self.run_id,
|
||||||
@ -114,7 +113,8 @@ class AutomationTrace:
|
|||||||
"finish": self._timestamp_finish,
|
"finish": self._timestamp_finish,
|
||||||
},
|
},
|
||||||
"trigger": trigger,
|
"trigger": trigger,
|
||||||
"unique_id": self._unique_id,
|
"domain": self._key[0],
|
||||||
|
"item_id": self._key[1],
|
||||||
}
|
}
|
||||||
if self._error is not None:
|
if self._error is not None:
|
||||||
result["error"] = str(self._error)
|
result["error"] = str(self._error)
|
||||||
@ -126,23 +126,24 @@ class AutomationTrace:
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def trace_automation(hass, unique_id, config, context):
|
def trace_automation(hass, item_id, config, context):
|
||||||
"""Trace action execution of automation with automation_id."""
|
"""Trace action execution of automation with automation_id."""
|
||||||
automation_trace = AutomationTrace(unique_id, config, context)
|
key = ("automation", item_id)
|
||||||
trace_id_set((unique_id, automation_trace.run_id))
|
trace = AutomationTrace(key, config, context)
|
||||||
|
trace_id_set((key, trace.run_id))
|
||||||
|
|
||||||
if unique_id:
|
if key:
|
||||||
automation_traces = hass.data[DATA_TRACE]
|
traces = hass.data[DATA_TRACE]
|
||||||
if unique_id not in automation_traces:
|
if key not in traces:
|
||||||
automation_traces[unique_id] = LimitedSizeDict(size_limit=STORED_TRACES)
|
traces[key] = LimitedSizeDict(size_limit=STORED_TRACES)
|
||||||
automation_traces[unique_id][automation_trace.run_id] = automation_trace
|
traces[key][trace.run_id] = trace
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yield automation_trace
|
yield trace
|
||||||
except Exception as ex: # pylint: disable=broad-except
|
except Exception as ex: # pylint: disable=broad-except
|
||||||
if unique_id:
|
if key:
|
||||||
automation_trace.set_error(ex)
|
trace.set_error(ex)
|
||||||
raise ex
|
raise ex
|
||||||
finally:
|
finally:
|
||||||
if unique_id:
|
if key:
|
||||||
automation_trace.finished()
|
trace.finished()
|
||||||
|
@ -5,17 +5,17 @@ from .const import DATA_TRACE
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def get_debug_trace(hass, automation_id, run_id):
|
def get_debug_trace(hass, key, run_id):
|
||||||
"""Return a serializable debug trace."""
|
"""Return a serializable debug trace."""
|
||||||
return hass.data[DATA_TRACE][automation_id][run_id]
|
return hass.data[DATA_TRACE][key][run_id]
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def get_debug_traces_for_automation(hass, automation_id, summary=False):
|
def get_debug_traces(hass, key, summary=False):
|
||||||
"""Return a serializable list of debug traces for an automation."""
|
"""Return a serializable list of debug traces for an automation or script."""
|
||||||
traces = []
|
traces = []
|
||||||
|
|
||||||
for trace in hass.data[DATA_TRACE].get(automation_id, {}).values():
|
for trace in hass.data[DATA_TRACE].get(key, {}).values():
|
||||||
if summary:
|
if summary:
|
||||||
traces.append(trace.as_short_dict())
|
traces.append(trace.as_short_dict())
|
||||||
else:
|
else:
|
||||||
@ -25,11 +25,11 @@ def get_debug_traces_for_automation(hass, automation_id, summary=False):
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def get_debug_traces(hass, summary=False):
|
def get_all_debug_traces(hass, summary=False):
|
||||||
"""Return a serializable list of debug traces."""
|
"""Return a serializable list of debug traces for all automations and scripts."""
|
||||||
traces = []
|
traces = []
|
||||||
|
|
||||||
for automation_id in hass.data[DATA_TRACE]:
|
for key in hass.data[DATA_TRACE]:
|
||||||
traces.extend(get_debug_traces_for_automation(hass, automation_id, summary))
|
traces.extend(get_debug_traces(hass, key, summary))
|
||||||
|
|
||||||
return traces
|
return traces
|
||||||
|
@ -23,29 +23,26 @@ from homeassistant.helpers.script import (
|
|||||||
debug_stop,
|
debug_stop,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .trace import (
|
from .trace import DATA_TRACE, get_all_debug_traces, get_debug_trace, get_debug_traces
|
||||||
DATA_TRACE,
|
|
||||||
get_debug_trace,
|
|
||||||
get_debug_traces,
|
|
||||||
get_debug_traces_for_automation,
|
|
||||||
)
|
|
||||||
from .utils import TraceJSONEncoder
|
from .utils import TraceJSONEncoder
|
||||||
|
|
||||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||||
|
|
||||||
|
TRACE_DOMAINS = ["automation"]
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_setup(hass: HomeAssistant) -> None:
|
def async_setup(hass: HomeAssistant) -> None:
|
||||||
"""Set up the websocket API."""
|
"""Set up the websocket API."""
|
||||||
websocket_api.async_register_command(hass, websocket_automation_trace_get)
|
websocket_api.async_register_command(hass, websocket_trace_get)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_trace_list)
|
websocket_api.async_register_command(hass, websocket_trace_list)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_trace_contexts)
|
websocket_api.async_register_command(hass, websocket_trace_contexts)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_breakpoint_clear)
|
websocket_api.async_register_command(hass, websocket_breakpoint_clear)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_breakpoint_list)
|
websocket_api.async_register_command(hass, websocket_breakpoint_list)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_breakpoint_set)
|
websocket_api.async_register_command(hass, websocket_breakpoint_set)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_debug_continue)
|
websocket_api.async_register_command(hass, websocket_debug_continue)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_debug_step)
|
websocket_api.async_register_command(hass, websocket_debug_step)
|
||||||
websocket_api.async_register_command(hass, websocket_automation_debug_stop)
|
websocket_api.async_register_command(hass, websocket_debug_stop)
|
||||||
websocket_api.async_register_command(hass, websocket_subscribe_breakpoint_events)
|
websocket_api.async_register_command(hass, websocket_subscribe_breakpoint_events)
|
||||||
|
|
||||||
|
|
||||||
@ -53,17 +50,18 @@ def async_setup(hass: HomeAssistant) -> None:
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/trace/get",
|
vol.Required("type"): "trace/get",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("run_id"): str,
|
vol.Required("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_trace_get(hass, connection, msg):
|
def websocket_trace_get(hass, connection, msg):
|
||||||
"""Get an automation trace."""
|
"""Get an automation or script trace."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
run_id = msg["run_id"]
|
run_id = msg["run_id"]
|
||||||
|
|
||||||
trace = get_debug_trace(hass, automation_id, run_id)
|
trace = get_debug_trace(hass, key, run_id)
|
||||||
message = websocket_api.messages.result_message(msg["id"], trace)
|
message = websocket_api.messages.result_message(msg["id"], trace)
|
||||||
|
|
||||||
connection.send_message(json.dumps(message, cls=TraceJSONEncoder, allow_nan=False))
|
connection.send_message(json.dumps(message, cls=TraceJSONEncoder, allow_nan=False))
|
||||||
@ -72,42 +70,45 @@ def websocket_automation_trace_get(hass, connection, msg):
|
|||||||
@callback
|
@callback
|
||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{vol.Required("type"): "automation/trace/list", vol.Optional("automation_id"): str}
|
{
|
||||||
|
vol.Required("type"): "trace/list",
|
||||||
|
vol.Inclusive("domain", "id"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Inclusive("item_id", "id"): str,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_trace_list(hass, connection, msg):
|
def websocket_trace_list(hass, connection, msg):
|
||||||
"""Summarize automation traces."""
|
"""Summarize automation and script traces."""
|
||||||
automation_id = msg.get("automation_id")
|
key = (msg["domain"], msg["item_id"]) if "item_id" in msg else None
|
||||||
|
|
||||||
if not automation_id:
|
if not key:
|
||||||
automation_traces = get_debug_traces(hass, summary=True)
|
traces = get_all_debug_traces(hass, summary=True)
|
||||||
else:
|
else:
|
||||||
automation_traces = get_debug_traces_for_automation(
|
traces = get_debug_traces(hass, key, summary=True)
|
||||||
hass, automation_id, summary=True
|
|
||||||
)
|
|
||||||
|
|
||||||
connection.send_result(msg["id"], automation_traces)
|
connection.send_result(msg["id"], traces)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/trace/contexts",
|
vol.Required("type"): "trace/contexts",
|
||||||
vol.Optional("automation_id"): str,
|
vol.Inclusive("domain", "id"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Inclusive("item_id", "id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_trace_contexts(hass, connection, msg):
|
def websocket_trace_contexts(hass, connection, msg):
|
||||||
"""Retrieve contexts we have traces for."""
|
"""Retrieve contexts we have traces for."""
|
||||||
automation_id = msg.get("automation_id")
|
key = (msg["domain"], msg["item_id"]) if "item_id" in msg else None
|
||||||
|
|
||||||
if automation_id is not None:
|
if key is not None:
|
||||||
values = {automation_id: hass.data[DATA_TRACE].get(automation_id, {})}
|
values = {key: hass.data[DATA_TRACE].get(key, {})}
|
||||||
else:
|
else:
|
||||||
values = hass.data[DATA_TRACE]
|
values = hass.data[DATA_TRACE]
|
||||||
|
|
||||||
contexts = {
|
contexts = {
|
||||||
trace.context.id: {"run_id": trace.run_id, "automation_id": automation_id}
|
trace.context.id: {"run_id": trace.run_id, "domain": key[0], "item_id": key[1]}
|
||||||
for automation_id, traces in values.items()
|
for key, traces in values.items()
|
||||||
for trace in traces.values()
|
for trace in traces.values()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,15 +119,16 @@ def websocket_automation_trace_contexts(hass, connection, msg):
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/debug/breakpoint/set",
|
vol.Required("type"): "trace/debug/breakpoint/set",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("node"): str,
|
vol.Required("node"): str,
|
||||||
vol.Optional("run_id"): str,
|
vol.Optional("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_breakpoint_set(hass, connection, msg):
|
def websocket_breakpoint_set(hass, connection, msg):
|
||||||
"""Set breakpoint."""
|
"""Set breakpoint."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
node = msg["node"]
|
node = msg["node"]
|
||||||
run_id = msg.get("run_id")
|
run_id = msg.get("run_id")
|
||||||
|
|
||||||
@ -136,7 +138,7 @@ def websocket_automation_breakpoint_set(hass, connection, msg):
|
|||||||
):
|
):
|
||||||
raise HomeAssistantError("No breakpoint subscription")
|
raise HomeAssistantError("No breakpoint subscription")
|
||||||
|
|
||||||
result = breakpoint_set(hass, automation_id, run_id, node)
|
result = breakpoint_set(hass, key, run_id, node)
|
||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
|
||||||
|
|
||||||
@ -144,33 +146,32 @@ def websocket_automation_breakpoint_set(hass, connection, msg):
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/debug/breakpoint/clear",
|
vol.Required("type"): "trace/debug/breakpoint/clear",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("node"): str,
|
vol.Required("node"): str,
|
||||||
vol.Optional("run_id"): str,
|
vol.Optional("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_breakpoint_clear(hass, connection, msg):
|
def websocket_breakpoint_clear(hass, connection, msg):
|
||||||
"""Clear breakpoint."""
|
"""Clear breakpoint."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
node = msg["node"]
|
node = msg["node"]
|
||||||
run_id = msg.get("run_id")
|
run_id = msg.get("run_id")
|
||||||
|
|
||||||
result = breakpoint_clear(hass, automation_id, run_id, node)
|
result = breakpoint_clear(hass, key, run_id, node)
|
||||||
|
|
||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command({vol.Required("type"): "trace/debug/breakpoint/list"})
|
||||||
{vol.Required("type"): "automation/debug/breakpoint/list"}
|
def websocket_breakpoint_list(hass, connection, msg):
|
||||||
)
|
|
||||||
def websocket_automation_breakpoint_list(hass, connection, msg):
|
|
||||||
"""List breakpoints."""
|
"""List breakpoints."""
|
||||||
breakpoints = breakpoint_list(hass)
|
breakpoints = breakpoint_list(hass)
|
||||||
for _breakpoint in breakpoints:
|
for _breakpoint in breakpoints:
|
||||||
_breakpoint["automation_id"] = _breakpoint.pop("unique_id")
|
_breakpoint["domain"], _breakpoint["item_id"] = _breakpoint.pop("key")
|
||||||
|
|
||||||
connection.send_result(msg["id"], breakpoints)
|
connection.send_result(msg["id"], breakpoints)
|
||||||
|
|
||||||
@ -178,19 +179,20 @@ def websocket_automation_breakpoint_list(hass, connection, msg):
|
|||||||
@callback
|
@callback
|
||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{vol.Required("type"): "automation/debug/breakpoint/subscribe"}
|
{vol.Required("type"): "trace/debug/breakpoint/subscribe"}
|
||||||
)
|
)
|
||||||
def websocket_subscribe_breakpoint_events(hass, connection, msg):
|
def websocket_subscribe_breakpoint_events(hass, connection, msg):
|
||||||
"""Subscribe to breakpoint events."""
|
"""Subscribe to breakpoint events."""
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def breakpoint_hit(automation_id, run_id, node):
|
def breakpoint_hit(key, run_id, node):
|
||||||
"""Forward events to websocket."""
|
"""Forward events to websocket."""
|
||||||
connection.send_message(
|
connection.send_message(
|
||||||
websocket_api.event_message(
|
websocket_api.event_message(
|
||||||
msg["id"],
|
msg["id"],
|
||||||
{
|
{
|
||||||
"automation_id": automation_id,
|
"domain": key[0],
|
||||||
|
"item_id": key[1],
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
"node": node,
|
"node": node,
|
||||||
},
|
},
|
||||||
@ -221,17 +223,18 @@ def websocket_subscribe_breakpoint_events(hass, connection, msg):
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/debug/continue",
|
vol.Required("type"): "trace/debug/continue",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("run_id"): str,
|
vol.Required("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_debug_continue(hass, connection, msg):
|
def websocket_debug_continue(hass, connection, msg):
|
||||||
"""Resume execution of halted automation."""
|
"""Resume execution of halted automation or script."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
run_id = msg["run_id"]
|
run_id = msg["run_id"]
|
||||||
|
|
||||||
result = debug_continue(hass, automation_id, run_id)
|
result = debug_continue(hass, key, run_id)
|
||||||
|
|
||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
|
||||||
@ -240,17 +243,18 @@ def websocket_automation_debug_continue(hass, connection, msg):
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/debug/step",
|
vol.Required("type"): "trace/debug/step",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("run_id"): str,
|
vol.Required("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_debug_step(hass, connection, msg):
|
def websocket_debug_step(hass, connection, msg):
|
||||||
"""Single step a halted automation."""
|
"""Single step a halted automation or script."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
run_id = msg["run_id"]
|
run_id = msg["run_id"]
|
||||||
|
|
||||||
result = debug_step(hass, automation_id, run_id)
|
result = debug_step(hass, key, run_id)
|
||||||
|
|
||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
|
||||||
@ -259,16 +263,17 @@ def websocket_automation_debug_step(hass, connection, msg):
|
|||||||
@websocket_api.require_admin
|
@websocket_api.require_admin
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "automation/debug/stop",
|
vol.Required("type"): "trace/debug/stop",
|
||||||
vol.Required("automation_id"): str,
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
||||||
|
vol.Required("item_id"): str,
|
||||||
vol.Required("run_id"): str,
|
vol.Required("run_id"): str,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def websocket_automation_debug_stop(hass, connection, msg):
|
def websocket_debug_stop(hass, connection, msg):
|
||||||
"""Stop a halted automation."""
|
"""Stop a halted automation or script."""
|
||||||
automation_id = msg["automation_id"]
|
key = (msg["domain"], msg["item_id"])
|
||||||
run_id = msg["run_id"]
|
run_id = msg["run_id"]
|
||||||
|
|
||||||
result = debug_stop(hass, automation_id, run_id)
|
result = debug_stop(hass, key, run_id)
|
||||||
|
|
||||||
connection.send_result(msg["id"], result)
|
connection.send_result(msg["id"], result)
|
||||||
|
@ -143,26 +143,26 @@ async def trace_action(hass, script_run, stop, variables):
|
|||||||
|
|
||||||
trace_id = trace_id_get()
|
trace_id = trace_id_get()
|
||||||
if trace_id:
|
if trace_id:
|
||||||
unique_id = trace_id[0]
|
key = trace_id[0]
|
||||||
run_id = trace_id[1]
|
run_id = trace_id[1]
|
||||||
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
||||||
if unique_id in breakpoints and (
|
if key in breakpoints and (
|
||||||
(
|
(
|
||||||
run_id in breakpoints[unique_id]
|
run_id in breakpoints[key]
|
||||||
and (
|
and (
|
||||||
path in breakpoints[unique_id][run_id]
|
path in breakpoints[key][run_id]
|
||||||
or NODE_ANY in breakpoints[unique_id][run_id]
|
or NODE_ANY in breakpoints[key][run_id]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
or (
|
or (
|
||||||
RUN_ID_ANY in breakpoints[unique_id]
|
RUN_ID_ANY in breakpoints[key]
|
||||||
and (
|
and (
|
||||||
path in breakpoints[unique_id][RUN_ID_ANY]
|
path in breakpoints[key][RUN_ID_ANY]
|
||||||
or NODE_ANY in breakpoints[unique_id][RUN_ID_ANY]
|
or NODE_ANY in breakpoints[key][RUN_ID_ANY]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
async_dispatcher_send(hass, SCRIPT_BREAKPOINT_HIT, unique_id, run_id, path)
|
async_dispatcher_send(hass, SCRIPT_BREAKPOINT_HIT, key, run_id, path)
|
||||||
|
|
||||||
done = asyncio.Event()
|
done = asyncio.Event()
|
||||||
|
|
||||||
@ -172,7 +172,7 @@ async def trace_action(hass, script_run, stop, variables):
|
|||||||
stop.set()
|
stop.set()
|
||||||
done.set()
|
done.set()
|
||||||
|
|
||||||
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(unique_id, run_id)
|
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(key, run_id)
|
||||||
remove_signal1 = async_dispatcher_connect(hass, signal, async_continue_stop)
|
remove_signal1 = async_dispatcher_connect(hass, signal, async_continue_stop)
|
||||||
remove_signal2 = async_dispatcher_connect(
|
remove_signal2 = async_dispatcher_connect(
|
||||||
hass, SCRIPT_DEBUG_CONTINUE_ALL, async_continue_stop
|
hass, SCRIPT_DEBUG_CONTINUE_ALL, async_continue_stop
|
||||||
@ -1281,13 +1281,13 @@ class Script:
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def breakpoint_clear(hass, unique_id, run_id, node):
|
def breakpoint_clear(hass, key, run_id, node):
|
||||||
"""Clear a breakpoint."""
|
"""Clear a breakpoint."""
|
||||||
run_id = run_id or RUN_ID_ANY
|
run_id = run_id or RUN_ID_ANY
|
||||||
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
||||||
if unique_id not in breakpoints or run_id not in breakpoints[unique_id]:
|
if key not in breakpoints or run_id not in breakpoints[key]:
|
||||||
return
|
return
|
||||||
breakpoints[unique_id][run_id].discard(node)
|
breakpoints[key][run_id].discard(node)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -1297,15 +1297,15 @@ def breakpoint_clear_all(hass):
|
|||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def breakpoint_set(hass, unique_id, run_id, node):
|
def breakpoint_set(hass, key, run_id, node):
|
||||||
"""Set a breakpoint."""
|
"""Set a breakpoint."""
|
||||||
run_id = run_id or RUN_ID_ANY
|
run_id = run_id or RUN_ID_ANY
|
||||||
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
||||||
if unique_id not in breakpoints:
|
if key not in breakpoints:
|
||||||
breakpoints[unique_id] = {}
|
breakpoints[key] = {}
|
||||||
if run_id not in breakpoints[unique_id]:
|
if run_id not in breakpoints[key]:
|
||||||
breakpoints[unique_id][run_id] = set()
|
breakpoints[key][run_id] = set()
|
||||||
breakpoints[unique_id][run_id].add(node)
|
breakpoints[key][run_id].add(node)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -1314,35 +1314,35 @@ def breakpoint_list(hass):
|
|||||||
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS]
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{"unique_id": unique_id, "run_id": run_id, "node": node}
|
{"key": key, "run_id": run_id, "node": node}
|
||||||
for unique_id in breakpoints
|
for key in breakpoints
|
||||||
for run_id in breakpoints[unique_id]
|
for run_id in breakpoints[key]
|
||||||
for node in breakpoints[unique_id][run_id]
|
for node in breakpoints[key][run_id]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def debug_continue(hass, unique_id, run_id):
|
def debug_continue(hass, key, run_id):
|
||||||
"""Continue execution of a halted script."""
|
"""Continue execution of a halted script."""
|
||||||
# Clear any wildcard breakpoint
|
# Clear any wildcard breakpoint
|
||||||
breakpoint_clear(hass, unique_id, run_id, NODE_ANY)
|
breakpoint_clear(hass, key, run_id, NODE_ANY)
|
||||||
|
|
||||||
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(unique_id, run_id)
|
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(key, run_id)
|
||||||
async_dispatcher_send(hass, signal, "continue")
|
async_dispatcher_send(hass, signal, "continue")
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def debug_step(hass, unique_id, run_id):
|
def debug_step(hass, key, run_id):
|
||||||
"""Single step a halted script."""
|
"""Single step a halted script."""
|
||||||
# Set a wildcard breakpoint
|
# Set a wildcard breakpoint
|
||||||
breakpoint_set(hass, unique_id, run_id, NODE_ANY)
|
breakpoint_set(hass, key, run_id, NODE_ANY)
|
||||||
|
|
||||||
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(unique_id, run_id)
|
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(key, run_id)
|
||||||
async_dispatcher_send(hass, signal, "continue")
|
async_dispatcher_send(hass, signal, "continue")
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def debug_stop(hass, unique_id, run_id):
|
def debug_stop(hass, key, run_id):
|
||||||
"""Stop execution of a running or halted script."""
|
"""Stop execution of a running or halted script."""
|
||||||
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(unique_id, run_id)
|
signal = SCRIPT_DEBUG_CONTINUE_STOP.format(key, run_id)
|
||||||
async_dispatcher_send(hass, signal, "stop")
|
async_dispatcher_send(hass, signal, "stop")
|
||||||
|
@ -2,25 +2,26 @@
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.bootstrap import async_setup_component
|
from homeassistant.bootstrap import async_setup_component
|
||||||
from homeassistant.components import automation, config
|
from homeassistant.components import config
|
||||||
|
from homeassistant.components.trace.const import STORED_TRACES
|
||||||
from homeassistant.core import Context
|
from homeassistant.core import Context
|
||||||
|
|
||||||
from tests.common import assert_lists_same
|
from tests.common import assert_lists_same
|
||||||
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
def _find_run_id(traces, automation_id):
|
def _find_run_id(traces, item_id):
|
||||||
"""Find newest run_id for an automation."""
|
"""Find newest run_id for an automation."""
|
||||||
for trace in reversed(traces):
|
for trace in reversed(traces):
|
||||||
if trace["automation_id"] == automation_id:
|
if trace["item_id"] == item_id:
|
||||||
return trace["run_id"]
|
return trace["run_id"]
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _find_traces_for_automation(traces, automation_id):
|
def _find_traces_for_automation(traces, item_id):
|
||||||
"""Find traces for an automation."""
|
"""Find traces for an automation."""
|
||||||
return [trace for trace in traces if trace["automation_id"] == automation_id]
|
return [trace for trace in traces if trace["item_id"] == item_id]
|
||||||
|
|
||||||
|
|
||||||
async def test_get_automation_trace(hass, hass_ws_client):
|
async def test_get_automation_trace(hass, hass_ws_client):
|
||||||
@ -85,7 +86,7 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# List traces
|
# List traces
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
run_id = _find_run_id(response["result"], "sun")
|
run_id = _find_run_id(response["result"], "sun")
|
||||||
@ -94,8 +95,9 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/trace/get",
|
"type": "trace/get",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -113,11 +115,12 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
assert trace["error"] == "Unable to find service test.automation"
|
assert trace["error"] == "Unable to find service test.automation"
|
||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["trigger"] == "event 'test_event'"
|
assert trace["trigger"] == "event 'test_event'"
|
||||||
assert trace["unique_id"] == "sun"
|
assert trace["item_id"] == "sun"
|
||||||
assert trace["variables"]
|
assert trace["variables"]
|
||||||
contexts[trace["context"]["id"]] = {
|
contexts[trace["context"]["id"]] = {
|
||||||
"run_id": trace["run_id"],
|
"run_id": trace["run_id"],
|
||||||
"automation_id": trace["automation_id"],
|
"domain": "automation",
|
||||||
|
"item_id": trace["item_id"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Trigger "moon" automation, with passing condition
|
# Trigger "moon" automation, with passing condition
|
||||||
@ -125,7 +128,7 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# List traces
|
# List traces
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
run_id = _find_run_id(response["result"], "moon")
|
run_id = _find_run_id(response["result"], "moon")
|
||||||
@ -134,8 +137,9 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/trace/get",
|
"type": "trace/get",
|
||||||
"automation_id": "moon",
|
"domain": "automation",
|
||||||
|
"item_id": "moon",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -154,11 +158,12 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
assert "error" not in trace
|
assert "error" not in trace
|
||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["trigger"] == "event 'test_event2'"
|
assert trace["trigger"] == "event 'test_event2'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
assert trace["variables"]
|
assert trace["variables"]
|
||||||
contexts[trace["context"]["id"]] = {
|
contexts[trace["context"]["id"]] = {
|
||||||
"run_id": trace["run_id"],
|
"run_id": trace["run_id"],
|
||||||
"automation_id": trace["automation_id"],
|
"domain": "automation",
|
||||||
|
"item_id": trace["item_id"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Trigger "moon" automation, with failing condition
|
# Trigger "moon" automation, with failing condition
|
||||||
@ -166,7 +171,7 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# List traces
|
# List traces
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
run_id = _find_run_id(response["result"], "moon")
|
run_id = _find_run_id(response["result"], "moon")
|
||||||
@ -175,8 +180,9 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/trace/get",
|
"type": "trace/get",
|
||||||
"automation_id": "moon",
|
"domain": "automation",
|
||||||
|
"item_id": "moon",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -192,11 +198,12 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
assert "error" not in trace
|
assert "error" not in trace
|
||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["trigger"] == "event 'test_event3'"
|
assert trace["trigger"] == "event 'test_event3'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
assert trace["variables"]
|
assert trace["variables"]
|
||||||
contexts[trace["context"]["id"]] = {
|
contexts[trace["context"]["id"]] = {
|
||||||
"run_id": trace["run_id"],
|
"run_id": trace["run_id"],
|
||||||
"automation_id": trace["automation_id"],
|
"domain": "automation",
|
||||||
|
"item_id": trace["item_id"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Trigger "moon" automation, with passing condition
|
# Trigger "moon" automation, with passing condition
|
||||||
@ -204,7 +211,7 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# List traces
|
# List traces
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
run_id = _find_run_id(response["result"], "moon")
|
run_id = _find_run_id(response["result"], "moon")
|
||||||
@ -213,8 +220,9 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/trace/get",
|
"type": "trace/get",
|
||||||
"automation_id": "moon",
|
"domain": "automation",
|
||||||
|
"item_id": "moon",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -233,15 +241,16 @@ async def test_get_automation_trace(hass, hass_ws_client):
|
|||||||
assert "error" not in trace
|
assert "error" not in trace
|
||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["trigger"] == "event 'test_event2'"
|
assert trace["trigger"] == "event 'test_event2'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
assert trace["variables"]
|
assert trace["variables"]
|
||||||
contexts[trace["context"]["id"]] = {
|
contexts[trace["context"]["id"]] = {
|
||||||
"run_id": trace["run_id"],
|
"run_id": trace["run_id"],
|
||||||
"automation_id": trace["automation_id"],
|
"domain": "automation",
|
||||||
|
"item_id": trace["item_id"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check contexts
|
# Check contexts
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/contexts"})
|
await client.send_json({"id": next_id(), "type": "trace/contexts"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == contexts
|
assert response["result"] == contexts
|
||||||
@ -283,7 +292,7 @@ async def test_automation_trace_overflow(hass, hass_ws_client):
|
|||||||
|
|
||||||
client = await hass_ws_client()
|
client = await hass_ws_client()
|
||||||
|
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == []
|
assert response["result"] == []
|
||||||
@ -294,7 +303,7 @@ async def test_automation_trace_overflow(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# List traces
|
# List traces
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert len(_find_traces_for_automation(response["result"], "moon")) == 1
|
assert len(_find_traces_for_automation(response["result"], "moon")) == 1
|
||||||
@ -302,21 +311,18 @@ async def test_automation_trace_overflow(hass, hass_ws_client):
|
|||||||
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
||||||
|
|
||||||
# Trigger "moon" automation enough times to overflow the number of stored traces
|
# Trigger "moon" automation enough times to overflow the number of stored traces
|
||||||
for _ in range(automation.trace.STORED_TRACES):
|
for _ in range(STORED_TRACES):
|
||||||
hass.bus.async_fire("test_event2")
|
hass.bus.async_fire("test_event2")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
moon_traces = _find_traces_for_automation(response["result"], "moon")
|
moon_traces = _find_traces_for_automation(response["result"], "moon")
|
||||||
assert len(moon_traces) == automation.trace.STORED_TRACES
|
assert len(moon_traces) == STORED_TRACES
|
||||||
assert moon_traces[0]
|
assert moon_traces[0]
|
||||||
assert int(moon_traces[0]["run_id"]) == int(moon_run_id) + 1
|
assert int(moon_traces[0]["run_id"]) == int(moon_run_id) + 1
|
||||||
assert (
|
assert int(moon_traces[-1]["run_id"]) == int(moon_run_id) + STORED_TRACES
|
||||||
int(moon_traces[-1]["run_id"])
|
|
||||||
== int(moon_run_id) + automation.trace.STORED_TRACES
|
|
||||||
)
|
|
||||||
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
||||||
|
|
||||||
|
|
||||||
@ -363,13 +369,18 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
|
|
||||||
client = await hass_ws_client()
|
client = await hass_ws_client()
|
||||||
|
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == []
|
assert response["result"] == []
|
||||||
|
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": next_id(), "type": "automation/trace/list", "automation_id": "sun"}
|
{
|
||||||
|
"id": next_id(),
|
||||||
|
"type": "trace/list",
|
||||||
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -380,14 +391,19 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Get trace
|
# Get trace
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert len(response["result"]) == 1
|
assert len(response["result"]) == 1
|
||||||
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
||||||
|
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": next_id(), "type": "automation/trace/list", "automation_id": "sun"}
|
{
|
||||||
|
"id": next_id(),
|
||||||
|
"type": "trace/list",
|
||||||
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -395,7 +411,12 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
assert len(_find_traces_for_automation(response["result"], "sun")) == 1
|
||||||
|
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": next_id(), "type": "automation/trace/list", "automation_id": "moon"}
|
{
|
||||||
|
"id": next_id(),
|
||||||
|
"type": "trace/list",
|
||||||
|
"domain": "automation",
|
||||||
|
"item_id": "moon",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -414,7 +435,7 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Get trace
|
# Get trace
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert len(_find_traces_for_automation(response["result"], "moon")) == 3
|
assert len(_find_traces_for_automation(response["result"], "moon")) == 3
|
||||||
@ -426,7 +447,7 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["timestamp"]
|
assert trace["timestamp"]
|
||||||
assert trace["trigger"] == "event 'test_event'"
|
assert trace["trigger"] == "event 'test_event'"
|
||||||
assert trace["unique_id"] == "sun"
|
assert trace["item_id"] == "sun"
|
||||||
|
|
||||||
trace = _find_traces_for_automation(response["result"], "moon")[0]
|
trace = _find_traces_for_automation(response["result"], "moon")[0]
|
||||||
assert trace["last_action"] == "action/0"
|
assert trace["last_action"] == "action/0"
|
||||||
@ -435,7 +456,7 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["timestamp"]
|
assert trace["timestamp"]
|
||||||
assert trace["trigger"] == "event 'test_event2'"
|
assert trace["trigger"] == "event 'test_event2'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
|
|
||||||
trace = _find_traces_for_automation(response["result"], "moon")[1]
|
trace = _find_traces_for_automation(response["result"], "moon")[1]
|
||||||
assert trace["last_action"] is None
|
assert trace["last_action"] is None
|
||||||
@ -444,7 +465,7 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["timestamp"]
|
assert trace["timestamp"]
|
||||||
assert trace["trigger"] == "event 'test_event3'"
|
assert trace["trigger"] == "event 'test_event3'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
|
|
||||||
trace = _find_traces_for_automation(response["result"], "moon")[2]
|
trace = _find_traces_for_automation(response["result"], "moon")[2]
|
||||||
assert trace["last_action"] == "action/0"
|
assert trace["last_action"] == "action/0"
|
||||||
@ -453,7 +474,7 @@ async def test_list_automation_traces(hass, hass_ws_client):
|
|||||||
assert trace["state"] == "stopped"
|
assert trace["state"] == "stopped"
|
||||||
assert trace["timestamp"]
|
assert trace["timestamp"]
|
||||||
assert trace["trigger"] == "event 'test_event2'"
|
assert trace["trigger"] == "event 'test_event2'"
|
||||||
assert trace["unique_id"] == "moon"
|
assert trace["item_id"] == "moon"
|
||||||
|
|
||||||
|
|
||||||
async def test_automation_breakpoints(hass, hass_ws_client):
|
async def test_automation_breakpoints(hass, hass_ws_client):
|
||||||
@ -465,11 +486,11 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
id += 1
|
id += 1
|
||||||
return id
|
return id
|
||||||
|
|
||||||
async def assert_last_action(automation_id, expected_action, expected_state):
|
async def assert_last_action(item_id, expected_action, expected_state):
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
trace = _find_traces_for_automation(response["result"], automation_id)[-1]
|
trace = _find_traces_for_automation(response["result"], item_id)[-1]
|
||||||
assert trace["last_action"] == expected_action
|
assert trace["last_action"] == expected_action
|
||||||
assert trace["state"] == expected_state
|
assert trace["state"] == expected_state
|
||||||
return trace["run_id"]
|
return trace["run_id"]
|
||||||
@ -508,24 +529,23 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "1",
|
"node": "1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert not response["success"]
|
assert not response["success"]
|
||||||
|
|
||||||
await client.send_json(
|
await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"})
|
||||||
{"id": next_id(), "type": "automation/debug/breakpoint/list"}
|
|
||||||
)
|
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert response["result"] == []
|
assert response["result"] == []
|
||||||
|
|
||||||
subscription_id = next_id()
|
subscription_id = next_id()
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": subscription_id, "type": "automation/debug/breakpoint/subscribe"}
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -533,8 +553,9 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -543,24 +564,33 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/5",
|
"node": "action/5",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
|
|
||||||
await client.send_json(
|
await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"})
|
||||||
{"id": next_id(), "type": "automation/debug/breakpoint/list"}
|
|
||||||
)
|
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
assert_lists_same(
|
assert_lists_same(
|
||||||
response["result"],
|
response["result"],
|
||||||
[
|
[
|
||||||
{"node": "action/1", "run_id": "*", "automation_id": "sun"},
|
{
|
||||||
{"node": "action/5", "run_id": "*", "automation_id": "sun"},
|
"node": "action/1",
|
||||||
|
"run_id": "*",
|
||||||
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node": "action/5",
|
||||||
|
"run_id": "*",
|
||||||
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -570,7 +600,8 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/1", "running")
|
run_id = await assert_last_action("sun", "action/1", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -578,8 +609,9 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/step",
|
"type": "trace/debug/step",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -589,7 +621,8 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/2", "running")
|
run_id = await assert_last_action("sun", "action/2", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/2",
|
"node": "action/2",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -597,8 +630,9 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/continue",
|
"type": "trace/debug/continue",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -608,7 +642,8 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/5", "running")
|
run_id = await assert_last_action("sun", "action/5", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/5",
|
"node": "action/5",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -616,8 +651,9 @@ async def test_automation_breakpoints(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/stop",
|
"type": "trace/debug/stop",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -636,11 +672,11 @@ async def test_automation_breakpoints_2(hass, hass_ws_client):
|
|||||||
id += 1
|
id += 1
|
||||||
return id
|
return id
|
||||||
|
|
||||||
async def assert_last_action(automation_id, expected_action, expected_state):
|
async def assert_last_action(item_id, expected_action, expected_state):
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
trace = _find_traces_for_automation(response["result"], automation_id)[-1]
|
trace = _find_traces_for_automation(response["result"], item_id)[-1]
|
||||||
assert trace["last_action"] == expected_action
|
assert trace["last_action"] == expected_action
|
||||||
assert trace["state"] == expected_state
|
assert trace["state"] == expected_state
|
||||||
return trace["run_id"]
|
return trace["run_id"]
|
||||||
@ -678,7 +714,7 @@ async def test_automation_breakpoints_2(hass, hass_ws_client):
|
|||||||
|
|
||||||
subscription_id = next_id()
|
subscription_id = next_id()
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": subscription_id, "type": "automation/debug/breakpoint/subscribe"}
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -686,8 +722,9 @@ async def test_automation_breakpoints_2(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -700,7 +737,8 @@ async def test_automation_breakpoints_2(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/1", "running")
|
run_id = await assert_last_action("sun", "action/1", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -718,8 +756,9 @@ async def test_automation_breakpoints_2(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "1",
|
"node": "1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -743,11 +782,11 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
id += 1
|
id += 1
|
||||||
return id
|
return id
|
||||||
|
|
||||||
async def assert_last_action(automation_id, expected_action, expected_state):
|
async def assert_last_action(item_id, expected_action, expected_state):
|
||||||
await client.send_json({"id": next_id(), "type": "automation/trace/list"})
|
await client.send_json({"id": next_id(), "type": "trace/list"})
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
trace = _find_traces_for_automation(response["result"], automation_id)[-1]
|
trace = _find_traces_for_automation(response["result"], item_id)[-1]
|
||||||
assert trace["last_action"] == expected_action
|
assert trace["last_action"] == expected_action
|
||||||
assert trace["state"] == expected_state
|
assert trace["state"] == expected_state
|
||||||
return trace["run_id"]
|
return trace["run_id"]
|
||||||
@ -785,7 +824,7 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
|
|
||||||
subscription_id = next_id()
|
subscription_id = next_id()
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": subscription_id, "type": "automation/debug/breakpoint/subscribe"}
|
{"id": subscription_id, "type": "trace/debug/breakpoint/subscribe"}
|
||||||
)
|
)
|
||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
assert response["success"]
|
assert response["success"]
|
||||||
@ -793,8 +832,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -804,8 +844,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/set",
|
"type": "trace/debug/breakpoint/set",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/5",
|
"node": "action/5",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -818,7 +859,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/1", "running")
|
run_id = await assert_last_action("sun", "action/1", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -826,8 +868,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/continue",
|
"type": "trace/debug/continue",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -837,7 +880,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/5", "running")
|
run_id = await assert_last_action("sun", "action/5", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/5",
|
"node": "action/5",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
@ -845,8 +889,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/stop",
|
"type": "trace/debug/stop",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -859,8 +904,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": next_id(),
|
"id": next_id(),
|
||||||
"type": "automation/debug/breakpoint/clear",
|
"type": "trace/debug/breakpoint/clear",
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/1",
|
"node": "action/1",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -873,7 +919,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client):
|
|||||||
response = await client.receive_json()
|
response = await client.receive_json()
|
||||||
run_id = await assert_last_action("sun", "action/5", "running")
|
run_id = await assert_last_action("sun", "action/5", "running")
|
||||||
assert response["event"] == {
|
assert response["event"] == {
|
||||||
"automation_id": "sun",
|
"domain": "automation",
|
||||||
|
"item_id": "sun",
|
||||||
"node": "action/5",
|
"node": "action/5",
|
||||||
"run_id": run_id,
|
"run_id": run_id,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user