Skip to content

feat(v2): add websocket client with typed events, reconnect, and tests#602

Open
tomquist wants to merge 1 commit intohomewizard:mainfrom
tomquist:feat/websocket-support
Open

feat(v2): add websocket client with typed events, reconnect, and tests#602
tomquist wants to merge 1 commit intohomewizard:mainfrom
tomquist:feat/websocket-support

Conversation

@tomquist
Copy link

@tomquist tomquist commented Mar 5, 2026

Summary

This PR adds HomeWizard v2 WebSocket support (/api/ws) with authentication, reconnect handling, typed events, docs, and tests.

Added

  • New HomeWizardEnergyWebSocket client in homewizard_energy/v2/websocket.py
  • HomeWizardEnergyV2.websocket() helper to create a websocket client
  • Public exports:
    • HomeWizardEnergyWebSocket
    • WebSocketTopic
    • HomeWizardEnergyWebSocketEvent

WebSocket behavior

  • Connects to wss://<host>/api/ws
  • Handshake flow:
    • wait for authorization_requested
    • send authorization with token
    • require authorized
  • Token is required (UnauthorizedError when missing)
  • Keeps handshake serialized under lock and only exposes self._ws after auth completes
  • Handles dropped connections with reconnect/backoff
    • exponential backoff with jitter
    • avoids reconnect loops for externally owned + closed sessions

API additions

  • Raw API remains available:
    • send, receive, events, subscribe, unsubscribe, identify, request
  • Typed API added:
    • receive_typed()
    • events_typed(reconnect=...)
    • WebSocketTopic enum for topic-safe subscribe/request calls
  • Best-effort model decoding for known topics:
    • device -> Device
    • measurement -> Measurement
    • system -> System
    • batteries -> Batteries

Error handling / lifecycle

  • Normalizes websocket receive/send failures to RequestError
  • Handles websocket { "type": "error" } payloads as RequestError
  • Guards against shared-session lifecycle issues and clarifies ownership in docs
  • Serializes close/reconnect teardown with lock

Documentation

  • Updated README.md websocket example to use typed API (WebSocketTopic, events_typed)

Tests

Added tests/v2/test_v2_websocket.py covering:

  • token requirement and handshake
  • invalid handshake
  • helper payloads for subscribe/unsubscribe/identify/request
  • websocket error payload handling
  • invalid JSON shape handling
  • reconnect on dropped socket
  • reconnect after transient connect failure
  • no reconnect when externally owned session is closed
  • typed event decoding

Note that I wasn't able to test this change E2E because I don't own a HomeWizard device.

Validation

  • pre-commit run --all-files
  • websocket/v2 targeted tests pass locally

Fixes #599

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.

Add WebSocket support for real-time updates

1 participant