Skip to content

Commit

Permalink
Fix missing context_id in script logbook entries (home-assistant#71602)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 9, 2022
1 parent 222baa5 commit d8336a5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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 @@ -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", {})
Expand Down

0 comments on commit d8336a5

Please sign in to comment.