Use parent_id to find cause of logbook events with new contexts#44416
Merged
Conversation
Contributor
bdraco
reviewed
Jan 1, 2021
bdraco
reviewed
Jan 1, 2021
bdraco
requested changes
Jan 1, 2021
Member
bdraco
left a comment
There was a problem hiding this comment.
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'
bdraco
reviewed
Jan 1, 2021
| 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: |
Member
There was a problem hiding this comment.
Suggested change
| if event == context_event: | |
| if not context_event or event == context_event: |
Member
There was a problem hiding this comment.
We should probably have a test for this case as well
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`.
Contributor
Author
|
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. |
bdraco
approved these changes
Jan 8, 2021
Member
bdraco
left a comment
There was a problem hiding this comment.
LGTM. If there are any additional review comments from another reviewer, please address them in a followup PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When looking up the causing event for logbook display, use the
parent_idof 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'sparent_idattribute. 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_idof 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
Checklist
black --fast homeassistant tests)The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: