Skip to content

Fix: Handle None attributes in history data - #2869

Merged
springfall2008 merged 1 commit into
springfall2008:mainfrom
mgazza:fix/attributes-none-crash
Nov 7, 2025
Merged

springfall2008 merged 1 commit into
springfall2008:mainfrom
mgazza:fix/attributes-none-crash

Conversation

@mgazza

@mgazza mgazza commented Nov 6, 2025 •

Copy link
Copy Markdown
Collaborator

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.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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: to if entry.get("attributes"):

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/predbat/ha.py
# Filter useless data from history data
for entry in new_history_data:
if "attributes" in entry:
if entry.get("attributes"):

Copilot AI Nov 7, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
if entry.get("attributes"):
if "attributes" in entry:

Copilot uses AI. Check for mistakes.
@springfall2008
springfall2008 merged commit fad9c6b into springfall2008:main Nov 7, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants