Skip to content

WebSocket connection issues #893

@sebjacob

Description

@sebjacob

Version of the custom_component

Latest version via HACS (installed 2026-03-09)

Configuration

# Hilo integration configuration via UI
# No custom yaml configuration - using default settings

Describe the bug

The Hilo integration is experiencing WebSocket connection issues that cause it to lose real-time connectivity and enter a reconnection loop. The integration periodically loses connection to Hilo services and gets stuck attempting to reconnect with authentication errors (401/404). This affects core functionality like real-time device updates and challenge notifications.

The issue appears to be in the WebSocket reconnection logic where token refresh attempts are made but the integration continues to use expired credentials, leading to endless reconnection loops every few minutes.

Debug log

2026-03-09 06:30:12.725 ERROR (MainThread) [pyhilo] Websocket: Received event to close connection: 257
2026-03-09 06:30:12.728 ERROR (MainThread) [pyhilo] Websocket: Closed while listening: Connection was closed.
2026-03-09 06:30:12.731 ERROR (MainThread) [pyhilo] Connection was closed.
Traceback (most recent call last):
  File "/usr/local/lib/python3.14/site-packages/pyhilo/websocket.py", line 351, in async_listen
    messages = await self._async_receive_json()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/site-packages/pyhilo/websocket.py", line 179, in _async_receive_json
    raise ConnectionClosedError("Connection was closed.")
pyhilo.exceptions.ConnectionClosedError: Connection was closed.

2026-03-09 06:30:17.875 ERROR (MainThread) [pyhilo] Unable to connect to WS server 401, message='Invalid response status', url='https://device.hiloenergie.com/client/?hub=devicehub&asrs.op=%2FDeviceHub&asrs_request_id=S6RSmf4CAAA%3D&id=6YGmcN_mDL-nNVdZyaU_KwW5wjaAo02&access_token=[REDACTED_TOKEN]
2026-03-09 06:30:17.878 WARNING (MainThread) [custom_components.hilo] Invalid credentials? Refreshing websocket infos

2026-03-09 06:30:22.722 ERROR (MainThread) [pyhilo] Websocket: Received event to close connection: 257
2026-03-09 06:30:22.723 ERROR (MainThread) [pyhilo] Websocket: Closed while listening: Connection was closed.
2026-03-09 06:30:22.723 ERROR (MainThread) [pyhilo] Connection was closed.
Traceback (most recent call last):
  File "/usr/local/lib/python3.14/site-packages/pyhilo/websocket.py", line 351, in async_listen
    messages = await self._async_receive_json()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/site-packages/pyhilo/websocket.py", line 179, in _async_receive_json
    raise ConnectionClosedError("Connection was closed.")
pyhilo.exceptions.ConnectionClosedError: Connection was closed.

2026-03-09 06:30:23.505 ERROR (MainThread) [pyhilo] Unable to connect to WS server 404, message='Invalid response status', url='https://device.hiloenergie.com/client/?hub=devicehub&asrs.op=%2FDeviceHub&asrs_request_id=4XSmFywDAAA%3D&id=LU2yMDobKNbI7TdFm4jDkgW5wjaAo02&access_token=[REDACTED_TOKEN]
2026-03-09 06:30:23.505 WARNING (MainThread) [custom_components.hilo] Invalid credentials? Refreshing websocket infos

2026-03-09 08:00:58.724 ERROR (MainThread) [pyhilo] Websocket: Received event to close connection: 257
pyhilo.exceptions.ConnectionClosedError: Connection was closed.
2026-03-09 08:01:01.724 ERROR (MainThread) [pyhilo] Websocket: Received event to close connection: 257
pyhilo.exceptions.NotConnectedError

Additional Analysis

After reviewing the source code, the issue appears to be in the WebSocket reconnection logic in custom_components/hilo/__init__.py lines 796-814:

  1. Token refresh logic order: The code cancels the WebSocket connection before refreshing the token, then attempts reconnection regardless of whether refresh succeeded
  2. Missing token validation: No validation that token refresh was successful before reconnecting
  3. Incomplete error handling: ConnectionClosedError not explicitly handled
  4. Fixed reconnect delay: 5-second delay for all errors creates rapid loops
  5. No exponential backoff: Failed auth attempts don't use backoff

Potential Solutions

  1. Fix token refresh logic order (refresh before cancel)
  2. Add token refresh validation
  3. Add specific ConnectionClosedError handling
  4. Implement exponential backoff for auth errors
  5. Use error-specific reconnection strategies

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions