Skip to content

Commit

Permalink
Reformat attribute serialization
Browse files Browse the repository at this point in the history
Dump the node descriptor

Ensure the output is stable

Ensure the output is stable
  • Loading branch information
puddly committed Oct 31, 2024
1 parent cf1a39e commit f51d1ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,6 @@ class ZhaJsonEncoder(json.JSONEncoder):
def default(self, obj):
"""Convert non-JSON types."""
if isinstance(obj, set):
return list(obj)
return sorted(obj, key=repr)

return super().default(obj)
47 changes: 32 additions & 15 deletions zha/zigbee/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,17 @@
_CHECKIN_GRACE_PERIODS = 2


def get_cluster_attr_data(cluster: Cluster) -> dict:
def get_cluster_attr_data(cluster: Cluster) -> list[dict]:
"""Return cluster attribute data."""
return {
"attributes": {
f"0x{attr_id:04x}": {
"attribute": repr(attr_def),
"value": cluster.get(attr_def.name),
}
for attr_id, attr_def in cluster.attributes.items()
},
"unsupported_attributes": sorted(
cluster.unsupported_attributes, key=lambda v: (isinstance(v, str), v)
),
}
return [
{
"id": f"0x{attr_def.id:04x}",
"name": attr_def.name,
"value": cluster.get(attr_def.name),
"unsupported": (attr_def.id in cluster.unsupported_attributes),
}
for attr_def in cluster.attributes.values()
]


def get_device_automation_triggers(
Expand Down Expand Up @@ -1127,6 +1124,25 @@ def get_diagnostics_json(self):
info["device_type"] = self.device_type
info["active_coordinator"] = self.is_active_coordinator

node_desc = self.device.node_desc
info["node_descriptor"] = {
"logical_type": node_desc.logical_type.name,
"complex_descriptor_available": bool(
node_desc.complex_descriptor_available
),
"user_descriptor_available": bool(node_desc.user_descriptor_available),
"reserved": node_desc.reserved,
"aps_flags": node_desc.aps_flags,
"frequency_band": node_desc.frequency_band,
"mac_capability_flags": node_desc.mac_capability_flags,
"manufacturer_code": node_desc.manufacturer_code,
"maximum_buffer_size": node_desc.maximum_buffer_size,
"maximum_incoming_transfer_size": node_desc.maximum_incoming_transfer_size,
"server_mask": node_desc.server_mask,
"maximum_outgoing_transfer_size": node_desc.maximum_outgoing_transfer_size,
"descriptor_capability_field": node_desc.descriptor_capability_field,
}

topology = self.gateway.application_controller.topology
info["neighbors"] = [
{
Expand Down Expand Up @@ -1177,15 +1193,15 @@ def get_diagnostics_json(self):
{
"cluster_id": f"0x{cluster_id:04x}",
"endpoint_attribute": cluster.ep_attribute,
**get_cluster_attr_data(cluster),
"attributes": get_cluster_attr_data(cluster),
}
for cluster_id, cluster in endpoint.in_clusters.items()
],
"out_clusters": [
{
"cluster_id": f"0x{cluster_id:04x}",
"endpoint_attribute": cluster.ep_attribute,
**get_cluster_attr_data(cluster),
"attributes": get_cluster_attr_data(cluster),
}
for cluster_id, cluster in endpoint.out_clusters.items()
],
Expand Down Expand Up @@ -1231,6 +1247,7 @@ def get_diagnostics_json(self):

for (platform, _unique_id), platform_entity in self.platform_entities.items():
info_object = dataclasses.asdict(platform_entity.info_object)
info_object["cluster_handlers"].sort(key=lambda i: i["unique_id"])

for cluster_handler_info in info_object["cluster_handlers"]:
cluster_info = cluster_handler_info["cluster"]
Expand Down

0 comments on commit f51d1ab

Please sign in to comment.