Extend state class sensor warnings with expected values (#87294)
This commit is contained in:
parent
95e8717fb4
commit
bf482eee24
3 changed files with 11 additions and 2 deletions
|
@ -508,12 +508,15 @@ class SensorEntity(Entity):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Entity %s (%s) is using state class '%s' which "
|
"Entity %s (%s) is using state class '%s' which "
|
||||||
"is impossible considering device class ('%s') it is using; "
|
"is impossible considering device class ('%s') it is using; "
|
||||||
|
"expected %s%s; "
|
||||||
"Please update your configuration if your entity is manually "
|
"Please update your configuration if your entity is manually "
|
||||||
"configured, otherwise %s",
|
"configured, otherwise %s",
|
||||||
self.entity_id,
|
self.entity_id,
|
||||||
type(self),
|
type(self),
|
||||||
state_class,
|
state_class,
|
||||||
device_class,
|
device_class,
|
||||||
|
"None or one of " if classes else "None",
|
||||||
|
", ".join(f"'{value.value}'" for value in classes),
|
||||||
report_issue,
|
report_issue,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -509,7 +509,7 @@ DEVICE_CLASS_UNITS: dict[SensorDeviceClass, set[type[StrEnum] | str | None]] = {
|
||||||
SensorDeviceClass.WIND_SPEED: set(UnitOfSpeed),
|
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.APPARENT_POWER: {SensorStateClass.MEASUREMENT},
|
||||||
SensorDeviceClass.AQI: {SensorStateClass.MEASUREMENT},
|
SensorDeviceClass.AQI: {SensorStateClass.MEASUREMENT},
|
||||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE: {SensorStateClass.MEASUREMENT},
|
SensorDeviceClass.ATMOSPHERIC_PRESSURE: {SensorStateClass.MEASUREMENT},
|
||||||
|
|
|
@ -9,6 +9,7 @@ import pytest
|
||||||
|
|
||||||
from homeassistant.components.number import NumberDeviceClass
|
from homeassistant.components.number import NumberDeviceClass
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
DEVICE_CLASS_STATE_CLASSES,
|
||||||
DEVICE_CLASS_UNITS,
|
DEVICE_CLASS_UNITS,
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
|
@ -1637,9 +1638,14 @@ async def test_device_classes_with_invalid_state_class(
|
||||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||||
await hass.async_block_till_done()
|
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 (
|
assert (
|
||||||
"is using state class 'INVALID!' which is impossible considering device "
|
"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
|
) in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue