From d8336a521652b7fe7c0c0a78bced558b1d35dd73 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 9 May 2022 15:22:27 -0500 Subject: [PATCH] Fix missing context_id in script logbook entries (#71602) --- homeassistant/components/script/logbook.py | 1 + tests/components/logbook/test_init.py | 62 ++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/homeassistant/components/script/logbook.py b/homeassistant/components/script/logbook.py index f75584540d3a..250b7231b32e 100644 --- a/homeassistant/components/script/logbook.py +++ b/homeassistant/components/script/logbook.py @@ -17,6 +17,7 @@ def async_describe_events(hass, async_describe_event): "name": data.get(ATTR_NAME), "message": "started", "entity_id": data.get(ATTR_ENTITY_ID), + "context_id": event.context_id, } async_describe_event(DOMAIN, EVENT_SCRIPT_STARTED, async_describe_logbook_event) diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index e55fca1133bd..4f961dcd2c11 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -1002,6 +1002,68 @@ async def test_logbook_entity_context_id(hass, recorder_mock, hass_client): assert json_dict[7]["context_user_id"] == "9400facee45711eaa9308bfd3d19e474" +async def test_logbook_context_id_automation_script_started_manually( + hass, recorder_mock, hass_client +): + """Test the logbook populates context_ids for scripts and automations started manually.""" + await async_setup_component(hass, "logbook", {}) + await async_setup_component(hass, "automation", {}) + await async_setup_component(hass, "script", {}) + + await async_recorder_block_till_done(hass) + + # An Automation + automation_entity_id_test = "automation.alarm" + automation_context = ha.Context( + id="fc5bd62de45711eaaeb351041eec8dd9", + user_id="f400facee45711eaa9308bfd3d19e474", + ) + hass.bus.async_fire( + EVENT_AUTOMATION_TRIGGERED, + {ATTR_NAME: "Mock automation", ATTR_ENTITY_ID: automation_entity_id_test}, + context=automation_context, + ) + script_context = ha.Context( + id="ac5bd62de45711eaaeb351041eec8dd9", + user_id="b400facee45711eaa9308bfd3d19e474", + ) + hass.bus.async_fire( + EVENT_SCRIPT_STARTED, + {ATTR_NAME: "Mock script", ATTR_ENTITY_ID: "script.mock_script"}, + context=script_context, + ) + + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done() + await async_wait_recording_done(hass) + + client = await hass_client() + + # Today time 00:00:00 + start = dt_util.utcnow().date() + start_date = datetime(start.year, start.month, start.day) + + # Test today entries with filter by end_time + end_time = start + timedelta(hours=24) + response = await client.get( + f"/api/logbook/{start_date.isoformat()}?end_time={end_time}" + ) + assert response.status == HTTPStatus.OK + json_dict = await response.json() + + assert json_dict[0]["entity_id"] == "automation.alarm" + assert "context_entity_id" not in json_dict[0] + assert json_dict[0]["context_user_id"] == "f400facee45711eaa9308bfd3d19e474" + assert json_dict[0]["context_id"] == "fc5bd62de45711eaaeb351041eec8dd9" + + assert json_dict[1]["entity_id"] == "script.mock_script" + assert "context_entity_id" not in json_dict[1] + assert json_dict[1]["context_user_id"] == "b400facee45711eaa9308bfd3d19e474" + assert json_dict[1]["context_id"] == "ac5bd62de45711eaaeb351041eec8dd9" + + assert json_dict[2]["domain"] == "homeassistant" + + async def test_logbook_entity_context_parent_id(hass, hass_client, recorder_mock): """Test the logbook view links events via context parent_id.""" await async_setup_component(hass, "logbook", {})