109 lines
3.2 KiB
Python
109 lines
3.2 KiB
Python
|
"""Diagnostics support for Powerview Hunter Douglas."""
|
||
|
from __future__ import annotations
|
||
|
|
||
|
from typing import Any
|
||
|
|
||
|
import attr
|
||
|
|
||
|
from homeassistant.components.diagnostics import async_redact_data
|
||
|
from homeassistant.config_entries import ConfigEntry
|
||
|
from homeassistant.const import CONF_HOST
|
||
|
from homeassistant.core import HomeAssistant, callback
|
||
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||
|
from homeassistant.helpers.device_registry import DeviceEntry
|
||
|
|
||
|
from .const import (
|
||
|
COORDINATOR,
|
||
|
DEVICE_INFO,
|
||
|
DEVICE_MAC_ADDRESS,
|
||
|
DEVICE_SERIAL_NUMBER,
|
||
|
DOMAIN,
|
||
|
PV_HUB_ADDRESS,
|
||
|
)
|
||
|
from .coordinator import PowerviewShadeUpdateCoordinator
|
||
|
|
||
|
REDACT_CONFIG = {
|
||
|
CONF_HOST,
|
||
|
DEVICE_MAC_ADDRESS,
|
||
|
DEVICE_SERIAL_NUMBER,
|
||
|
PV_HUB_ADDRESS,
|
||
|
"configuration_url",
|
||
|
}
|
||
|
|
||
|
|
||
|
async def async_get_config_entry_diagnostics(
|
||
|
hass: HomeAssistant, entry: ConfigEntry
|
||
|
) -> dict[str, Any]:
|
||
|
"""Return diagnostics for a config entry."""
|
||
|
data = _async_get_diagnostics(hass, entry)
|
||
|
device_registry = dr.async_get(hass)
|
||
|
data.update(
|
||
|
device_info=[
|
||
|
_async_device_as_dict(hass, device)
|
||
|
for device in dr.async_entries_for_config_entry(
|
||
|
device_registry, entry.entry_id
|
||
|
)
|
||
|
],
|
||
|
)
|
||
|
return data
|
||
|
|
||
|
|
||
|
async def async_get_device_diagnostics(
|
||
|
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
|
||
|
) -> dict[str, Any]:
|
||
|
"""Return diagnostics for a device entry."""
|
||
|
data = _async_get_diagnostics(hass, entry)
|
||
|
data["device_info"] = _async_device_as_dict(hass, device)
|
||
|
# try to match on name to restrict to shade if we can
|
||
|
# otherwise just return all shade data
|
||
|
# shade name is unique in powerview
|
||
|
shade_data = data["shade_data"]
|
||
|
for shade in shade_data:
|
||
|
if shade_data[shade]["name_unicode"] == device.name:
|
||
|
data["shade_data"] = shade_data[shade]
|
||
|
return data
|
||
|
|
||
|
|
||
|
@callback
|
||
|
def _async_get_diagnostics(
|
||
|
hass: HomeAssistant,
|
||
|
entry: ConfigEntry,
|
||
|
) -> dict[str, Any]:
|
||
|
"""Return diagnostics for a config entry."""
|
||
|
pv_data = hass.data[DOMAIN][entry.entry_id]
|
||
|
coordinator: PowerviewShadeUpdateCoordinator = pv_data[COORDINATOR]
|
||
|
shade_data = coordinator.data.get_all_raw_data()
|
||
|
hub_info = async_redact_data(pv_data[DEVICE_INFO], REDACT_CONFIG)
|
||
|
return {"hub_info": hub_info, "shade_data": shade_data}
|
||
|
|
||
|
|
||
|
@callback
|
||
|
def _async_device_as_dict(hass: HomeAssistant, device: DeviceEntry) -> dict[str, Any]:
|
||
|
"""Represent a Powerview device as a dictionary."""
|
||
|
|
||
|
# Gather information how this device is represented in Home Assistant
|
||
|
entity_registry = er.async_get(hass)
|
||
|
|
||
|
data = async_redact_data(attr.asdict(device), REDACT_CONFIG)
|
||
|
data["entities"] = []
|
||
|
entities: list[dict[str, Any]] = data["entities"]
|
||
|
|
||
|
entries = er.async_entries_for_device(
|
||
|
entity_registry,
|
||
|
device_id=device.id,
|
||
|
include_disabled_entities=True,
|
||
|
)
|
||
|
|
||
|
for entity_entry in entries:
|
||
|
state = hass.states.get(entity_entry.entity_id)
|
||
|
state_dict = None
|
||
|
if state:
|
||
|
state_dict = dict(state.as_dict())
|
||
|
state_dict.pop("context", None)
|
||
|
|
||
|
entity = attr.asdict(entity_entry)
|
||
|
entity["state"] = state_dict
|
||
|
entities.append(entity)
|
||
|
|
||
|
return data
|