Skip to content

Fix WebSocket handshake error handling and unnecessary token refresh#6725

Open
agners wants to merge 1 commit intomainfrom
improve-websocket-error-handling
Open

Fix WebSocket handshake error handling and unnecessary token refresh#6725
agners wants to merge 1 commit intomainfrom
improve-websocket-error-handling

Conversation

@agners
Copy link
Copy Markdown
Member

@agners agners commented Apr 10, 2026

Proposed change

Fix two issues with WebSocket error handling when Core's WebSocket
endpoint isn't ready (e.g. when the http integration fails to load):

  1. Unhandled exception: connect_with_auth/connect raised
    HomeAssistantAPIError for handshake failures, but fire-and-forget
    callers only catch HomeAssistantWSError (a subclass, not a parent).
    This caused "Task exception was never retrieved" errors. Fixed by
    raising HomeAssistantWSConnectionError instead, which is the
    correct exception for connection-phase failures.

  2. Unnecessary token refresh spam: The retry loop in
    connect_websocket caught the broad HomeAssistantAPIError, so
    connection 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

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:
  • Link to client library pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints or add-on configuration are added/changed:

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>
@agners agners added the bugfix A bug fix label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant