Skip to content

Commit

Permalink
Fix ZHA device diagnostics error for unknown unsupported attributes (h…
Browse files Browse the repository at this point in the history
…ome-assistant#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
  • Loading branch information
TheJulianJES authored Oct 6, 2023
1 parent d654c4b commit 5d0c894
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
20 changes: 14 additions & 6 deletions homeassistant/components/zha/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}": {
Expand All @@ -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,
}
18 changes: 17 additions & 1 deletion tests/components/zha/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d0c894

Please sign in to comment.