From b5bb97c856a07ad02a84d25a33795da8025c4b35 Mon Sep 17 00:00:00 2001
From: Florian Kisser <github@kssr.net>
Date: Sun, 21 Jan 2024 02:37:13 +0100
Subject: [PATCH] Fix zha illuminance measured value mapping (#108547)

---
 homeassistant/components/zha/sensor.py | 6 +++++-
 tests/components/zha/test_sensor.py    | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py
index c87ae9d72fb..9de4bcf75f5 100644
--- a/homeassistant/components/zha/sensor.py
+++ b/homeassistant/components/zha/sensor.py
@@ -483,8 +483,12 @@ class Illuminance(Sensor):
     _attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
     _attr_native_unit_of_measurement = LIGHT_LUX
 
-    def formatter(self, value: int) -> int:
+    def formatter(self, value: int) -> int | None:
         """Convert illumination data."""
+        if value == 0:
+            return 0
+        if value == 0xFFFF:
+            return None
         return round(pow(10, ((value - 1) / 10000)))
 
 
diff --git a/tests/components/zha/test_sensor.py b/tests/components/zha/test_sensor.py
index 4103897a000..7d67e41512a 100644
--- a/tests/components/zha/test_sensor.py
+++ b/tests/components/zha/test_sensor.py
@@ -136,6 +136,12 @@ async def async_test_illuminance(hass, cluster, entity_id):
     await send_attributes_report(hass, cluster, {1: 1, 0: 10, 2: 20})
     assert_state(hass, entity_id, "1", LIGHT_LUX)
 
+    await send_attributes_report(hass, cluster, {1: 0, 0: 0, 2: 20})
+    assert_state(hass, entity_id, "0", LIGHT_LUX)
+
+    await send_attributes_report(hass, cluster, {1: 0, 0: 0xFFFF, 2: 20})
+    assert_state(hass, entity_id, "unknown", LIGHT_LUX)
+
 
 async def async_test_metering(hass, cluster, entity_id):
     """Test Smart Energy metering sensor."""
-- 
GitLab