From f8fa676ac8e4119558767c810af08369f548dd45 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 30 Dec 2022 19:07:49 +0100 Subject: [PATCH] Do not validate device classes when entity state is unknown (#84860) --- homeassistant/components/sensor/__init__.py | 1 + tests/components/sensor/test_init.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 2db8a7680c4..7beac83f059 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -968,6 +968,7 @@ class SensorEntity(Entity): # Validate unit of measurement used for sensors with a device class if ( not self._invalid_unit_of_measurement_reported + and value is not None and device_class and (units := DEVICE_CLASS_UNITS.get(device_class)) is not None and native_unit_of_measurement not in units diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 33c1d9e8889..d0027a6a07c 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -1144,7 +1144,7 @@ async def test_device_classes_with_invalid_unit_of_measurement( platform.init(empty=True) platform.ENTITIES["0"] = platform.MockSensor( name="Test", - native_value=None, + native_value="1.0", device_class=device_class, native_unit_of_measurement="INVALID!", )