diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index 821ea6a9fbe..78ce47c7e57 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -4,6 +4,7 @@ from __future__ import annotations import enum import functools import numbers +import sys from typing import TYPE_CHECKING, Any from typing_extensions import Self @@ -442,7 +443,12 @@ class SmartEnergyMetering(Sensor): if self._channel.device_type is not None: attrs["device_type"] = self._channel.device_type if (status := self._channel.status) is not None: - attrs["status"] = str(status)[len(status.__class__.__name__) + 1 :] + if isinstance(status, enum.IntFlag) and sys.version_info >= (3, 11): + attrs["status"] = str( + status.name if status.name is not None else status.value + ) + else: + attrs["status"] = str(status)[len(status.__class__.__name__) + 1 :] return attrs diff --git a/tests/components/zha/test_sensor.py b/tests/components/zha/test_sensor.py index 1d50639df16..ad990325abf 100644 --- a/tests/components/zha/test_sensor.py +++ b/tests/components/zha/test_sensor.py @@ -139,16 +139,16 @@ async def async_test_metering(hass, cluster, entity_id): await send_attributes_report(hass, cluster, {1024: 12346, "status": 64 + 8}) assert_state(hass, entity_id, "12346.0", None) - assert ( - hass.states.get(entity_id).attributes["status"] - == "SERVICE_DISCONNECT|POWER_FAILURE" + assert hass.states.get(entity_id).attributes["status"] in ( + "SERVICE_DISCONNECT|POWER_FAILURE", + "POWER_FAILURE|SERVICE_DISCONNECT", ) await send_attributes_report( hass, cluster, {"status": 32, "metering_device_type": 1} ) # currently only statuses for electric meters are supported - assert hass.states.get(entity_id).attributes["status"] == "" + assert hass.states.get(entity_id).attributes["status"] in ("", "32") async def async_test_smart_energy_summation(hass, cluster, entity_id):