Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for Powerview Hunter Douglas."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import asdict
6 from typing import Any
7 
8 import attr
9 
10 from homeassistant.components.diagnostics import async_redact_data
11 from homeassistant.const import ATTR_CONFIGURATION_URL, CONF_HOST
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers import device_registry as dr, entity_registry as er
14 from homeassistant.helpers.device_registry import DeviceEntry
15 
16 from .const import REDACT_HUB_ADDRESS, REDACT_MAC_ADDRESS, REDACT_SERIAL_NUMBER
17 from .model import PowerviewConfigEntry
18 
19 REDACT_CONFIG = {
20  CONF_HOST,
21  REDACT_HUB_ADDRESS,
22  REDACT_MAC_ADDRESS,
23  REDACT_SERIAL_NUMBER,
24  ATTR_CONFIGURATION_URL,
25 }
26 
27 
29  hass: HomeAssistant, entry: PowerviewConfigEntry
30 ) -> dict[str, Any]:
31  """Return diagnostics for a config entry."""
32  data = _async_get_diagnostics(hass, entry)
33  device_registry = dr.async_get(hass)
34  data.update(
35  device_info=[
36  _async_device_as_dict(hass, device)
37  for device in dr.async_entries_for_config_entry(
38  device_registry, entry.entry_id
39  )
40  ],
41  )
42  return data
43 
44 
46  hass: HomeAssistant, entry: PowerviewConfigEntry, device: DeviceEntry
47 ) -> dict[str, Any]:
48  """Return diagnostics for a device entry."""
49  data = _async_get_diagnostics(hass, entry)
50  data["device_info"] = _async_device_as_dict(hass, device)
51  # try to match on name to restrict to shade if we can
52  # otherwise just return all shade data
53  # shade name is unique in powerview
54  shade_data = data["shade_data"]
55  for shade in shade_data:
56  if shade_data[shade]["name_unicode"] == device.name:
57  data["shade_data"] = shade_data[shade]
58  return data
59 
60 
61 @callback
63  hass: HomeAssistant,
64  entry: PowerviewConfigEntry,
65 ) -> dict[str, Any]:
66  """Return diagnostics for a config entry."""
67  pv_entry = entry.runtime_data
68  shade_data = pv_entry.coordinator.data.get_all_raw_data()
69  hub_info = async_redact_data(asdict(pv_entry.device_info), REDACT_CONFIG)
70  return {"hub_info": hub_info, "shade_data": shade_data}
71 
72 
73 @callback
74 def _async_device_as_dict(hass: HomeAssistant, device: DeviceEntry) -> dict[str, Any]:
75  """Represent a Powerview device as a dictionary."""
76 
77  # Gather information how this device is represented in Home Assistant
78  entity_registry = er.async_get(hass)
79 
80  data = async_redact_data(attr.asdict(device), REDACT_CONFIG)
81  data["entities"] = []
82  entities: list[dict[str, Any]] = data["entities"]
83 
84  entries = er.async_entries_for_device(
85  entity_registry,
86  device_id=device.id,
87  include_disabled_entities=True,
88  )
89 
90  for entity_entry in entries:
91  state = hass.states.get(entity_entry.entity_id)
92  state_dict = None
93  if state:
94  state_dict = dict(state.as_dict())
95  state_dict.pop("context", None)
96 
97  entity = attr.asdict(entity_entry)
98  entity["state"] = state_dict
99  entities.append(entity)
100 
101  return data
dict async_redact_data(Mapping data, Iterable[Any] to_redact)
Definition: util.py:14
dict[str, Any] async_get_device_diagnostics(HomeAssistant hass, PowerviewConfigEntry entry, DeviceEntry device)
Definition: diagnostics.py:47
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, PowerviewConfigEntry entry)
Definition: diagnostics.py:30
dict[str, Any] _async_get_diagnostics(HomeAssistant hass, PowerviewConfigEntry entry)
Definition: diagnostics.py:65
dict[str, Any] _async_device_as_dict(HomeAssistant hass, DeviceEntry device)
Definition: diagnostics.py:74