Extend state class sensor warnings with expected values (#87294)

This commit is contained in:
Franck Nijhof 2023-02-04 11:49:24 +01:00 committed by GitHub
parent 95e8717fb4
commit bf482eee24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View file

@ -508,12 +508,15 @@ class SensorEntity(Entity):
_LOGGER.warning(
"Entity %s (%s) is using state class '%s' which "
"is impossible considering device class ('%s') it is using; "
"expected %s%s; "
"Please update your configuration if your entity is manually "
"configured, otherwise %s",
self.entity_id,
type(self),
state_class,
device_class,
"None or one of " if classes else "None",
", ".join(f"'{value.value}'" for value in classes),
report_issue,
)

View file

@ -509,7 +509,7 @@ DEVICE_CLASS_UNITS: dict[SensorDeviceClass, set[type[StrEnum] | str | None]] = {
SensorDeviceClass.WIND_SPEED: set(UnitOfSpeed),
}
DEVICE_CLASS_STATE_CLASSES: dict[SensorDeviceClass, set[SensorStateClass | None]] = {
DEVICE_CLASS_STATE_CLASSES: dict[SensorDeviceClass, set[SensorStateClass]] = {
SensorDeviceClass.APPARENT_POWER: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.AQI: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.ATMOSPHERIC_PRESSURE: {SensorStateClass.MEASUREMENT},

View file

@ -9,6 +9,7 @@ import pytest
from homeassistant.components.number import NumberDeviceClass
from homeassistant.components.sensor import (
DEVICE_CLASS_STATE_CLASSES,
DEVICE_CLASS_UNITS,
SensorDeviceClass,
SensorStateClass,
@ -1637,9 +1638,14 @@ async def test_device_classes_with_invalid_state_class(
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done()
classes = DEVICE_CLASS_STATE_CLASSES.get(device_class, set())
one_of = ", ".join(f"'{value.value}'" for value in classes)
expected = f"None or one of {one_of}" if classes else "None"
assert (
"is using state class 'INVALID!' which is impossible considering device "
f"class ('{device_class}') it is using"
f"class ('{device_class}') it is using; "
f"expected {expected}"
) in caplog.text