Skip to content

Add BL0937 energy metering: Electrical Measurement + Metering clusters - #497

Open
XaviSentis wants to merge 1 commit into
romasku:mainfrom
XaviSentis:pr-bl0937-metering
Open

XaviSentis wants to merge 1 commit into
romasku:mainfrom
XaviSentis:pr-bl0937-metering

Conversation

@XaviSentis

Copy link
Copy Markdown

What

Adds optional power/energy metering for BL0937-based Tuya plugs (a very common metering chip on TS011F and similar devices). Generic feature only — no specific board is enabled here (see the follow-up PR for the Aubess PM).

How it works

  • New E config-str token for the pin wiring: E<CF><CF1><SEL>[i] (2-char pin names; trailing i inverts the SEL level). Parsed in config_parser, mirrors the existing token style.
  • BL0937 driver (base_components/metering_bl0937.c): counts CF/CF1 pulses via a small GPIO hardware-counter HAL (gpio_counter), multiplexes CF1 between voltage and current with SEL, and produces rmsVoltage / rmsCurrent / activePower / accumulated energy.
  • ZCL clusters (zigbee/metering_cluster.c): serves Electrical Measurement (0x0B04: rmsVoltage/Current/activePower + AC multiplier/divisor incl. acPowerMultiplier 0x0604) and Metering (0x0702: currentSummationDelivered, Status 0x0200, unit/multiplier/divisor/formatting/deviceType).
  • HAL fix (telink/hal/zigbee_zcl.c): get_register_func_by_cluster_id() now maps 0x0B04/0x0702 to a register function (via the generic zcl_registerCluster()). Without this the clusters are advertised in the simple descriptor but every attribute read returns UNSUPPORTED_ATTRIBUTE and no reports are emitted.
  • Z2M converter: the generator emits electricityMeter({ endpointNames: [...] }) for metering devices, so the exposed properties carry the endpoint suffix and match the reported data on multi-endpoint devices (otherwise HA discovers power/current entities that never receive data while the values land under power_switch/current_switch).

Notes

  • TC32 build fixes needed by the driver: 32-bit-only frequency math (the toolchain has no 64-bit division helpers) and stddef.h for NULL.
  • Calibration coefficients (DEFAULT_COEF_*) are placeholder defaults and need per-board calibration — a concrete calibrated board is added in the follow-up PR.
  • Out of scope: overcurrent protection and a flash/OEM scanner tool (kept on separate branches).

Testing

  • Host stub build compiles; clusters register with their full attribute tables.
  • Firmware build (Telink TLSR8258) passes.
  • Exercised end-to-end on hardware in the follow-up (Aubess PM) PR.

Adds optional power/energy metering for BL0937-based Tuya plugs (generic; no
board enabled here).

- New `E` config-str token for the pin wiring: E<CF><CF1><SEL>[i] (2-char pin
  names; trailing `i` inverts SEL). Parsed in config_parser.
- BL0937 driver + a small GPIO hardware-counter HAL: counts CF/CF1 pulses,
  multiplexes CF1 between voltage and current via SEL, produces
  rmsVoltage/rmsCurrent/activePower and accumulated energy.
- ZCL clusters (metering_cluster): Electrical Measurement 0x0B04 (rms V/I/P +
  AC multiplier/divisor incl. acPowerMultiplier 0x0604) and Metering 0x0702
  (currentSummationDelivered, Status 0x0200, unit/multiplier/divisor/
  formatting/deviceType).
- HAL fix (telink zigbee_zcl): get_register_func_by_cluster_id() now maps
  0x0B04/0x0702 to a register function (via generic zcl_registerCluster), so
  their attributes are actually served -- previously the clusters were
  advertised but every attribute read returned UNSUPPORTED_ATTRIBUTE and no
  reports were emitted.
- Z2M converter generator: emits electricityMeter({ endpointNames: [...] }) for
  metering devices so exposes match the reported data on multi-endpoint devices.
- TC32 build fixes: 32-bit-only frequency math, stddef for NULL.

Coefficients are placeholder defaults and need per-board calibration.
@XaviSentis

Copy link
Copy Markdown
Author

Rebased onto current main. The only conflict was the modernExtend imports in switch_custom.js.jinja (deviceAddCustomCluster is no longer imported upstream, so this PR now only adds electricityMeter). I also ran uncrustify so the lint job passes, and make tests passes locally (231 passed). This has been running on a real Aubess PM plug (TS011F / _TZ3000_gvn91tmx) since the end of August, reporting voltage, current, power and energy. The checks show "No jobs were run" because it's my first PR here, so they need approval to run. Happy to change anything — thanks!

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