Repository navigation
Fix: Handle None attributes in history data - #2869
Conversation
Fixes crash: 'NoneType' object has no attribute 'pop'
Issue: Home Assistant history data can contain entries where 'attributes'
key exists but has a None value instead of a dictionary. This causes a
crash when trying to call .pop() on it.
Solution: Changed from checking key existence ('attributes' in entry) to
using entry.get('attributes') which returns None if the key doesn't exist
or if its value is None, and only processes attributes when it's truthy.
Reported by: daryl
There was a problem hiding this comment.
Pull Request Overview
This PR changes the conditional check for the presence of attributes in history entry dictionaries from using the in operator to using the .get() method.
- Changed attribute existence check from
if "attributes" in entry:toif entry.get("attributes"):
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Filter useless data from history data | ||
| for entry in new_history_data: | ||
| if "attributes" in entry: | ||
| if entry.get("attributes"): |
There was a problem hiding this comment.
The change from if \"attributes\" in entry: to if entry.get(\"attributes\"): alters the behavior. The original check verified the key's existence, while .get() returns the value and evaluates its truthiness. This means an empty dictionary {\"attributes\": {}} would be skipped with the new code but processed with the old code, preventing the filtering loop from executing when it should. Use if \"attributes\" in entry: to maintain the original behavior, or use if entry.get(\"attributes\") is not None: if you want to explicitly handle None values.
| if entry.get("attributes"): | |
| if "attributes" in entry: |
Fixes crash: 'NoneType' object has no attribute 'pop'
Issue: history data can contain entries where 'attributes' key exists but has a None value instead of a dictionary. This causes a crash when trying to call .pop() on it.
Solution: Changed from checking key existence ('attributes' in entry) to using entry.get('attributes') which returns None if the key doesn't exist or if its value is None, and only processes attributes when it's truthy.