Skip to content

Handle invalid XiaoAI conversation data - #2891

Open
binbin2333 wants to merge 1 commit into
al-one:masterfrom
binbin2333:fix/xiaoai-null-conversation-data
Open

Handle invalid XiaoAI conversation data#2891
binbin2333 wants to merge 1 commit into
al-one:masterfrom
binbin2333:fix/xiaoai-null-conversation-data

Conversation

@binbin2333

Copy link
Copy Markdown

Summary

  • Treat null or unexpected Mina conversation data payloads as no update.
  • Preserve the last valid conversation and its attributes.
  • Add regression coverage for null and non-dict payloads.

Problem

The Mina conversation endpoint may intermittently return data: null.
The sensor currently attempts to call json.loads(None), logs a
TypeError for the affected poll, and replaces the current conversation
attributes with empty values.

Tests

  • python -m pytest -q tests/test_xiaoai_conversation_sensor.py
    • 2 passed

Fixes #2802

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request improves error handling in fetch_latest_message within sensor.py by returning an empty dictionary early when the response data is None or not a dictionary, and adds a corresponding unit test. The reviewer suggested handling potential JSON decoding errors explicitly to prevent falling into the outer exception block, which would otherwise clear the last valid conversation state.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +603 to 604
if isinstance(rdt, (str, bytes, bytearray)):
rdt = json.loads(rdt) or {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If json.loads(rdt) raises a JSONDecodeError (which is a subclass of ValueError), the execution falls into the outer except block. Currently, that block sets rdt = {} and continues execution, which clears the conversation and its attributes (setting them to empty/None).

To fully satisfy the PR's goal of preserving the last valid conversation on unexpected payloads or failures, we should handle JSON parsing errors gracefully by returning {} immediately. Additionally, you should consider updating the outer except block to also return {} instead of setting rdt = {} so that connection or API errors do not clear the last valid conversation.

Suggested change
if isinstance(rdt, (str, bytes, bytearray)):
rdt = json.loads(rdt) or {}
if isinstance(rdt, (str, bytes, bytearray)):
try:
rdt = json.loads(rdt) or {}
except (TypeError, ValueError):
return {}

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.

小爱音响警告

1 participant