Skip to content

Commit cf9cf72

Browse files
AboveColinclaude
andcommitted
Set unit_class on the imported statistics metadata
Home Assistant 2026.9 logs a frame warning when async_import_statistics is called without unit_class, and the recorder stops accepting metadata without the key in 2026.11. Derive it from STATISTIC_UNIT_TO_UNIT_CONVERTER the way the recorder derives it, and skip the key on cores that predate it, so the 2024.11 floor in hacs.json still holds. Measured on the ha-dev instance (Home Assistant 2026.9, podman container ha-dev on nix1): before the change the metadata was accepted and the warning fired; after it the metadata carries unit_class 'mass' for a kg sensor, no warning appears, and the two hourly rows land in statistics_meta/statistics with min 80.0, max 81.0, mean 80.5 at 08:00 UTC and 79.5/79.5/79.5 at 09:00. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 573b693 commit cf9cf72

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

custom_components/fitdays/statistics.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
from homeassistant.components.recorder import DOMAIN as RECORDER_DOMAIN
2121
from homeassistant.components.recorder.models import StatisticData, StatisticMetaData
2222
from homeassistant.components.recorder.statistics import async_import_statistics
23-
24-
try: # Home Assistant 2025.11 and later
25-
from homeassistant.components.recorder.models import StatisticMeanType
26-
except ImportError: # pragma: no cover - older cores only carry has_mean
27-
StatisticMeanType = None # type: ignore[assignment]
2823
from homeassistant.components.sensor import SensorStateClass
2924
from homeassistant.config_entries import ConfigEntry
3025
from homeassistant.core import HomeAssistant
@@ -33,6 +28,25 @@
3328

3429
from .const import DOMAIN
3530

31+
try: # Home Assistant 2025.11 and later
32+
from homeassistant.components.recorder.models import StatisticMeanType
33+
except ImportError: # pragma: no cover - older cores only carry has_mean
34+
StatisticMeanType = None # type: ignore[assignment]
35+
36+
try: # ``unit_class`` arrived after the 2024.11 floor supported here
37+
from homeassistant.components.recorder.statistics import (
38+
STATISTIC_UNIT_TO_UNIT_CONVERTER,
39+
)
40+
except ImportError: # pragma: no cover - older cores have no unit classes
41+
STATISTIC_UNIT_TO_UNIT_CONVERTER = None # type: ignore[assignment]
42+
43+
# The recorder fills unit_class in itself today and warns about it; it starts
44+
# rejecting metadata without the key in 2026.11.
45+
_HAS_UNIT_CLASS = (
46+
STATISTIC_UNIT_TO_UNIT_CONVERTER is not None
47+
and "unit_class" in StatisticMetaData.__annotations__
48+
)
49+
3650
_LOGGER = logging.getLogger(__name__)
3751

3852

@@ -66,7 +80,9 @@ def _metadata(entity_id: str, unit: str | None) -> StatisticMetaData:
6680
Describe one sensor's statistics series.
6781
6882
``has_mean`` is the pre-2025.11 spelling and the recorder drops it in
69-
2026.4, so ``mean_type`` is used wherever the enum exists.
83+
2026.4, so ``mean_type`` is used wherever the enum exists. ``unit_class``
84+
is derived the same way the recorder derives it, which keeps the metadata
85+
valid once the recorder stops filling it in for us in 2026.11.
7086
"""
7187
metadata: dict[str, Any] = {
7288
"has_sum": False,
@@ -79,6 +95,9 @@ def _metadata(entity_id: str, unit: str | None) -> StatisticMetaData:
7995
metadata["mean_type"] = StatisticMeanType.ARITHMETIC
8096
else:
8197
metadata["has_mean"] = True
98+
if _HAS_UNIT_CLASS:
99+
converter = STATISTIC_UNIT_TO_UNIT_CONVERTER.get(unit)
100+
metadata["unit_class"] = converter.UNIT_CLASS if converter else None
82101
return metadata # type: ignore[return-value]
83102

84103

0 commit comments

Comments
 (0)