Skip to content

DRV-28: Add Valve entity support#42

Open
svc-finitelabs[bot] wants to merge 39 commits intomainfrom
agent/DRV-28-valve-entity
Open

DRV-28: Add Valve entity support#42
svc-finitelabs[bot] wants to merge 39 commits intomainfrom
agent/DRV-28-valve-entity

Conversation

@svc-finitelabs
Copy link
Copy Markdown
Contributor

Summary

Add support for ESPHome valve entities in the control4-esphome driver.

What

Valve entities represent water valves, gas shutoffs, irrigation valves, and similar open/close devices. The implementation mirrors the existing Cover entity pattern:

  • Contact sensor bindings for open/closed state reporting
  • Relay bindings for open/close/stop control
  • Position-based state tracking (0.0 closed to 1.0 open)
  • Current operation tracking (idle/opening/closing)

Uses the valve_command RPC which was already defined in proto_schema.lua. Unlike Cover, Valve has no legacy command support, so the command handler is simpler.

Changes

  • src/esphome/entities/valve.lua — New valve entity handler (~190 lines, adapted from cover.lua)
  • drivers/esphome/driver.lua — Register ValveEntity in entity table
  • README.md — Mark Valve as supported in entity table
  • CHANGELOG.md — Add changelog entry

Testing

This follows the exact same patterns as Cover (contact sensors, relays, position/operation state). No new patterns introduced.

Fixes DRV-28

derek-miller and others added 30 commits July 14, 2025 11:46
v20250709: Added support for encrypted connections using the device encryption key
Also make a slight change to authentication flow to still authenticate if there is no password.
- Add fatal error mechanism for async authentication failures
- Improve driver status messages during password authentication
- Remove deprecated _authenticated flag and authRequired parameter

