[emontx] Use total state class for energy sensors (#20128)

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
Frédéric Metrich
2026-10-05 09:16:21 -05:00
committed by GitHub
co-authored by Claude pre-commit-ci-lite[bot]
parent 00303fb72b
commit ca74e71348
3 changed files with 8 additions and 8 deletions
+2 -1
View File
@@ -16,6 +16,7 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL,
STATE_CLASS_TOTAL_INCREASING,
UNIT_AMPERE,
UNIT_CELSIUS,
@@ -70,7 +71,7 @@ SENSOR_CONFIGS = {
"E": {
CONF_UNIT_OF_MEASUREMENT: UNIT_WATT_HOURS,
CONF_DEVICE_CLASS: DEVICE_CLASS_ENERGY,
CONF_STATE_CLASS: STATE_CLASS_TOTAL_INCREASING,
CONF_STATE_CLASS: STATE_CLASS_TOTAL,
CONF_ACCURACY_DECIMALS: 0,
},
"V": {
@@ -18,6 +18,7 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL,
STATE_CLASS_TOTAL_INCREASING,
UNIT_AMPERE,
UNIT_CELSIUS,
@@ -42,14 +43,12 @@ def _resolve_via_config_schema(tag: str) -> dict:
def test_config_schema_applies_tag_default_state_class():
"""If sensor_schema(state_class=...) is reintroduced, the schema-level
default wins over apply_tag_defaults' per-prefix value, and E1 would
resolve to measurement instead of total_increasing. Driving the real
resolve to measurement instead of total. Driving the real
CONFIG_SCHEMA (not just apply_tag_defaults) catches that, since
sensor_schema() runs before apply_tag_defaults in the cv.All() chain.
"""
result = _resolve_via_config_schema("E1")
assert result[CONF_STATE_CLASS] == sensor.validate_state_class(
STATE_CLASS_TOTAL_INCREASING
)
assert result[CONF_STATE_CLASS] == sensor.validate_state_class(STATE_CLASS_TOTAL)
def test_config_schema_applies_tag_default_accuracy_decimals():
@@ -70,8 +69,8 @@ def _make_config(tag: str) -> dict:
("tag", "expected_state_class", "expected_decimals"),
[
# Known numeric-index prefixes
("E1", STATE_CLASS_TOTAL_INCREASING, 0),
("E12", STATE_CLASS_TOTAL_INCREASING, 0),
("E1", STATE_CLASS_TOTAL, 0),
("E12", STATE_CLASS_TOTAL, 0),
("P1", STATE_CLASS_MEASUREMENT, 0),
("V1", STATE_CLASS_MEASUREMENT, 2),
("I1", STATE_CLASS_MEASUREMENT, 2),
@@ -8,7 +8,7 @@ packages:
# exercised, not the user-override path.
sensor:
# Energy sensor (E prefix): expects state_class=total_increasing, unit=Wh,
# Energy sensor (E prefix): expects state_class=total, unit=Wh,
# device_class=energy, accuracy_decimals=0
- platform: emontx
tag_name: E1