From 1b97a51b5ecba27fcffea677f3e92f46eabc2d69 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Wed, 25 Jan 2023 20:45:50 +0100 Subject: [PATCH] Print expected device class units in error log (#86125) --- homeassistant/components/sensor/__init__.py | 2 ++ tests/components/sensor/test_init.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 6dd745861e0..351976db162 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -665,6 +665,7 @@ class SensorEntity(Entity): ( "Entity %s (%s) is using native unit of measurement '%s' which " "is not a valid unit for the device class ('%s') it is using; " + "expected one of %s; " "Please update your configuration if your entity is manually " "configured, otherwise %s" ), @@ -672,6 +673,7 @@ class SensorEntity(Entity): type(self), native_unit_of_measurement, device_class, + [str(unit) if unit else "no unit of measurement" for unit in units], report_issue, ) diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index cd62228f83a..e65ae8ae7b8 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -9,7 +9,11 @@ import pytest from pytest import approx from homeassistant.components.number import NumberDeviceClass -from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass +from homeassistant.components.sensor import ( + DEVICE_CLASS_UNITS, + SensorDeviceClass, + SensorStateClass, +) from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, @@ -1403,13 +1407,17 @@ async def test_device_classes_with_invalid_unit_of_measurement( device_class=device_class, native_unit_of_measurement="INVALID!", ) - + units = [ + str(unit) if unit else "no unit of measurement" + for unit in DEVICE_CLASS_UNITS.get(device_class, set()) + ] assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) await hass.async_block_till_done() assert ( "is using native unit of measurement 'INVALID!' which is not a valid " - f"unit for the device class ('{device_class}') it is using" + f"unit for the device class ('{device_class}') it is using; " + f"expected one of {units}" ) in caplog.text