Fixes #12
…e tracking (#15)

* Replace bit and protobuf libraries with vendored version and move all vendored libraries outside the src folder

* Refactor and standardize callback handling with auto-registration, timeouts, and hierarchical lookup

* Add device log forwarding feature to ESPHome driver

* Upgrade bitn and protobuf libraries to v0.2.0

* Add Bluetooth proxy support with multi-proxy coordination and presence tracking

Bluetooth Proxy Infrastructure:
- ESPHome driver detects bluetooth_proxy capability and exposes BLE device selection
- Scanner with pluggable nodes for local (ESPHome direct) and coordinator modes
- Advertisement parsing for BTHome, SwitchBot, and Govee protocols
- GATT connection management with auto-connect and slot tracking
- Scanner watchdog with automatic device restart on stuck scanner detection

Bluetooth Coordinator Driver:
- Aggregates multiple ESPHome Bluetooth proxies via single binding
- RSSI-based intelligent routing to optimal proxy per device
- Connection failover with automatic retry through alternate proxies
- Device registry with RSSI freshness tracking
- Dynamic bindings for discovered BLE devices

Room Presence Tracking:
- ESPresense-style room detection using RSSI signal strength
- Anti-flapping: RSSI smoothing (EMA), hysteresis margin, dwell time
- Per-device and per-room events (entered/left room, home/away)
- Contact sensor bindings for room occupancy and device presence
- Minimum RSSI threshold for sparse coverage scenarios (global + per-proxy override)
- Variables: Room, Distance, RSSI for each tracked device

Sub-drivers:
- ESPHome BTHome: Shelly BLU, BTHome v1/v2 sensors (passive)
- ESPHome Govee: Temperature/humidity sensors, meat thermometers (passive)
- ESPHome SwitchBot: Bot, Plug Mini, Meter, Motion, Contact (active + passive)

Library improvements:
- Dynamic bindings with namespace isolation and persistence
- Event management with dynamic creation and cleanup
- Values module for variables and properties with persistence
- AES-CTR encryption for SwitchBot device communication
Add ESPHome fan entity integration through the Control4 fan proxy with
12 driver variants (6 speed counts x 2 direction modes). Includes
preprocessor enhancements for variant-level #ifdef conditions,
cross-product dimension definitions, and consolidated PDF generation.
New Yale/August BLE lock driver with secure GATT handshake, lock/unlock
control, DoorSense contact sensor, battery monitoring, and Yale Cloud
API integration for offline key provisioning. Supports persistent and
poll connection modes.

Bluetooth proxy: V3 BLE CCCD descriptor write support, consistent BLE
address derivation via BLEAddress.fromString, improved GATT logging.

Replaced custom Lua AES-CTR implementation with native C4 crypto.

Documentation: Yale compatibility table with supported lock models,
DoorSense calibration note, key rotation troubleshooting, encryption
tip for BLE-only proxies to reduce controller CPU load, BTHome and
coordinator style corrections.

Normalize action command names to underscore format across all drivers.
- Add run_test.sh as general-purpose test runner that sets up LUA_PATH,
  loads C4 shim, and passes env vars to test files
- Add runEventLoop() to c4_shim so tests don't duplicate the event loop
- Remove hardcoded passwords and encryption keys from examples
- Normalize test files: consistent env var usage, 2-space indent,
  explicit dependency requires
JSON serialization of 48-bit BLE addresses via SerializeSafe loses
precision in scientific notation (e.g., 266846339488132 becomes
266846339488130, off by 2). This caused the coordinator to send wrong
addresses to ESP proxies, resulting in 20-second connection timeouts.

Refactored so numeric addresses only exist inside client.lua at the
protobuf boundary. All layers above (coordinator, router, bluetooth
proxy capability) now pass MAC strings exclusively. The client converts
MAC to number internally right before protobuf encoding.

Also switches gen-squishy from system Lua to LuaJIT to match the
Control4 runtime, and adds a generated-file header to README.md.
* Fix listEntities() never resolving due to callback registration order

ListEntitiesDoneResponse has no `ifdef` option in its proto schema.
The entity type check was evaluated before the DoneResponse check,
causing it to be skipped as "Unknown entity type". The Done callback
was never registered, so the deferred never resolved and entity
discovery silently failed. Sub-drivers (lights, fans, locks) stayed
Disconnected.

Fix: check for ListEntitiesDoneResponse first, before the entity type
lookup.

* Fix sub-drivers staying Disconnected after parent connects

ListEntitiesDoneResponse has no `ifdef` option in its proto schema.
The entity type check rejected it as unknown before reaching the Done
callback registration, so the deferred never resolved and entity
discovery silently failed.

Fix: check for ListEntitiesDoneResponse first, before the entity type
lookup. Also log unknown entity types at trace level instead of
silently skipping them.
Apply control4-driver-template to manage shared infrastructure via Copier.
Adds copier answers, Makefile, CONTRIBUTING, and syncs shared libs.

Co-authored-by: Derek Miller <derek@finitelabs.com>
* AGENT-30: Deduplicate DoorSense contact sensor notifications

Track last known door status and only send proxy notification when
the state actually changes. Previously, updateDoorStatus() fired
SendToProxy on every poll cycle regardless of state change, causing
Control4 to log repeated contact sensor events every ~1 minute.

Reset lastDoorStatus to nil on disconnect/reconnect and driver reset
so the first poll after reconnect always reports the current state.

* refactor: persist door status via values lib instead of local variable

Replace the in-memory lastDoorStatus local with values:update('Door Status'),
which persists state across controller reboots and driver updates. The values
lib's update() method returns false when the value hasn't changed, providing
the same deduplication behavior while also surviving isolate restarts.

- Remove lastDoorStatus local variable and all reset sites
- Use values:update() return value for change detection
- Property updates now handled by values lib (no manual UpdateProperty)
- values:reset() in EC.Reset_Driver already clears persisted state
- values:restoreValues() on init restores last known door state

---------

Co-authored-by: OpenClaw <openclaw@dmiller.me>
Added missing Unreleased section to CHANGELOG.md documenting the
DoorSense contact sensor deduplication fix. Rebuilt to embed updated
changelog in README.

Refs: AGENT-30

Co-authored-by: OpenClaw <openclaw@dmiller.me>
Co-authored-by: OpenClaw <openclaw@dmiller.me>
* TPL-2: copier update to v0.4.0 - add GitHub Actions CI

* fix: regenerate README.md to match pandoc output

The committed README.md had a trailing backslash line break that pandoc
converts to two trailing spaces, causing the dirty-tree CI check to fail.

* fix: remove trailing whitespace from README.md

* fix: remove trailing backslash line break in source doc

Applies the same fix from README.md to the source documentation.
Addresses review comment on PR #26.

* fix: join reflowed lines in source doc and README to match pandoc output

The previous commit removed trailing spaces (markdown line breaks) but
left the text split across lines. Pandoc reflows this into a single
paragraph, causing the dirty-tree CI check to fail. Join the lines
to match pandoc's expected output.

---------

Co-authored-by: OpenClaw <openclaw@dmiller.me>
Co-authored-by: openclaw[bot] <openclaw[bot]@users.noreply.github.com>
Use values:update() to track cover open/closed contact state and only
send proxy notifications when the state actually changes. Prevents
duplicate history agent entries during open/close operations when
ESPHome sends multiple position updates.
Automatically creates a GitHub release when a version tag is pushed.
Waits for the existing build workflow to complete, downloads the oss
artifacts, and publishes with auto-generated release notes.
Co-authored-by: OpenClaw <openclaw@dmiller.me>
…ks (#33)

- Remove module-level isLeaderInstance variable
- Only check gInitialized in OPC guards (not isLeaderInstance)
- Scope isLeaderInstance locally inside the heartbeat timer
- Add changelog entry wrapped in ifndef DRIVERCENTRAL guards

Co-authored-by: svc-finitelabs[bot] <269744575+svc-finitelabs[bot]@users.noreply.github.com>
derek-miller and others added 4 commits March 26, 2026 12:12
* DRV-25: Add Select entity support

- Create src/esphome/entities/select.lua (~130 lines)
- STRING variable with current option, writable via variable writes
- selectRegistry pattern for programming commands (matches button.lua)
- Add 'Set Select' command with dynamic Select and Option dropdowns
- Register SelectEntity in driver.lua entity table
- Add Set Select command definition to driver.xml
- Update README: entity support table, variables table, commands table
- Add CHANGELOG entry

* fix: update source documentation with Select entity support

The README was updated manually but the source documentation
(drivers/esphome/www/documentation/index.md) still had Select marked
as unsupported. The build regenerates README from source via pandoc,
causing 'Uncommitted changes after build' CI failure.

Updates source documentation to match:
- Mark Select entity as supported (✅)
- Add Select variable row to Variables table
- Add Set Select command to Commands table
- Update notes to mention Select entities

* chore: fix README.md formatting for dirty tree check

* fix: use nil check instead of IsEmpty for option value

An empty string is a valid select option. Using IsEmpty would
reject it, preventing the command from executing. Check for nil
instead to only guard against missing parameters.

---------

Co-authored-by: svc-finitelabs[bot] <269744575+svc-finitelabs[bot]@users.noreply.github.com>
Co-authored-by: OpenClaw <openclaw@dmiller.me>
Co-authored-by: svc-finitelabs[bot] <svc-finitelabs[bot]@users.noreply.github.com>
* DRV-31: Add Event entity support

Implement ESPHome event entity handling for stateless triggers (button
presses, gestures, doorbell rings). Events create Control4 events for
programming and track the last event type in a variable.

Changes:
- Add src/esphome/entities/event.lua with discovered/updated handlers
- Register EventEntity in driver.lua entity table
- Fix client.lua subscribeStates to handle EventResponse (does not
  match *StateResponse pattern)
- Update README entity table, variables, and events documentation
- Add CHANGELOG entry

* fix: move event docs to source index.md so build doesn't produce uncommitted changes

README.md is generated from drivers/esphome/www/documentation/index.md
during the build. The event entity docs were added to README.md directly
but not the source doc, causing the build to overwrite them and fail the
uncommitted-changes check.

Also wraps long CHANGELOG.md line to satisfy prettier formatting.

* fix: align blockquote line wrap with pandoc output

---------

Co-authored-by: OpenClaw <openclaw@dmiller.me>
Co-authored-by: svc-finitelabs[bot] <svc-finitelabs[bot]@users.noreply.github.com>
* DRV-30: Add Date, Time, and Datetime entity support

- date.lua: formats as YYYY-MM-DD, writable (parses back to year/month/day)
- time.lua: formats as HH:MM:SS, writable (parses back to hour/minute/second)
- datetime.lua: converts epoch_seconds to YYYY-MM-DD HH:MM:SS, writable
- All three handle missing_state by setting variable to empty string
- Registered all three in driver.lua entity table
- Updated README, index.md, and CHANGELOG

* docs: alphabetically sort entity tables in documentation

* fix: re-sort documentation tables to match build output

The entity variable reference tables in README.md and the driver
documentation were manually ordered, but prettier --prose-wrap always
reformats them alphabetically during build. Commit the post-build
output so the dirty-tree CI check passes.

---------

Co-authored-by: svc-finitelabs[bot] <269744575+svc-finitelabs[bot]@users.noreply.github.com>
Co-authored-by: OpenClaw <openclaw@dmiller.me>
@derek-miller
Copy link
Copy Markdown
Contributor

Can you recommend any esphome devices with this entity? Or a way to build a mock one? I need something to test this with.

Add Voice Assistant footnote to entity table
@svc-finitelabs
Copy link
Copy Markdown
Contributor Author

Real ESPHome devices with native valve support are pretty niche — most are irrigation or water shutoff controllers:

  • Givenergy AIO and some Shelly Plus boards can expose valve entities, but they're not super common in the wild
  • ESP32 + relay + solenoid valve is probably the most realistic DIY option (wire a 12V solenoid valve to a relay module on an ESP32)

But the easiest path for testing is a template valve in ESPHome — no hardware needed. Flash any ESP32/ESP8266 you have lying around with this config:

valve:
  - platform: template
    name: "Test Valve"
    device_class: water
    optimistic: true
    has_position: true
    open_action:
      - logger.log: "Opening valve"
    close_action:
      - logger.log: "Closing valve"
    stop_action:
      - logger.log: "Stopping valve"

This gives you a fully functional valve entity that responds to open/close/stop commands and reports position, which covers all the code paths in the PR (contact sensor bindings, relay bindings, position tracking, operation state). optimistic: true means commands immediately update state without needing real hardware feedback.

If you want to test without any physical device at all, you could also use the ESPHome native API emulator approach, but the template valve on a spare ESP32 is probably the fastest.

@derek-miller
Copy link
Copy Markdown
Contributor

rebase and resolve conflicts

svc-finitelabs bot and others added 3 commits March 30, 2026 09:42
Add support for ESPHome valve entities (water valves, gas shutoffs,
irrigation valves). Mirrors the existing Cover entity pattern:

- Contact sensor bindings for open/closed state
- Relay bindings for open/close/stop control
- Position-based state tracking (0.0 closed to 1.0 open)
- Current operation tracking (idle/opening/closing)

Uses the valve_command RPC (already defined in proto_schema.lua).
No legacy command support needed (unlike Cover).
@svc-finitelabs svc-finitelabs bot force-pushed the agent/DRV-28-valve-entity branch from 826fcd1 to 1fc8345 Compare March 30, 2026 14:43
@svc-finitelabs
Copy link
Copy Markdown
Contributor Author

Rebased onto main and resolved conflicts:

  • CHANGELOG.md: Kept Event, Date/Time/Datetime, and Select entries from main; added Valve entry to Unreleased section with proper line wrapping
  • README.md + docs/index.md: Kept updated entity tables from main (Date ✅, Datetime ✅, Event ✅, Select ✅, Time ✅); flipped Valve from ❌ to ✅
  • driver.lua: Added ValveEntity require and entity registration alongside the new TimeEntity from main

All 3 commits cleanly rebased. Ready for review.

@svc-finitelabs
Copy link
Copy Markdown
Contributor Author

Build failed because the Lua formatter fixed indentation on the two new entries in the Entities table (TimeEntity and ValveEntity were missing leading spaces). Pushed the fix in 91d4a52 — should be green now.

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.

1 participant