Repository navigation
Add ESPHome DSMR meter driver + hot-reload site-meter swap - #248
Conversation
|
Approve — driver looks clean. Sign conversion at the boundary ( Could you rebase onto master before I merge? Looks like there may be conflicts after #309 / recent main changes. |
|
Friendly bump @erikarenhill — last green CI was 2026-05-09 and master has moved meaningfully since (configreload idempotency fix in #337 / #344, several main.go touches). The hot-reload site-meter swap should rebase cleanly against the new |
Local-HTTP meter driver for any ESPHome device running the `dsmr`
component (e.g. Sourceful Zap on the open-source firmware at
github.com/erikarenhill/sourceful-zap-esphome). Reads the standard
`web_server: version: 3` REST surface — `/sensor/<id>` and
`/text_sensor/<id>` — translates DSMR's split-direction reporting
(`power_consumed` − `power_produced`) into site-convention W, and
emits per-phase V/A/W plus lifetime kWh.
Verified against a live LGF5E360 meter at 192.168.1.147: every
active-power, voltage, and current channel matches the raw OBIS
telegram bit-for-bit; lifetime energy counters match within
~3 Wh of the stock Zap firmware (display rounding on a 22 MWh
counter — well below DSMR class-1 accuracy).
Robustness:
- Exponential backoff on consecutive failures (1 → 2 → 4 → 8 …
capped at 60 s) so a meter that drops off the LAN doesn't get
hammered every poll_ms.
- Reactive-power channels probed once on first poll. Firmwares
without them skip the eight reactive HTTP requests forever
(~80 ms saved per cycle on stripped-down DIY builds).
- Serial discovery: tries DSMR equipment_id first, falls back to
OBIS meter_identification (LGF5E360 etc.), falls through to
MAC ARP / endpoint hash if both empty.
UI fix: the new-device wizard's HTTP branch defaulted unknown HTTP
drivers to `{email, password, serial}` (cloud-creds shape) when the
catalog had no `connection_defaults.host`. Local-HTTP meter / PV /
battery drivers without a canned hostname (every ESPHome install
picks its own mDNS name from the YAML `name:` field) were
mis-classified. Added a capability gate — meter / pv / battery
caps default to `{host: ""}` regardless of connection_defaults.
Tests cover sign correctness (importing + exporting), per-phase
math, kWh→Wh conversion, custom-make config override, total-read
failure, reactive emission, capability-probe gating, backoff
sequence, recovery reset, and catalog metadata.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The is_site_meter flag is captured at three places during startup: ctrl.SiteMeterDriver (control.NewState at main.go:164), mpcSvc.SiteMeter (line ~634), and loadSvc.SiteMeter (line ~619). The configreload watcher previously refreshed grid_target / tolerance / slew rate / dispatch interval but ignored the site-meter pointer, so an operator moving `is_site_meter: true` between drivers via YAML edit + UI save ended up with grid_w pegged at 0 once the old driver stopped emitting — the dispatcher kept calling `tel.Get(<old driver>, DerMeter)`. The only workaround was a process restart, which surprised me when the ESPHome driver landed and is_site_meter was flipped onto it live. The watcher now updates ctrl.SiteMeterDriver inside the same ctrlMu block that already gates the other site-level mutations, and main.go's applier closure propagates the new name to mpcSvc.SiteMeter and loadSvc.SiteMeter (both pre-declared earlier so the closure can capture them by reference; loadSvc gained the same forward-declaration treatment mpcSvc has used since #121). Race story for the field writes on the two services: the assignment happens at most once per YAML save (~human cadence) and is read once per MPC replan / loadmodel-tick (~1 Hz). Aligned-string writes on amd64/arm64 don't tear in practice, and a one-tick stale value on these paths is harmless — the next tick observes the new name. This matches the pattern the existing applier already uses for ctrl.InverterGroups, ctrl.SupportsPVCurtail, etc. Test exercises a YAML rewrite that moves is_site_meter from ferroamp to zap-p1, asserts ctrl.SiteMeterDriver flips to "zap-p1" without a process restart. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
93fb955 to
48e8fa7
Compare
Summary
Two related changes shipped together because the second was uncovered by testing the first:
New ESPHome P1 / DSMR meter driver (
drivers/esphome_dsmr.lua) — local-HTTP read of any device running ESPHome'sdsmrcomponent (e.g. Sourceful Zap on the open-source firmware). Hits/sensor/<id>+/text_sensor/<id>from the standardweb_server: version: 3REST surface, translates DSMR's split-direction reporting (power_consumed−power_produced) into site-convention W, emits per-phase V/A/W + lifetime kWh.Hot-reload of
is_site_meter— flipping the flag in YAML used to require a process restart becausectrl.SiteMeterDriver,mpcSvc.SiteMeter, andloadSvc.SiteMeterwere all captured at startup and never refreshed. Discovered the hard way when commissioning the new driver: moved the flag, saved the YAML, watchedgrid_wpeg at 0.Driver verification
Verified against a live LGF5E360 meter at
192.168.1.147over multiple sample windows. Every active-power, voltage, and current channel matches the raw OBIS telegram bit-for-bit. Lifetime energy counters match within ~3 Wh of the stock Zap firmware (display rounding on a 22 MWh counter — well below DSMR class-1 accuracy).power_consumed−power_produced)·1000power_*_lNdiff·1000voltage_lNenergy_*·1000Driver robustness
equipment_idfirst, falls back to OBISmeter_identification(LGF5E360 etc.), falls through to MAC ARP / endpoint hash if both empty.UI fix that came along
The setup wizard's HTTP branch defaulted unknown HTTP drivers to
{email, password, serial}(cloud-creds shape) when the catalog had noconnection_defaults.host. Local-HTTP meter / PV / battery drivers without a canned hostname (every ESPHome install picks its own mDNS name from the YAMLname:field, so no universal default would be right) were mis-classified as cloud. Added a capability gate —meter/pv/batterycaps default to{host: ""}regardless ofconnection_defaults.Hot-reload fix details
configreload.Watcher.reload()now updatesctrl.SiteMeterDriverinside the samectrlMublock that gates the other site-level mutations. main.go's applier closure propagates the new name tompcSvc.SiteMeterandloadSvc.SiteMeter(both pre-declared earlier so the closure can capture them by reference;loadSvcgained the same forward-declaration treatmentmpcSvchas used since #121).Race story: the assignment happens at most once per YAML save (human cadence) and is read once per MPC replan / loadmodel-tick (~1 Hz). Aligned-string writes on amd64/arm64 don't tear in practice, and a one-tick stale value on these paths is harmless — the next tick observes the new name. Matches the pattern the existing applier already uses for
ctrl.InverterGroups,ctrl.SupportsPVCurtail,ctrl.SiteFuse*, etc.Test plan
go test ./internal/drivers -run ESPHomeDSMR -count=1(9 cases — sign correctness, per-phase math, energy conversion, custom-make config, total-read failure, reactive emission, capability-probe gating, backoff sequence + recovery, catalog entry)go test ./internal/configreload -count=1(4 cases including newTestWatcherUpdatesSiteMeterDriverOnReload)go test ./... -count=1 -short— whole module, 34 packages, all green including e2e192.168.1.147from amake devsession — driver booted, discovered serialLGF5E360, started emitting; flippingis_site_meterfromferroamp(sim) tozap-p1and saving the YAML movedgrid_wto matchmeter_wfrom the next tick onward, no process restart needed.🤖 Generated with Claude Code