diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 35ffc1c3d2a..5108a167552 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -503,12 +503,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, ) diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index c8402a28ffe..2167e1a1ba5 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -501,7 +501,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}, diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 89501cf37df..b43c63f015c 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -10,6 +10,7 @@ from pytest import approx from homeassistant.components.number import NumberDeviceClass from homeassistant.components.sensor import ( + DEVICE_CLASS_STATE_CLASSES, DEVICE_CLASS_UNITS, SensorDeviceClass, SensorStateClass, @@ -1637,7 +1638,12 @@ 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