From ca74e71348eec007e2ed820fa7848f3e14a53722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Metrich?= <45318189+FredM67@users.noreply.github.com> Date: Mon, 5 Oct 2026 16:16:21 +0200 Subject: [PATCH] [emontx] Use total state class for energy sensors (#20128) Co-authored-by: Claude Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- esphome/components/emontx/sensor/__init__.py | 3 ++- tests/component_tests/emontx/test_sensor_defaults.py | 11 +++++------ tests/components/emontx/validate.esp32-idf.yaml | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/esphome/components/emontx/sensor/__init__.py b/esphome/components/emontx/sensor/__init__.py index 56a7fb8b55..eadba1ea3f 100644 --- a/esphome/components/emontx/sensor/__init__.py +++ b/esphome/components/emontx/sensor/__init__.py @@ -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": { diff --git a/tests/component_tests/emontx/test_sensor_defaults.py b/tests/component_tests/emontx/test_sensor_defaults.py index 5c6a8e4108..2d64f92be6 100644 --- a/tests/component_tests/emontx/test_sensor_defaults.py +++ b/tests/component_tests/emontx/test_sensor_defaults.py @@ -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), diff --git a/tests/components/emontx/validate.esp32-idf.yaml b/tests/components/emontx/validate.esp32-idf.yaml index 882ee26fcb..548a3c9c41 100644 --- a/tests/components/emontx/validate.esp32-idf.yaml +++ b/tests/components/emontx/validate.esp32-idf.yaml @@ -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