diff --git a/.coveragerc b/.coveragerc index e326f5f44c876d116849810a72871823eaffe53a..283adf0d3fc2302c0a161d4c84a940ff7d7e5e89 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1538,7 +1538,6 @@ omit = homeassistant/components/zha/core/registries.py homeassistant/components/zha/entity.py homeassistant/components/zha/light.py - homeassistant/components/zha/sensor.py homeassistant/components/zhong_hong/climate.py homeassistant/components/ziggo_mediabox_xl/media_player.py homeassistant/components/zoneminder/* diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index f42e88041efea1178cd2457d81b69f58c432f047..e0f5bb958cd744df349254a07cad1976b2f05257 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -183,7 +183,7 @@ class Sensor(ZhaEntity, SensorEntity): """Handle state update from channel.""" self.async_write_ha_state() - def formatter(self, value: int) -> int | float: + def formatter(self, value: int) -> int | float | None: """Numeric pass-through formatter.""" if self._decimals > 0: return round( @@ -236,11 +236,11 @@ class Battery(Sensor): return cls(unique_id, zha_device, channels, **kwargs) @staticmethod - def formatter(value: int) -> int: # pylint: disable=arguments-differ + def formatter(value: int) -> int | None: # pylint: disable=arguments-differ """Return the state of the entity.""" # per zcl specs battery percent is reported at 200% ¯\_(ツ)_/¯ if not isinstance(value, numbers.Number) or value == -1: - return value + return None value = round(value / 2) return value diff --git a/tests/components/zha/test_sensor.py b/tests/components/zha/test_sensor.py index d2fc7c3ca73582a166c1d90be415fd4b23dd1b16..0698c07db9e5ed8f5881c49cbd96ba4e13bd9d28 100644 --- a/tests/components/zha/test_sensor.py +++ b/tests/components/zha/test_sensor.py @@ -255,6 +255,17 @@ async def async_test_powerconfiguration(hass, cluster, entity_id): assert hass.states.get(entity_id).attributes["battery_voltage"] == 2.0 +async def async_test_powerconfiguration2(hass, cluster, entity_id): + """Test powerconfiguration/battery sensor.""" + await send_attributes_report(hass, cluster, {33: -1}) + assert_state(hass, entity_id, STATE_UNKNOWN, "%") + assert hass.states.get(entity_id).attributes["battery_voltage"] == 2.9 + assert hass.states.get(entity_id).attributes["battery_quantity"] == 3 + assert hass.states.get(entity_id).attributes["battery_size"] == "AAA" + await send_attributes_report(hass, cluster, {32: 20}) + assert hass.states.get(entity_id).attributes["battery_voltage"] == 2.0 + + async def async_test_device_temperature(hass, cluster, entity_id): """Test temperature sensor.""" await send_attributes_report(hass, cluster, {0: 2900}) @@ -370,6 +381,18 @@ async def async_test_device_temperature(hass, cluster, entity_id): }, None, ), + ( + general.PowerConfiguration.cluster_id, + "battery", + async_test_powerconfiguration2, + 2, + { + "battery_size": 4, # AAA + "battery_voltage": 29, + "battery_quantity": 3, + }, + None, + ), ( general.DeviceTemperature.cluster_id, "device_temperature",