Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for Shelly."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.bluetooth import async_scanner_by_source
8 from homeassistant.components.diagnostics import async_redact_data
9 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import format_mac
12 
13 from .coordinator import ShellyConfigEntry
14 from .utils import get_rpc_ws_url
15 
16 TO_REDACT = {CONF_USERNAME, CONF_PASSWORD}
17 
18 
20  hass: HomeAssistant, entry: ShellyConfigEntry
21 ) -> dict[str, Any]:
22  """Return diagnostics for a config entry."""
23  shelly_entry_data = entry.runtime_data
24 
25  device_settings: str | dict = "not initialized"
26  device_status: str | dict = "not initialized"
27  bluetooth: str | dict = "not initialized"
28  last_error: str = "not initialized"
29 
30  if shelly_entry_data.block:
31  block_coordinator = shelly_entry_data.block
32  assert block_coordinator
33  device_info = {
34  "name": block_coordinator.name,
35  "model": block_coordinator.model,
36  "sw_version": block_coordinator.sw_version,
37  }
38  if block_coordinator.device.initialized:
39  device_settings = {
40  k: v
41  for k, v in block_coordinator.device.settings.items()
42  if k in ["cloud", "coiot"]
43  }
44  device_status = {
45  k: v
46  for k, v in block_coordinator.device.status.items()
47  if k
48  in [
49  "update",
50  "wifi_sta",
51  "time",
52  "has_update",
53  "ram_total",
54  "ram_free",
55  "ram_lwm",
56  "fs_size",
57  "fs_free",
58  "uptime",
59  ]
60  }
61 
62  if block_coordinator.device.last_error:
63  last_error = repr(block_coordinator.device.last_error)
64 
65  else:
66  rpc_coordinator = shelly_entry_data.rpc
67  assert rpc_coordinator
68  device_info = {
69  "name": rpc_coordinator.name,
70  "model": rpc_coordinator.model,
71  "sw_version": rpc_coordinator.sw_version,
72  }
73  if rpc_coordinator.device.initialized:
74  device_settings = {
75  k: v for k, v in rpc_coordinator.device.config.items() if k in ["cloud"]
76  }
77  ws_config = rpc_coordinator.device.config["ws"]
78  device_settings["ws_outbound_enabled"] = ws_config["enable"]
79  if ws_config["enable"]:
80  device_settings["ws_outbound_server_valid"] = bool(
81  ws_config["server"] == get_rpc_ws_url(hass)
82  )
83  device_status = {
84  k: v
85  for k, v in rpc_coordinator.device.status.items()
86  if k in ["sys", "wifi"]
87  }
88 
89  source = format_mac(rpc_coordinator.mac).upper()
90  if scanner := async_scanner_by_source(hass, source):
91  bluetooth = {
92  "scanner": await scanner.async_diagnostics(),
93  }
94 
95  if rpc_coordinator.device.last_error:
96  last_error = repr(rpc_coordinator.device.last_error)
97 
98  if isinstance(device_status, dict):
99  device_status = async_redact_data(device_status, ["ssid"])
100 
101  return {
102  "entry": async_redact_data(entry.as_dict(), TO_REDACT),
103  "device_info": device_info,
104  "device_settings": device_settings,
105  "device_status": device_status,
106  "last_error": last_error,
107  "bluetooth": bluetooth,
108  }
BaseHaScanner|None async_scanner_by_source(HomeAssistant hass, str source)
Definition: api.py:51
dict async_redact_data(Mapping data, Iterable[Any] to_redact)
Definition: util.py:14
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, ShellyConfigEntry entry)
Definition: diagnostics.py:21
str|None get_rpc_ws_url(HomeAssistant hass)
Definition: utils.py:585