From bf482eee24dd129da930bb1055f4ee83315b6d0a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 4 Feb 2023 11:49:24 +0100 Subject: [PATCH] Extend state class sensor warnings with expected values (#87294) --- homeassistant/components/sensor/__init__.py | 3 +++ homeassistant/components/sensor/const.py | 2 +- tests/components/sensor/test_init.py | 8 +++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 0222e6f00ed..bed886d4ba0 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -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, ) diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 09bddd0f1e0..0727a156d21 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -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}, diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 1901381ed1c..17a794ac0d3 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -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