Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for ESPHome."""
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
10 from homeassistant.core import HomeAssistant
11 
12 from . import CONF_NOISE_PSK
13 from .dashboard import async_get_dashboard
14 from .entry_data import ESPHomeConfigEntry
15 
16 CONF_MAC_ADDRESS = "mac_address"
17 
18 REDACT_KEYS = {CONF_NOISE_PSK, CONF_PASSWORD, CONF_MAC_ADDRESS}
19 
20 
22  hass: HomeAssistant, config_entry: ESPHomeConfigEntry
23 ) -> dict[str, Any]:
24  """Return diagnostics for a config entry."""
25  diag: dict[str, Any] = {}
26 
27  diag["config"] = config_entry.as_dict()
28 
29  entry_data = config_entry.runtime_data
30 
31  if (storage_data := await entry_data.store.async_load()) is not None:
32  diag["storage_data"] = storage_data
33 
34  if (
35  config_entry.unique_id
36  and (scanner := async_scanner_by_source(hass, config_entry.unique_id.upper()))
37  and (bluetooth_device := entry_data.bluetooth_device)
38  ):
39  diag["bluetooth"] = {
40  "connections_free": bluetooth_device.ble_connections_free,
41  "connections_limit": bluetooth_device.ble_connections_limit,
42  "available": bluetooth_device.available,
43  "scanner": await scanner.async_diagnostics(),
44  }
45 
46  if dashboard := async_get_dashboard(hass):
47  diag["dashboard"] = dashboard.addon_slug
48 
49  return async_redact_data(diag, REDACT_KEYS)
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
ESPHomeDashboardCoordinator|None async_get_dashboard(HomeAssistant hass)
Definition: dashboard.py:134
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, ESPHomeConfigEntry config_entry)
Definition: diagnostics.py:23