Fix ZHA device diagnostics error for unknown unsupported attributes (#101239)

* Modify test to account for scenario of unknown unsupported attributes

* Add error checking for finding unsupported attributes

* Change comment to clarify zigpy misses an attribute def

This should make it more clear that it's about an unknown attribute (where zigpy doesn't have an attribute definition).

* Increase test coverage

This increases test coverage by doing the following:
- adding the `IasZone` to our test device, so we have a cluster which actually has some attribute definitions
- adding not just an unknown unsupported attribute by id, but also by name
- adding a known unsupported attribute by id and by name

* Fix diagnostics logic
This commit is contained in:
TheJulianJES 2023-10-06 18:23:48 +02:00 committed by Franck Nijhof
parent 01daae69ab
commit 7dfb397aef
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 31 additions and 7 deletions

View file

@ -139,6 +139,19 @@ def get_endpoint_cluster_attr_data(zha_device: ZHADevice) -> dict:
def get_cluster_attr_data(cluster: Cluster) -> dict:
"""Return cluster attribute data."""
unsupported_attributes = {}
for u_attr in cluster.unsupported_attributes:
try:
u_attr_def = cluster.find_attribute(u_attr)
unsupported_attributes[f"0x{u_attr_def.id:04x}"] = {
ATTR_ATTRIBUTE_NAME: u_attr_def.name
}
except KeyError:
if isinstance(u_attr, int):
unsupported_attributes[f"0x{u_attr:04x}"] = {}
else:
unsupported_attributes[u_attr] = {}
return {
ATTRIBUTES: {
f"0x{attr_id:04x}": {
@ -148,10 +161,5 @@ def get_cluster_attr_data(cluster: Cluster) -> dict:
for attr_id, attr_def in cluster.attributes.items()
if (attr_value := cluster.get(attr_def.name)) is not None
},
UNSUPPORTED_ATTRIBUTES: {
f"0x{cluster.find_attribute(u_attr).id:04x}": {
ATTR_ATTRIBUTE_NAME: cluster.find_attribute(u_attr).name
}
for u_attr in cluster.unsupported_attributes
},
UNSUPPORTED_ATTRIBUTES: unsupported_attributes,
}

View file

@ -44,7 +44,7 @@ def zigpy_device(zigpy_device_mock):
"""Device tracker zigpy device."""
endpoints = {
1: {
SIG_EP_INPUT: [security.IasAce.cluster_id],
SIG_EP_INPUT: [security.IasAce.cluster_id, security.IasZone.cluster_id],
SIG_EP_OUTPUT: [],
SIG_EP_TYPE: zha.DeviceType.IAS_ANCILLARY_CONTROL,
SIG_EP_PROFILE: zha.PROFILE_ID,
@ -93,6 +93,22 @@ async def test_diagnostics_for_device(
) -> None:
"""Test diagnostics for device."""
zha_device: ZHADevice = await zha_device_joined(zigpy_device)
# add unknown unsupported attribute with id and name
zha_device.device.endpoints[1].in_clusters[
security.IasAce.cluster_id
].unsupported_attributes.update({0x1000, "unknown_attribute_name"})
# add known unsupported attributes with id and name
zha_device.device.endpoints[1].in_clusters[
security.IasZone.cluster_id
].unsupported_attributes.update(
{
security.IasZone.AttributeDefs.num_zone_sensitivity_levels_supported.id,
security.IasZone.AttributeDefs.current_zone_sensitivity_level.name,
}
)
dev_reg = async_get(hass)
device = dev_reg.async_get_device(identifiers={("zha", str(zha_device.ieee))})
assert device