Skip to content

Use parent_id to find cause of logbook events with new contexts#44416

Merged
bdraco merged 3 commits into
home-assistant:devfrom
chishm:logbook-parent-context
Jan 8, 2021
Merged

Use parent_id to find cause of logbook events with new contexts#44416
bdraco merged 3 commits into
home-assistant:devfrom
chishm:logbook-parent-context

Conversation

@chishm
Copy link
Copy Markdown
Contributor

@chishm chishm commented Dec 21, 2020

When looking up the causing event for logbook display, use the parent_id
of the current context if the current context just points back to the
current event.

This now shows in the logbook the cause of an event in the case that a
component has created a new context from an existing context and tied
them together via the Context.parent_id.

Proposed change

With #21028, new contexts are generated and linked to a causing event via the Context's parent_id attribute. The creation of a new context has the side-effect that the logbook no longer displays the cause of an event if that event has a chained context.

This is a change to the logbook such that when it is looking up the cause of an event for display, it will try to use the parent_id
of the current context if the current context just points back to the current event. This means the logbook will show the cause of a chained event.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @chishm,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@MartinHjelmare MartinHjelmare changed the title Use parent_id to find cause of events with new contexts Use parent_id to find cause of logbook events with new contexts Dec 25, 2020
Comment thread tests/components/logbook/test_init.py Outdated
Comment thread tests/components/logbook/test_init.py Outdated
Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing a traceback while testing

2021-01-01 01:32:46 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 118, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 72, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 129, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/logbook/__init__.py", line 251, in get
    return await hass.async_add_executor_job(json_events)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/logbook/__init__.py", line 240, in json_events
    _get_events(
  File "/usr/src/homeassistant/homeassistant/components/logbook/__init__.py", line 474, in _get_events
    return list(
  File "/usr/src/homeassistant/homeassistant/components/logbook/__init__.py", line 343, in humanify
    _augment_data_with_context(
  File "/usr/src/homeassistant/homeassistant/components/logbook/__init__.py", line 612, in _augment_data_with_context
    event_type = context_event.event_type
AttributeError: 'NoneType' object has no attribute 'event_type'

if event.context_parent_id:
context_event = context_lookup.get(event.context_parent_id)
# Ensure the (parent) context_event is not the root cause of this log entry
if event == context_event:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if event == context_event:
if not context_event or event == context_event:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have a test for this case as well

chishm added 3 commits January 8, 2021 12:28
When looking up the causing event for logbook display, use the `parent_id`
of the current context if the current context just points back to the
current event.

This now shows in the logbook the cause of an event in the case that a
component has created a new context from an existing context and tied
them together via the `Context.parent_id`.
@chishm
Copy link
Copy Markdown
Contributor Author

chishm commented Jan 8, 2021

Thank you for the code review. This is my first PR to HA and all of your review comments have been quite helpful.

I've made the requested changes and added a test case that replicates the circumstances of that NoneType exception.

Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. If there are any additional review comments from another reviewer, please address them in a followup PR.

@bdraco bdraco merged commit e35e460 into home-assistant:dev Jan 8, 2021
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants