Fix WebSocket handshake error handling and unnecessary token refresh#6725
Open
Fix WebSocket handshake error handling and unnecessary token refresh#6725
Conversation
Raise HomeAssistantWSConnectionError instead of HomeAssistantAPIError for WebSocket handshake failures. The broader HomeAssistantAPIError was not caught by the fire-and-forget send path which only catches HomeAssistantWSError, resulting in "Task exception was never retrieved" errors when Core's WebSocket endpoint isn't ready. Additionally, narrow the retry catch in connect_websocket from HomeAssistantAPIError to HomeAssistantAuthError. The broad catch caused connection errors (not auth failures) to trigger unnecessary token refreshes and retries, spamming "Updated Home Assistant API token" logs. Also raise the log level for failed fire-and-forget WebSocket commands from debug to warning for better visibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed change
Fix two issues with WebSocket error handling when Core's WebSocket
endpoint isn't ready (e.g. when the
httpintegration fails to load):Unhandled exception:
connect_with_auth/connectraisedHomeAssistantAPIErrorfor handshake failures, but fire-and-forgetcallers only catch
HomeAssistantWSError(a subclass, not a parent).This caused "Task exception was never retrieved" errors. Fixed by
raising
HomeAssistantWSConnectionErrorinstead, which is thecorrect exception for connection-phase failures.
Unnecessary token refresh spam: The retry loop in
connect_websocketcaught the broadHomeAssistantAPIError, soconnection errors (not just auth failures) triggered token
invalidation and refresh. This caused "Updated Home Assistant API
token" to be logged on every failed WebSocket message. Fixed by
narrowing the catch to
HomeAssistantAuthError.Also raised the log level for failed fire-and-forget WebSocket commands
from debug to warning for better visibility.
Type of change
Additional information
Checklist
ruff format supervisor tests)If API endpoints or add-on configuration are added/changed: