Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hlw8012]: new counter impl #6730

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open

[hlw8012]: new counter impl #6730

wants to merge 1 commit into from

Conversation

mknjc
Copy link
Contributor

@mknjc mknjc commented May 13, 2024

What does this implement/fix?

Replace the pulse counter with a specialized implementation.
The old implementation had worse resolution with low loads and fast update intervals.
Additionally, jitter in the update interval, caused by other components doesn't cause measurement errors anymore.
The new implementation reduces flash usage as long as the pulse counter isn't used somewhere else.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): -

Pull request in esphome-docs with documentation (if applicable): -

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
    Tested with a BL0937

Example entry for config.yaml:

sensor:
  - platform: hlw8012
    model: BL0937
    sel_pin: GPIO12
    cf_pin: GPIO04
    cf1_pin: GPIO05
    change_mode_every: 4
    current_resistor: 0.001
    voltage_divider: 1700
    update_interval: 3s

    current:
      name: "Test - Ampere"
      unit_of_measurement: A
      accuracy_decimals: 3
      icon: mdi:current-ac

    voltage:
      name: "Test - Volt"
      unit_of_measurement: V
      accuracy_decimals: 1
      icon: mdi:flash-outline

    power:
      name: "Test - Watt"
      unit_of_measurement: W
      id: "power_wattage"
      icon: mdi:gauge

    energy:
      name: "Test - Energy"

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented May 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.25%. Comparing base (4d8b5ed) to head (28ecb72).
Report is 572 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6730      +/-   ##
==========================================
+ Coverage   53.70%   54.25%   +0.54%     
==========================================
  Files          50       50              
  Lines        9408     9587     +179     
  Branches     1654     1689      +35     
==========================================
+ Hits         5053     5201     +148     
- Misses       4056     4062       +6     
- Partials      299      324      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* does not use the pulse counter anymore
* needs less flash and ram than the old implementation
* isn't influenced by update interval jitter
* increases resolution with small loads (< ~50W) and fast update intervals (< 10s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants