2022-04-12 05:35:29 +00:00
|
|
|
"""Provides diagnostics for ZHA."""
|
2024-03-08 15:35:45 +00:00
|
|
|
|
2022-04-12 05:35:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import dataclasses
|
2023-02-27 20:07:57 +00:00
|
|
|
from importlib.metadata import version
|
2022-04-12 05:35:29 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2024-07-08 18:18:30 +00:00
|
|
|
from zha.application.const import (
|
|
|
|
ATTR_ATTRIBUTE_NAME,
|
|
|
|
ATTR_DEVICE_TYPE,
|
|
|
|
ATTR_IEEE,
|
|
|
|
ATTR_IN_CLUSTERS,
|
|
|
|
ATTR_OUT_CLUSTERS,
|
|
|
|
ATTR_PROFILE_ID,
|
|
|
|
ATTR_VALUE,
|
|
|
|
UNKNOWN,
|
|
|
|
)
|
|
|
|
from zha.application.gateway import Gateway
|
|
|
|
from zha.zigbee.device import Device
|
2022-04-12 05:35:29 +00:00
|
|
|
from zigpy.config import CONF_NWK_EXTENDED_PAN_ID
|
2022-06-28 15:01:27 +00:00
|
|
|
from zigpy.profiles import PROFILES
|
2023-03-31 19:37:00 +00:00
|
|
|
from zigpy.types import Channels
|
2022-06-28 15:01:27 +00:00
|
|
|
from zigpy.zcl import Cluster
|
2022-04-12 05:35:29 +00:00
|
|
|
|
|
|
|
from homeassistant.components.diagnostics.util import async_redact_data
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-06-28 15:01:27 +00:00
|
|
|
from homeassistant.const import CONF_ID, CONF_NAME, CONF_UNIQUE_ID
|
2022-04-12 05:35:29 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
|
2024-07-08 18:18:30 +00:00
|
|
|
from .const import CONF_ALARM_MASTER_CODE
|
|
|
|
from .helpers import (
|
|
|
|
ZHADeviceProxy,
|
|
|
|
async_get_zha_device_proxy,
|
|
|
|
get_zha_data,
|
|
|
|
get_zha_gateway,
|
2022-06-28 15:01:27 +00:00
|
|
|
)
|
2022-04-12 05:35:29 +00:00
|
|
|
|
|
|
|
KEYS_TO_REDACT = {
|
|
|
|
ATTR_IEEE,
|
|
|
|
CONF_UNIQUE_ID,
|
2022-06-28 15:01:27 +00:00
|
|
|
CONF_ALARM_MASTER_CODE,
|
2022-04-12 05:35:29 +00:00
|
|
|
"network_key",
|
|
|
|
CONF_NWK_EXTENDED_PAN_ID,
|
2022-04-13 21:02:57 +00:00
|
|
|
"partner_ieee",
|
2022-04-12 05:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 15:01:27 +00:00
|
|
|
ATTRIBUTES = "attributes"
|
|
|
|
CLUSTER_DETAILS = "cluster_details"
|
|
|
|
UNSUPPORTED_ATTRIBUTES = "unsupported_attributes"
|
|
|
|
|
2022-04-12 05:35:29 +00:00
|
|
|
|
|
|
|
def shallow_asdict(obj: Any) -> dict:
|
|
|
|
"""Return a shallow copy of a dataclass as a dict."""
|
|
|
|
if hasattr(obj, "__dataclass_fields__"):
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
for field in dataclasses.fields(obj):
|
|
|
|
result[field.name] = shallow_asdict(getattr(obj, field.name))
|
|
|
|
|
|
|
|
return result
|
|
|
|
if hasattr(obj, "as_dict"):
|
|
|
|
return obj.as_dict()
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_config_entry_diagnostics(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
2023-01-09 14:17:48 +00:00
|
|
|
) -> dict[str, Any]:
|
2022-04-12 05:35:29 +00:00
|
|
|
"""Return diagnostics for a config entry."""
|
2023-09-11 19:39:33 +00:00
|
|
|
zha_data = get_zha_data(hass)
|
2024-07-08 18:18:30 +00:00
|
|
|
gateway: Gateway = get_zha_gateway(hass)
|
2024-02-27 11:44:26 +00:00
|
|
|
app = gateway.application_controller
|
2023-03-31 19:37:00 +00:00
|
|
|
|
2023-09-11 19:39:33 +00:00
|
|
|
energy_scan = await app.energy_scan(
|
2023-03-31 19:37:00 +00:00
|
|
|
channels=Channels.ALL_CHANNELS, duration_exp=4, count=1
|
|
|
|
)
|
|
|
|
|
2022-04-12 05:35:29 +00:00
|
|
|
return async_redact_data(
|
|
|
|
{
|
2023-09-11 19:39:33 +00:00
|
|
|
"config": zha_data.yaml_config,
|
2022-04-12 05:35:29 +00:00
|
|
|
"config_entry": config_entry.as_dict(),
|
2023-09-11 19:39:33 +00:00
|
|
|
"application_state": shallow_asdict(app.state),
|
2023-03-31 19:37:00 +00:00
|
|
|
"energy_scan": {
|
|
|
|
channel: 100 * energy / 255 for channel, energy in energy_scan.items()
|
|
|
|
},
|
2022-04-12 05:35:29 +00:00
|
|
|
"versions": {
|
2023-02-27 20:07:57 +00:00
|
|
|
"bellows": version("bellows"),
|
|
|
|
"zigpy": version("zigpy"),
|
|
|
|
"zigpy_deconz": version("zigpy-deconz"),
|
|
|
|
"zigpy_xbee": version("zigpy-xbee"),
|
|
|
|
"zigpy_znp": version("zigpy_znp"),
|
|
|
|
"zigpy_zigate": version("zigpy-zigate"),
|
|
|
|
"zhaquirks": version("zha-quirks"),
|
2024-07-08 18:18:30 +00:00
|
|
|
"zha": version("zha"),
|
2022-04-12 05:35:29 +00:00
|
|
|
},
|
2024-02-27 11:44:26 +00:00
|
|
|
"devices": [
|
|
|
|
{
|
|
|
|
"manufacturer": device.manufacturer,
|
|
|
|
"model": device.model,
|
|
|
|
"logical_type": device.device_type,
|
|
|
|
}
|
|
|
|
for device in gateway.devices.values()
|
|
|
|
],
|
2022-04-12 05:35:29 +00:00
|
|
|
},
|
|
|
|
KEYS_TO_REDACT,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_device_diagnostics(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, device: dr.DeviceEntry
|
2023-01-09 14:17:48 +00:00
|
|
|
) -> dict[str, Any]:
|
2022-04-12 05:35:29 +00:00
|
|
|
"""Return diagnostics for a device."""
|
2024-07-08 18:18:30 +00:00
|
|
|
zha_device_proxy: ZHADeviceProxy = async_get_zha_device_proxy(hass, device.id)
|
|
|
|
device_info: dict[str, Any] = zha_device_proxy.zha_device_info
|
|
|
|
device_info[CLUSTER_DETAILS] = get_endpoint_cluster_attr_data(
|
|
|
|
zha_device_proxy.device
|
|
|
|
)
|
2022-06-28 15:01:27 +00:00
|
|
|
return async_redact_data(device_info, KEYS_TO_REDACT)
|
|
|
|
|
|
|
|
|
2024-07-08 18:18:30 +00:00
|
|
|
def get_endpoint_cluster_attr_data(zha_device: Device) -> dict:
|
2022-06-28 15:01:27 +00:00
|
|
|
"""Return endpoint cluster attribute data."""
|
|
|
|
cluster_details = {}
|
|
|
|
for ep_id, endpoint in zha_device.device.endpoints.items():
|
|
|
|
if ep_id == 0:
|
|
|
|
continue
|
|
|
|
endpoint_key = (
|
|
|
|
f"{PROFILES.get(endpoint.profile_id).DeviceType(endpoint.device_type).name}"
|
|
|
|
if PROFILES.get(endpoint.profile_id) is not None
|
|
|
|
and endpoint.device_type is not None
|
|
|
|
else UNKNOWN
|
|
|
|
)
|
|
|
|
cluster_details[ep_id] = {
|
|
|
|
ATTR_DEVICE_TYPE: {
|
|
|
|
CONF_NAME: endpoint_key,
|
|
|
|
CONF_ID: endpoint.device_type,
|
|
|
|
},
|
|
|
|
ATTR_PROFILE_ID: endpoint.profile_id,
|
|
|
|
ATTR_IN_CLUSTERS: {
|
|
|
|
f"0x{cluster_id:04x}": {
|
|
|
|
"endpoint_attribute": cluster.ep_attribute,
|
|
|
|
**get_cluster_attr_data(cluster),
|
|
|
|
}
|
|
|
|
for cluster_id, cluster in endpoint.in_clusters.items()
|
|
|
|
},
|
|
|
|
ATTR_OUT_CLUSTERS: {
|
|
|
|
f"0x{cluster_id:04x}": {
|
|
|
|
"endpoint_attribute": cluster.ep_attribute,
|
|
|
|
**get_cluster_attr_data(cluster),
|
|
|
|
}
|
|
|
|
for cluster_id, cluster in endpoint.out_clusters.items()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cluster_details
|
|
|
|
|
|
|
|
|
|
|
|
def get_cluster_attr_data(cluster: Cluster) -> dict:
|
|
|
|
"""Return cluster attribute data."""
|
2023-10-06 16:23:48 +00:00
|
|
|
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] = {}
|
|
|
|
|
2022-06-28 15:01:27 +00:00
|
|
|
return {
|
|
|
|
ATTRIBUTES: {
|
|
|
|
f"0x{attr_id:04x}": {
|
|
|
|
ATTR_ATTRIBUTE_NAME: attr_def.name,
|
|
|
|
ATTR_VALUE: attr_value,
|
|
|
|
}
|
|
|
|
for attr_id, attr_def in cluster.attributes.items()
|
|
|
|
if (attr_value := cluster.get(attr_def.name)) is not None
|
|
|
|
},
|
2023-10-06 16:23:48 +00:00
|
|
|
UNSUPPORTED_ATTRIBUTES: unsupported_attributes,
|
2022-06-28 15:01:27 +00:00
|
|
|
}
|