Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/script/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def async_describe_logbook_event(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)
62 changes: 62 additions & 0 deletions tests/components/logbook/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,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", {})
Expand Down