Account for changed python3.11 enum.IntFlag 0 behavior in zha (#88144)

This commit is contained in:
J. Nick Koston 2023-02-15 11:40:48 -06:00 committed by GitHub
parent 80ee196fd8
commit 4221433ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -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

View File

@ -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"] == "<bitmap8.32: 32>"
assert hass.states.get(entity_id).attributes["status"] in ("<bitmap8.32: 32>", "32")
async def async_test_smart_energy_summation(hass, cluster, entity_id):