From adacbcdbdc1ab64327916affdef9340612de17a7 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 9 Oct 2026 09:55:50 +0200 Subject: [PATCH] Do not link a trace that is not stored as a child (#185207) --- homeassistant/components/trace/models.py | 3 +- tests/components/trace/test_websocket_api.py | 52 +++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/trace/models.py b/homeassistant/components/trace/models.py index 89b2d2d3ddd6..d3a42ede803d 100644 --- a/homeassistant/components/trace/models.py +++ b/homeassistant/components/trace/models.py @@ -93,7 +93,8 @@ class ActionTrace(BaseTrace): self.key = f"{self._domain}.{item_id}" self._dict: dict[str, Any] | None = None self._short_dict: dict[str, Any] | None = None - if trace_id_get(): + # An automation without an id stores no trace, so there is nothing to link + if item_id and trace_id_get(): trace_set_child_id(self.key, self.run_id) trace_id_set((self.key, self.run_id)) diff --git a/tests/components/trace/test_websocket_api.py b/tests/components/trace/test_websocket_api.py index de2c9f72f5ed..2b8ef93dfa4e 100644 --- a/tests/components/trace/test_websocket_api.py +++ b/tests/components/trace/test_websocket_api.py @@ -19,7 +19,7 @@ from homeassistant.helpers.typing import UNDEFINED from homeassistant.setup import async_setup_component from homeassistant.util.uuid import random_uuid_hex -from tests.common import async_load_json_object_fixture +from tests.common import async_capture_events, async_load_json_object_fixture from tests.typing import WebSocketGenerator @@ -1696,6 +1696,56 @@ async def test_automation_without_id_does_not_store_trace( assert "automation.None" not in hass.data[DATA_TRACE] +async def test_automation_without_id_is_not_linked_as_child( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """A step that triggers an automation without an id does not link to it.""" + await _setup_automation_or_script( + hass, + "automation", + [ + { + "id": "sun", + "triggers": {"platform": "event", "event_type": "test_event"}, + "actions": { + "action": "automation.trigger", + "target": {"entity_id": "automation.no_id"}, + }, + }, + { + "alias": "No id", + "triggers": {"platform": "event", "event_type": "never_fired"}, + "actions": {"event": "automation_ran"}, + }, + ], + ) + events = async_capture_events(hass, "automation_ran") + + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(events) == 1 + + client = await hass_ws_client() + await client.send_json({"id": 1, "type": "trace/list", "domain": "automation"}) + response = await client.receive_json() + assert response["success"] + sun_run_id = _find_run_id(response["result"], "automation", "sun") + + await client.send_json( + { + "id": 2, + "type": "trace/get", + "domain": "automation", + "item_id": "sun", + "run_id": sun_run_id, + } + ) + response = await client.receive_json() + assert response["success"] + assert "child_id" not in response["result"]["trace"]["action/0"][0] + + class _DiagnosticActionTrace(ActionTrace): """Automation-domain trace used to exercise not-triggered serialization."""