2022-01-30 21:53:46 +00:00
|
|
|
"""Diagnostics support for ESPHome."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2022-01-26 22:35:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-06-26 02:31:31 +00:00
|
|
|
from typing import Any
|
2022-01-26 22:35:41 +00:00
|
|
|
|
2022-11-27 19:59:37 +00:00
|
|
|
from homeassistant.components.bluetooth import async_scanner_by_source
|
2022-01-26 22:35:41 +00:00
|
|
|
from homeassistant.components.diagnostics import async_redact_data
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_PASSWORD
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from . import CONF_NOISE_PSK, DomainData
|
2023-01-18 20:15:37 +00:00
|
|
|
from .dashboard import async_get_dashboard
|
2022-01-26 22:35:41 +00:00
|
|
|
|
|
|
|
CONF_MAC_ADDRESS = "mac_address"
|
|
|
|
|
|
|
|
REDACT_KEYS = {CONF_NOISE_PSK, CONF_PASSWORD, CONF_MAC_ADDRESS}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_get_config_entry_diagnostics(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
"""Return diagnostics for a config entry."""
|
|
|
|
diag: dict[str, Any] = {}
|
|
|
|
|
|
|
|
diag["config"] = config_entry.as_dict()
|
|
|
|
|
|
|
|
entry_data = DomainData.get(hass).get_entry_data(config_entry)
|
|
|
|
|
|
|
|
if (storage_data := await entry_data.store.async_load()) is not None:
|
|
|
|
diag["storage_data"] = storage_data
|
|
|
|
|
2023-07-21 20:41:50 +00:00
|
|
|
if (
|
|
|
|
config_entry.unique_id
|
2023-12-21 04:33:43 +00:00
|
|
|
and (scanner := async_scanner_by_source(hass, config_entry.unique_id.upper()))
|
2023-07-21 20:41:50 +00:00
|
|
|
and (bluetooth_device := entry_data.bluetooth_device)
|
2022-11-27 19:59:37 +00:00
|
|
|
):
|
|
|
|
diag["bluetooth"] = {
|
2023-07-21 20:41:50 +00:00
|
|
|
"connections_free": bluetooth_device.ble_connections_free,
|
|
|
|
"connections_limit": bluetooth_device.ble_connections_limit,
|
2023-12-21 04:33:43 +00:00
|
|
|
"available": bluetooth_device.available,
|
2022-11-27 19:59:37 +00:00
|
|
|
"scanner": await scanner.async_diagnostics(),
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:15:37 +00:00
|
|
|
if dashboard := async_get_dashboard(hass):
|
|
|
|
diag["dashboard"] = dashboard.addon_slug
|
|
|
|
|
2022-01-26 22:35:41 +00:00
|
|
|
return async_redact_data(diag, REDACT_KEYS)
|