diff --git a/homeassistant/components/automation/trace.py b/homeassistant/components/automation/trace.py index de199ad9310..f5e93e09c38 100644 --- a/homeassistant/components/automation/trace.py +++ b/homeassistant/components/automation/trace.py @@ -23,7 +23,7 @@ class AutomationTrace: def __init__( self, - unique_id: str | None, + key: tuple[str, str], config: dict[str, Any], context: Context, ): @@ -37,7 +37,7 @@ class AutomationTrace: self.run_id: str = str(next(self._run_ids)) self._timestamp_finish: dt.datetime | None = None 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 def set_action_trace(self, trace: dict[str, Deque[TraceElement]]) -> None: @@ -104,7 +104,6 @@ class AutomationTrace: trigger = self._variables.get("trigger", {}).get("description") result = { - "automation_id": self._unique_id, "last_action": last_action, "last_condition": last_condition, "run_id": self.run_id, @@ -114,7 +113,8 @@ class AutomationTrace: "finish": self._timestamp_finish, }, "trigger": trigger, - "unique_id": self._unique_id, + "domain": self._key[0], + "item_id": self._key[1], } if self._error is not None: result["error"] = str(self._error) @@ -126,23 +126,24 @@ class AutomationTrace: @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.""" - automation_trace = AutomationTrace(unique_id, config, context) - trace_id_set((unique_id, automation_trace.run_id)) + key = ("automation", item_id) + trace = AutomationTrace(key, config, context) + trace_id_set((key, trace.run_id)) - if unique_id: - automation_traces = hass.data[DATA_TRACE] - if unique_id not in automation_traces: - automation_traces[unique_id] = LimitedSizeDict(size_limit=STORED_TRACES) - automation_traces[unique_id][automation_trace.run_id] = automation_trace + if key: + traces = hass.data[DATA_TRACE] + if key not in traces: + traces[key] = LimitedSizeDict(size_limit=STORED_TRACES) + traces[key][trace.run_id] = trace try: - yield automation_trace + yield trace except Exception as ex: # pylint: disable=broad-except - if unique_id: - automation_trace.set_error(ex) + if key: + trace.set_error(ex) raise ex finally: - if unique_id: - automation_trace.finished() + if key: + trace.finished() diff --git a/homeassistant/components/trace/trace.py b/homeassistant/components/trace/trace.py index cc51e3269ab..e9255550ec5 100644 --- a/homeassistant/components/trace/trace.py +++ b/homeassistant/components/trace/trace.py @@ -5,17 +5,17 @@ from .const import DATA_TRACE @callback -def get_debug_trace(hass, automation_id, run_id): +def get_debug_trace(hass, key, run_id): """Return a serializable debug trace.""" - return hass.data[DATA_TRACE][automation_id][run_id] + return hass.data[DATA_TRACE][key][run_id] @callback -def get_debug_traces_for_automation(hass, automation_id, summary=False): - """Return a serializable list of debug traces for an automation.""" +def get_debug_traces(hass, key, summary=False): + """Return a serializable list of debug traces for an automation or script.""" traces = [] - for trace in hass.data[DATA_TRACE].get(automation_id, {}).values(): + for trace in hass.data[DATA_TRACE].get(key, {}).values(): if summary: traces.append(trace.as_short_dict()) else: @@ -25,11 +25,11 @@ def get_debug_traces_for_automation(hass, automation_id, summary=False): @callback -def get_debug_traces(hass, summary=False): - """Return a serializable list of debug traces.""" +def get_all_debug_traces(hass, summary=False): + """Return a serializable list of debug traces for all automations and scripts.""" traces = [] - for automation_id in hass.data[DATA_TRACE]: - traces.extend(get_debug_traces_for_automation(hass, automation_id, summary)) + for key in hass.data[DATA_TRACE]: + traces.extend(get_debug_traces(hass, key, summary)) return traces diff --git a/homeassistant/components/trace/websocket_api.py b/homeassistant/components/trace/websocket_api.py index 9ac1828de14..1f42b50671e 100644 --- a/homeassistant/components/trace/websocket_api.py +++ b/homeassistant/components/trace/websocket_api.py @@ -23,29 +23,26 @@ from homeassistant.helpers.script import ( debug_stop, ) -from .trace import ( - DATA_TRACE, - get_debug_trace, - get_debug_traces, - get_debug_traces_for_automation, -) +from .trace import DATA_TRACE, get_all_debug_traces, get_debug_trace, get_debug_traces from .utils import TraceJSONEncoder # mypy: allow-untyped-calls, allow-untyped-defs +TRACE_DOMAINS = ["automation"] + @callback def async_setup(hass: HomeAssistant) -> None: """Set up the websocket API.""" - websocket_api.async_register_command(hass, websocket_automation_trace_get) - websocket_api.async_register_command(hass, websocket_automation_trace_list) - websocket_api.async_register_command(hass, websocket_automation_trace_contexts) - websocket_api.async_register_command(hass, websocket_automation_breakpoint_clear) - websocket_api.async_register_command(hass, websocket_automation_breakpoint_list) - websocket_api.async_register_command(hass, websocket_automation_breakpoint_set) - websocket_api.async_register_command(hass, websocket_automation_debug_continue) - websocket_api.async_register_command(hass, websocket_automation_debug_step) - websocket_api.async_register_command(hass, websocket_automation_debug_stop) + websocket_api.async_register_command(hass, websocket_trace_get) + websocket_api.async_register_command(hass, websocket_trace_list) + websocket_api.async_register_command(hass, websocket_trace_contexts) + websocket_api.async_register_command(hass, websocket_breakpoint_clear) + websocket_api.async_register_command(hass, websocket_breakpoint_list) + websocket_api.async_register_command(hass, websocket_breakpoint_set) + websocket_api.async_register_command(hass, websocket_debug_continue) + websocket_api.async_register_command(hass, websocket_debug_step) + websocket_api.async_register_command(hass, websocket_debug_stop) 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.websocket_command( { - vol.Required("type"): "automation/trace/get", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/get", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("run_id"): str, } ) -def websocket_automation_trace_get(hass, connection, msg): - """Get an automation trace.""" - automation_id = msg["automation_id"] +def websocket_trace_get(hass, connection, msg): + """Get an automation or script trace.""" + key = (msg["domain"], msg["item_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) connection.send_message(json.dumps(message, cls=TraceJSONEncoder, allow_nan=False)) @@ -72,42 +70,45 @@ def websocket_automation_trace_get(hass, connection, msg): @callback @websocket_api.require_admin @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): - """Summarize automation traces.""" - automation_id = msg.get("automation_id") +def websocket_trace_list(hass, connection, msg): + """Summarize automation and script traces.""" + key = (msg["domain"], msg["item_id"]) if "item_id" in msg else None - if not automation_id: - automation_traces = get_debug_traces(hass, summary=True) + if not key: + traces = get_all_debug_traces(hass, summary=True) else: - automation_traces = get_debug_traces_for_automation( - hass, automation_id, summary=True - ) + traces = get_debug_traces(hass, key, summary=True) - connection.send_result(msg["id"], automation_traces) + connection.send_result(msg["id"], traces) @callback @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/trace/contexts", - vol.Optional("automation_id"): str, + vol.Required("type"): "trace/contexts", + 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.""" - 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: - values = {automation_id: hass.data[DATA_TRACE].get(automation_id, {})} + if key is not None: + values = {key: hass.data[DATA_TRACE].get(key, {})} else: values = hass.data[DATA_TRACE] contexts = { - trace.context.id: {"run_id": trace.run_id, "automation_id": automation_id} - for automation_id, traces in values.items() + trace.context.id: {"run_id": trace.run_id, "domain": key[0], "item_id": key[1]} + for key, traces in values.items() for trace in traces.values() } @@ -118,15 +119,16 @@ def websocket_automation_trace_contexts(hass, connection, msg): @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/debug/breakpoint/set", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/debug/breakpoint/set", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("node"): str, vol.Optional("run_id"): str, } ) -def websocket_automation_breakpoint_set(hass, connection, msg): +def websocket_breakpoint_set(hass, connection, msg): """Set breakpoint.""" - automation_id = msg["automation_id"] + key = (msg["domain"], msg["item_id"]) node = msg["node"] run_id = msg.get("run_id") @@ -136,7 +138,7 @@ def websocket_automation_breakpoint_set(hass, connection, msg): ): 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) @@ -144,33 +146,32 @@ def websocket_automation_breakpoint_set(hass, connection, msg): @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/debug/breakpoint/clear", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/debug/breakpoint/clear", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("node"): str, vol.Optional("run_id"): str, } ) -def websocket_automation_breakpoint_clear(hass, connection, msg): +def websocket_breakpoint_clear(hass, connection, msg): """Clear breakpoint.""" - automation_id = msg["automation_id"] + key = (msg["domain"], msg["item_id"]) node = msg["node"] 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) @callback @websocket_api.require_admin -@websocket_api.websocket_command( - {vol.Required("type"): "automation/debug/breakpoint/list"} -) -def websocket_automation_breakpoint_list(hass, connection, msg): +@websocket_api.websocket_command({vol.Required("type"): "trace/debug/breakpoint/list"}) +def websocket_breakpoint_list(hass, connection, msg): """List breakpoints.""" breakpoints = breakpoint_list(hass) 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) @@ -178,19 +179,20 @@ def websocket_automation_breakpoint_list(hass, connection, msg): @callback @websocket_api.require_admin @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): """Subscribe to breakpoint events.""" @callback - def breakpoint_hit(automation_id, run_id, node): + def breakpoint_hit(key, run_id, node): """Forward events to websocket.""" connection.send_message( websocket_api.event_message( msg["id"], { - "automation_id": automation_id, + "domain": key[0], + "item_id": key[1], "run_id": run_id, "node": node, }, @@ -221,17 +223,18 @@ def websocket_subscribe_breakpoint_events(hass, connection, msg): @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/debug/continue", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/debug/continue", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("run_id"): str, } ) -def websocket_automation_debug_continue(hass, connection, msg): - """Resume execution of halted automation.""" - automation_id = msg["automation_id"] +def websocket_debug_continue(hass, connection, msg): + """Resume execution of halted automation or script.""" + key = (msg["domain"], msg["item_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) @@ -240,17 +243,18 @@ def websocket_automation_debug_continue(hass, connection, msg): @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/debug/step", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/debug/step", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("run_id"): str, } ) -def websocket_automation_debug_step(hass, connection, msg): - """Single step a halted automation.""" - automation_id = msg["automation_id"] +def websocket_debug_step(hass, connection, msg): + """Single step a halted automation or script.""" + key = (msg["domain"], msg["item_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) @@ -259,16 +263,17 @@ def websocket_automation_debug_step(hass, connection, msg): @websocket_api.require_admin @websocket_api.websocket_command( { - vol.Required("type"): "automation/debug/stop", - vol.Required("automation_id"): str, + vol.Required("type"): "trace/debug/stop", + vol.Required("domain"): vol.In(TRACE_DOMAINS), + vol.Required("item_id"): str, vol.Required("run_id"): str, } ) -def websocket_automation_debug_stop(hass, connection, msg): - """Stop a halted automation.""" - automation_id = msg["automation_id"] +def websocket_debug_stop(hass, connection, msg): + """Stop a halted automation or script.""" + key = (msg["domain"], msg["item_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) diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index d07d963c95d..3df5be09f13 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -143,26 +143,26 @@ async def trace_action(hass, script_run, stop, variables): trace_id = trace_id_get() if trace_id: - unique_id = trace_id[0] + key = trace_id[0] run_id = trace_id[1] 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 ( - path in breakpoints[unique_id][run_id] - or NODE_ANY in breakpoints[unique_id][run_id] + path in breakpoints[key][run_id] + or NODE_ANY in breakpoints[key][run_id] ) ) or ( - RUN_ID_ANY in breakpoints[unique_id] + RUN_ID_ANY in breakpoints[key] and ( - path in breakpoints[unique_id][RUN_ID_ANY] - or NODE_ANY in breakpoints[unique_id][RUN_ID_ANY] + path in breakpoints[key][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() @@ -172,7 +172,7 @@ async def trace_action(hass, script_run, stop, variables): stop.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_signal2 = async_dispatcher_connect( hass, SCRIPT_DEBUG_CONTINUE_ALL, async_continue_stop @@ -1281,13 +1281,13 @@ class Script: @callback -def breakpoint_clear(hass, unique_id, run_id, node): +def breakpoint_clear(hass, key, run_id, node): """Clear a breakpoint.""" run_id = run_id or RUN_ID_ANY 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 - breakpoints[unique_id][run_id].discard(node) + breakpoints[key][run_id].discard(node) @callback @@ -1297,15 +1297,15 @@ def breakpoint_clear_all(hass): @callback -def breakpoint_set(hass, unique_id, run_id, node): +def breakpoint_set(hass, key, run_id, node): """Set a breakpoint.""" run_id = run_id or RUN_ID_ANY breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS] - if unique_id not in breakpoints: - breakpoints[unique_id] = {} - if run_id not in breakpoints[unique_id]: - breakpoints[unique_id][run_id] = set() - breakpoints[unique_id][run_id].add(node) + if key not in breakpoints: + breakpoints[key] = {} + if run_id not in breakpoints[key]: + breakpoints[key][run_id] = set() + breakpoints[key][run_id].add(node) @callback @@ -1314,35 +1314,35 @@ def breakpoint_list(hass): breakpoints = hass.data[DATA_SCRIPT_BREAKPOINTS] return [ - {"unique_id": unique_id, "run_id": run_id, "node": node} - for unique_id in breakpoints - for run_id in breakpoints[unique_id] - for node in breakpoints[unique_id][run_id] + {"key": key, "run_id": run_id, "node": node} + for key in breakpoints + for run_id in breakpoints[key] + for node in breakpoints[key][run_id] ] @callback -def debug_continue(hass, unique_id, run_id): +def debug_continue(hass, key, run_id): """Continue execution of a halted script.""" # 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") @callback -def debug_step(hass, unique_id, run_id): +def debug_step(hass, key, run_id): """Single step a halted script.""" # 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") @callback -def debug_stop(hass, unique_id, run_id): +def debug_stop(hass, key, run_id): """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") diff --git a/tests/components/trace/test_websocket_api.py b/tests/components/trace/test_websocket_api.py index 882d857c014..e07c042d1d0 100644 --- a/tests/components/trace/test_websocket_api.py +++ b/tests/components/trace/test_websocket_api.py @@ -2,25 +2,26 @@ from unittest.mock import patch 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 tests.common import assert_lists_same 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.""" for trace in reversed(traces): - if trace["automation_id"] == automation_id: + if trace["item_id"] == item_id: return trace["run_id"] return None -def _find_traces_for_automation(traces, automation_id): +def _find_traces_for_automation(traces, item_id): """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): @@ -85,7 +86,7 @@ async def test_get_automation_trace(hass, hass_ws_client): await hass.async_block_till_done() # 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() assert response["success"] 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( { "id": next_id(), - "type": "automation/trace/get", - "automation_id": "sun", + "type": "trace/get", + "domain": "automation", + "item_id": "sun", "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["state"] == "stopped" assert trace["trigger"] == "event 'test_event'" - assert trace["unique_id"] == "sun" + assert trace["item_id"] == "sun" assert trace["variables"] contexts[trace["context"]["id"]] = { "run_id": trace["run_id"], - "automation_id": trace["automation_id"], + "domain": "automation", + "item_id": trace["item_id"], } # 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() # 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() assert response["success"] 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( { "id": next_id(), - "type": "automation/trace/get", - "automation_id": "moon", + "type": "trace/get", + "domain": "automation", + "item_id": "moon", "run_id": run_id, } ) @@ -154,11 +158,12 @@ async def test_get_automation_trace(hass, hass_ws_client): assert "error" not in trace assert trace["state"] == "stopped" assert trace["trigger"] == "event 'test_event2'" - assert trace["unique_id"] == "moon" + assert trace["item_id"] == "moon" assert trace["variables"] contexts[trace["context"]["id"]] = { "run_id": trace["run_id"], - "automation_id": trace["automation_id"], + "domain": "automation", + "item_id": trace["item_id"], } # 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() # 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() assert response["success"] 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( { "id": next_id(), - "type": "automation/trace/get", - "automation_id": "moon", + "type": "trace/get", + "domain": "automation", + "item_id": "moon", "run_id": run_id, } ) @@ -192,11 +198,12 @@ async def test_get_automation_trace(hass, hass_ws_client): assert "error" not in trace assert trace["state"] == "stopped" assert trace["trigger"] == "event 'test_event3'" - assert trace["unique_id"] == "moon" + assert trace["item_id"] == "moon" assert trace["variables"] contexts[trace["context"]["id"]] = { "run_id": trace["run_id"], - "automation_id": trace["automation_id"], + "domain": "automation", + "item_id": trace["item_id"], } # 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() # 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() assert response["success"] 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( { "id": next_id(), - "type": "automation/trace/get", - "automation_id": "moon", + "type": "trace/get", + "domain": "automation", + "item_id": "moon", "run_id": run_id, } ) @@ -233,15 +241,16 @@ async def test_get_automation_trace(hass, hass_ws_client): assert "error" not in trace assert trace["state"] == "stopped" assert trace["trigger"] == "event 'test_event2'" - assert trace["unique_id"] == "moon" + assert trace["item_id"] == "moon" assert trace["variables"] contexts[trace["context"]["id"]] = { "run_id": trace["run_id"], - "automation_id": trace["automation_id"], + "domain": "automation", + "item_id": trace["item_id"], } # 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() assert response["success"] assert response["result"] == contexts @@ -283,7 +292,7 @@ async def test_automation_trace_overflow(hass, 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() assert response["success"] assert response["result"] == [] @@ -294,7 +303,7 @@ async def test_automation_trace_overflow(hass, hass_ws_client): await hass.async_block_till_done() # 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() assert response["success"] 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 # 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") 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() assert response["success"] 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 int(moon_traces[0]["run_id"]) == int(moon_run_id) + 1 - assert ( - int(moon_traces[-1]["run_id"]) - == int(moon_run_id) + automation.trace.STORED_TRACES - ) + assert int(moon_traces[-1]["run_id"]) == int(moon_run_id) + STORED_TRACES 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() - 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() assert response["success"] assert response["result"] == [] 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() assert response["success"] @@ -380,14 +391,19 @@ async def test_list_automation_traces(hass, hass_ws_client): await hass.async_block_till_done() # 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() assert response["success"] assert len(response["result"]) == 1 assert len(_find_traces_for_automation(response["result"], "sun")) == 1 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() 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 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() assert response["success"] @@ -414,7 +435,7 @@ async def test_list_automation_traces(hass, hass_ws_client): await hass.async_block_till_done() # 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() assert response["success"] 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["timestamp"] 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] 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["timestamp"] 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] 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["timestamp"] 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] 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["timestamp"] 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): @@ -465,11 +486,11 @@ async def test_automation_breakpoints(hass, hass_ws_client): id += 1 return id - async def assert_last_action(automation_id, expected_action, expected_state): - await client.send_json({"id": next_id(), "type": "automation/trace/list"}) + async def assert_last_action(item_id, expected_action, expected_state): + await client.send_json({"id": next_id(), "type": "trace/list"}) response = await client.receive_json() 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["state"] == expected_state return trace["run_id"] @@ -508,24 +529,23 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "1", } ) response = await client.receive_json() assert not response["success"] - await client.send_json( - {"id": next_id(), "type": "automation/debug/breakpoint/list"} - ) + await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"}) response = await client.receive_json() assert response["success"] assert response["result"] == [] subscription_id = next_id() 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() assert response["success"] @@ -533,8 +553,9 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "action/1", } ) @@ -543,24 +564,33 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "action/5", } ) response = await client.receive_json() assert response["success"] - await client.send_json( - {"id": next_id(), "type": "automation/debug/breakpoint/list"} - ) + await client.send_json({"id": next_id(), "type": "trace/debug/breakpoint/list"}) response = await client.receive_json() assert response["success"] assert_lists_same( 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() run_id = await assert_last_action("sun", "action/1", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/1", "run_id": run_id, } @@ -578,8 +609,9 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/step", - "automation_id": "sun", + "type": "trace/debug/step", + "domain": "automation", + "item_id": "sun", "run_id": run_id, } ) @@ -589,7 +621,8 @@ async def test_automation_breakpoints(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/2", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/2", "run_id": run_id, } @@ -597,8 +630,9 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/continue", - "automation_id": "sun", + "type": "trace/debug/continue", + "domain": "automation", + "item_id": "sun", "run_id": run_id, } ) @@ -608,7 +642,8 @@ async def test_automation_breakpoints(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/5", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/5", "run_id": run_id, } @@ -616,8 +651,9 @@ async def test_automation_breakpoints(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/stop", - "automation_id": "sun", + "type": "trace/debug/stop", + "domain": "automation", + "item_id": "sun", "run_id": run_id, } ) @@ -636,11 +672,11 @@ async def test_automation_breakpoints_2(hass, hass_ws_client): id += 1 return id - async def assert_last_action(automation_id, expected_action, expected_state): - await client.send_json({"id": next_id(), "type": "automation/trace/list"}) + async def assert_last_action(item_id, expected_action, expected_state): + await client.send_json({"id": next_id(), "type": "trace/list"}) response = await client.receive_json() 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["state"] == expected_state return trace["run_id"] @@ -678,7 +714,7 @@ async def test_automation_breakpoints_2(hass, hass_ws_client): subscription_id = next_id() 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() assert response["success"] @@ -686,8 +722,9 @@ async def test_automation_breakpoints_2(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "action/1", } ) @@ -700,7 +737,8 @@ async def test_automation_breakpoints_2(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/1", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/1", "run_id": run_id, } @@ -718,8 +756,9 @@ async def test_automation_breakpoints_2(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "1", } ) @@ -743,11 +782,11 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): id += 1 return id - async def assert_last_action(automation_id, expected_action, expected_state): - await client.send_json({"id": next_id(), "type": "automation/trace/list"}) + async def assert_last_action(item_id, expected_action, expected_state): + await client.send_json({"id": next_id(), "type": "trace/list"}) response = await client.receive_json() 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["state"] == expected_state return trace["run_id"] @@ -785,7 +824,7 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): subscription_id = next_id() 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() assert response["success"] @@ -793,8 +832,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "action/1", } ) @@ -804,8 +844,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/set", - "automation_id": "sun", + "type": "trace/debug/breakpoint/set", + "domain": "automation", + "item_id": "sun", "node": "action/5", } ) @@ -818,7 +859,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/1", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/1", "run_id": run_id, } @@ -826,8 +868,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/continue", - "automation_id": "sun", + "type": "trace/debug/continue", + "domain": "automation", + "item_id": "sun", "run_id": run_id, } ) @@ -837,7 +880,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/5", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/5", "run_id": run_id, } @@ -845,8 +889,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/stop", - "automation_id": "sun", + "type": "trace/debug/stop", + "domain": "automation", + "item_id": "sun", "run_id": run_id, } ) @@ -859,8 +904,9 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): await client.send_json( { "id": next_id(), - "type": "automation/debug/breakpoint/clear", - "automation_id": "sun", + "type": "trace/debug/breakpoint/clear", + "domain": "automation", + "item_id": "sun", "node": "action/1", } ) @@ -873,7 +919,8 @@ async def test_automation_breakpoints_3(hass, hass_ws_client): response = await client.receive_json() run_id = await assert_last_action("sun", "action/5", "running") assert response["event"] == { - "automation_id": "sun", + "domain": "automation", + "item_id": "sun", "node": "action/5", "run_id": run_id, }