feat(ws_bridge): add WebSocket bridge component to Home Assistant (no…#269
Merged
Conversation
… MQTT) Connects an ESP32 (ESP-IDF) directly to Home Assistant's /api/websocket and speaks the hass-ws-bridge protocol (github.com/eigger/hass-ws-bridge) to declare entities, push state, and receive commands — removing the need for an MQTT broker. Combined with a secure way to reach HA from outside the LAN (Nabu Casa remote UI, a reverse proxy with a valid cert), it can also remove the need for a VPN for remote devices. - ws_bridge.h/.cpp: hub Component. Uses ESP-IDF's esp_websocket_client (pulled in via esp32.add_idf_component, no framing/TLS hand-rolled) for wss://, a small state machine for the HA auth handshake (auth_required -> auth -> auth_ok -> ws_bridge/connect), and a lock-free SPSC queue (EventPool + LockFreeQueue) to hand transport events from the esp_websocket_client task to the main loop safely. - ws_protocol.h/.cpp: JSON message build/parse using ESPHome's existing json::build_json/parse_json helpers (ArduinoJson v7 API). - ws_bridge_device.h: shared entity mixin; the hub re-declares every registered entity (and its current state) on every (re)connect, per the protocol's reconnection guidance. - sensor/binary_sensor/switch/number/select/button: one hub + per-platform subfolder structure mirroring this repo's uartex component. Read-only platforms push via add_on_state_callback; control platforms route incoming HA commands to turn_on/turn_off/make_call().set_value()/ set_option()/press(). - Exposes keep_last_state_on_disconnect (sent in ws_bridge/connect) so a gateway's entities can keep their last state instead of going unavailable on disconnect, matching the hass-ws-bridge PR adding that option. Verified compiling for esp32dev (esp-idf) with ESPHome 2026.7, hub + all six platforms, zero errors/warnings. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
rx_accum_ (the WS fragment reassembly buffer) was only cleared after a complete message was dequeued. If the socket dropped mid-fragment (a message split across TCP segments that never finished), the leftover partial bytes would still be sitting in rx_accum_ when the connection came back, and would get silently prepended to the next connection's first fragmented message, corrupting the JSON. Clear rx_accum_ on WEBSOCKET_EVENT_CONNECTED/DISCONNECTED/ERROR/CLOSED (from the producer-task callback itself, preserving the single-writer invariant) so every connection starts message reassembly from a clean slate. Verified compiling for esp32dev (esp-idf) with ESPHome 2026.7. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Adds tests/components/ws_bridge/test.esp32-idf.yaml exercising the hub and all six platforms (sensor/binary_sensor/switch/number/select/button), required by the repo's CI workflow (.github/workflows/esphome.yml) for any modified component. Verified with `esphome config` and `esphome compile` run the same way CI does (relative external_components path, cwd inside tests/components/ws_bridge). Co-Authored-By: Claude Opus 4.8 <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.
… MQTT)
Connects an ESP32 (ESP-IDF) directly to Home Assistant's /api/websocket and speaks the hass-ws-bridge protocol (github.com/eigger/hass-ws-bridge) to declare entities, push state, and receive commands — removing the need for an MQTT broker. Combined with a secure way to reach HA from outside the LAN (Nabu Casa remote UI, a reverse proxy with a valid cert), it can also remove the need for a VPN for remote devices.
Verified compiling for esp32dev (esp-idf) with ESPHome 2026.7, hub + all six platforms, zero errors/warnings.