[dlms_meter] Spell out each removed key's settings and test the errors

This commit is contained in:
J. Nick Koston
2026-10-08 11:23:21 -10:00
parent 134ded3331
commit 3f21d68ae3
4 changed files with 62 additions and 16 deletions
@@ -9,20 +9,31 @@ DEPENDENCIES = ["dlms_meter"]
# Removed in 2026.11.0 - kept to provide helpful error message
# Remove before 2027.5.0
_MEASUREMENT = "state_class: measurement"
_VOLTAGE = f"unit_of_measurement: V, accuracy_decimals: 1, device_class: voltage, {_MEASUREMENT}"
_CURRENT = f"unit_of_measurement: A, accuracy_decimals: 2, device_class: current, {_MEASUREMENT}"
_POWER = (
f"unit_of_measurement: W, accuracy_decimals: 0, device_class: power, {_MEASUREMENT}"
)
_ENERGY = (
"unit_of_measurement: Wh, accuracy_decimals: 0, device_class: energy, "
"state_class: total_increasing"
)
_POWER_FACTOR = f"accuracy_decimals: 3, device_class: power_factor, {_MEASUREMENT}"
REMOVED_KEYS = {
"voltage_l1": "1.0.32.7.0.255",
"voltage_l2": "1.0.52.7.0.255",
"voltage_l3": "1.0.72.7.0.255",
"current_l1": "1.0.31.7.0.255",
"current_l2": "1.0.51.7.0.255",
"current_l3": "1.0.71.7.0.255",
"active_power_plus": "1.0.1.7.0.255",
"active_power_minus": "1.0.2.7.0.255",
"active_energy_plus": "1.0.1.8.0.255",
"active_energy_minus": "1.0.2.8.0.255",
"reactive_energy_plus": "1.0.3.8.0.255",
"reactive_energy_minus": "1.0.4.8.0.255",
"power_factor": "1.0.13.7.0.255",
"voltage_l1": ("1.0.32.7.0.255", _VOLTAGE),
"voltage_l2": ("1.0.52.7.0.255", _VOLTAGE),
"voltage_l3": ("1.0.72.7.0.255", _VOLTAGE),
"current_l1": ("1.0.31.7.0.255", _CURRENT),
"current_l2": ("1.0.51.7.0.255", _CURRENT),
"current_l3": ("1.0.71.7.0.255", _CURRENT),
"active_power_plus": ("1.0.1.7.0.255", _POWER),
"active_power_minus": ("1.0.2.7.0.255", _POWER),
"active_energy_plus": ("1.0.1.8.0.255", _ENERGY),
"active_energy_minus": ("1.0.2.8.0.255", _ENERGY),
"reactive_energy_plus": ("1.0.3.8.0.255", _ENERGY),
"reactive_energy_minus": ("1.0.4.8.0.255", _ENERGY),
"power_factor": ("1.0.13.7.0.255", _POWER_FACTOR),
}
@@ -33,9 +44,10 @@ CONFIG_SCHEMA = sensor.sensor_schema().extend(
**{
cv.Optional(key): cv.invalid(
f"The predefined '{key}' key was removed in ESPHome 2026.11.0. "
f"Use 'obis_code: \"{obis}\"' and set the unit and classes yourself"
f"Add a separate '- platform: dlms_meter' sensor with "
f'obis_code: "{obis}" and {settings}'
)
for key, obis in REMOVED_KEYS.items()
for key, (obis, settings) in REMOVED_KEYS.items()
},
}
)
@@ -22,7 +22,8 @@ CONFIG_SCHEMA = text_sensor.text_sensor_schema().extend(
**{
cv.Optional(key): cv.invalid(
f"The predefined '{key}' key was removed in ESPHome 2026.11.0. "
f"Use 'obis_code: \"{obis}\"' instead"
f"Add a separate '- platform: dlms_meter' text sensor with "
f'obis_code: "{obis}" instead'
)
for key, obis in REMOVED_KEYS.items()
},
@@ -0,0 +1,33 @@
"""Tests for the dlms_meter predefined keys removed in 2026.11.0."""
from collections.abc import Callable
import pytest
from voluptuous import Invalid, MultipleInvalid
from esphome.components.dlms_meter.sensor import CONFIG_SCHEMA as SENSOR_SCHEMA
from esphome.components.dlms_meter.text_sensor import (
CONFIG_SCHEMA as TEXT_SENSOR_SCHEMA,
)
from esphome.types import ConfigType
def _errors(schema: Callable[[ConfigType], ConfigType], config: ConfigType) -> str:
with pytest.raises(Invalid) as exc_info:
schema(config)
errors = (
exc_info.value.errors
if isinstance(exc_info.value, MultipleInvalid)
else [exc_info.value]
)
return "\n".join(str(error) for error in errors)
def test_removed_sensor_key_names_its_replacement() -> None:
errors = _errors(SENSOR_SCHEMA, {"voltage_l1": {"name": "Voltage"}})
assert 'obis_code: "1.0.32.7.0.255" and unit_of_measurement: V' in errors
def test_removed_text_sensor_key_names_its_replacement() -> None:
errors = _errors(TEXT_SENSOR_SCHEMA, {"timestamp": {"name": "Timestamp"}})
assert 'obis_code: "0.0.1.0.0.255"' in errors