8000
Skip to content

ina2xx should be total increasing for energy sensor - #10711

Merged
jesserockz merged 2 commits into
esphome:devfrom
mikelawrence:ina2xx-fix-energy-sensor
Sep 14, 2025
Merged

ina2xx should be total increasing for energy sensor#10711
jesserockz merged 2 commits into
esphome:devfrom
mikelawrence:ina2xx-fix-energy-sensor

Conversation

@mikelawrence
@mikelawrence mikelawrence commented Sep 14, 2025
Copy link
Copy Markdown
Contributor

What does this implement/fix?

The ina2xx component reports the energy sensor with device_class=energy and state_class=measurement. Home Assistant produces a warning on this.

Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:587
integration: Sensor (documentation, issues)

Entity sensor.parking_assistant_energy (<class 'homeassistant.components.esphome.sensor.EsphomeSensor'>) is 
using state class 'measurement' which is impossible considering device class ('energy') it is using; expected None 
or one of 'total', 'total_increasing'; Please update your configuration if your entity is manually configured, 
otherwise create a bug report at https://github.com/home-assistant/core/issues?

I think the energy sensor should be device_class=energy and state_class=total increasing.

While looking at the component I think the energy joules sensor is also reported incorrectly although it does not produce a warning because it is not declared with device class=energy. I think is should be the same as the energy sensor including state_class=total increasing.

I also wonder that both of these sensors have an accuracy_decimals=8.
8 decimal places is equivalent to a resolution of 10 microwatt hr (10e-6) resolution for the energy sensor which is not too bad.
8 decimal places for the energy_joules sensor is a bit odd. There are 3.6e6 J in 1 kWh, 8 decimal places would divide that further for resolution of something around 2.8 femtowatt hr (2.8e-15).

But maybe this is for a later PR...

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)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

No bug reports that I can find.

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

  • esphome/esphome-docs#

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# Example config.yaml
sensor:
  - platform: ina2xx_i2c
    id: pcb_power
    model: INA228
    address: 0x40
    shunt_resistance: 0.050ohm
    max_current: 3A
    adc_range: 0
    update_interval: 60s
    energy:
      name: "Energy kWh"
    energy_joules:
      name: "Energy Joules"

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:

@github-actions
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#10711
    components: [ina2xx_base]
    refresh: 1h

(Added by the PR bot)

@github-actions
Copy link
Copy Markdown
Contributor

👋 Hi there! This PR modifies 1 file(s) with codeowners.

@latonita - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

@codecov-commenter
codecov-commenter commented Sep 14, 2025
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.89%. Comparing base (7e6b11c) to head (1825805).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #10711   +/-   ##
=======================================
  Coverage   65.89%   65.89%           
=======================================
  Files          52       52           
  Lines       10560    10560           
  Branches     1416     1416           
=======================================
  Hits         6959     6959           
  Misses       3220     3220           
  Partials      381      381           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mikelawrence
Copy link
Copy Markdown
Contributor Author

If you want to test the PR you actually need this:

external_components:
  - source: github://pr#10711
    components: [ina2xx_base, ina2xx_i2c, ina2xx_spi]
    refresh: 1h

@mikelawrence
Copy link
Copy Markdown
Contributor Author

I do not have access to a SPI version of this sensor to test but this PR should not care what interface you use.

@jesserockz
Copy link
Copy Markdown
Member

If you want to test the PR you actually need this:

external_components:
  - source: github://pr#10711
    components: [ina2xx_base, ina2xx_i2c, ina2xx_spi]
    refresh: 1h

Given the files in the interface specific components have not changed, you shouldn't need to override them from this PR, they will load the external_component version of the _base which includes these changes.

@jesserockz jesserockz added this to the 2025.9.0b2 milestone Sep 14, 2025
@jesserockz
jesserockz merged commit f756de2 into esphome:dev Sep 14, 2025
31 checks passed
@jesserockz jesserockz mentioned this pull request Sep 15, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0