Skip to content

Commit

Permalink
Add device to diagnostics (#1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp authored Aug 18, 2024
1 parent 446cd20 commit 5c9156d
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions custom_components/battery_notes/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
"""Diagnostic helpers."""

from __future__ import annotations

from typing import Any

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
device_registry as dr,
)
from homeassistant.helpers import (
entity_registry as er,
)

from .common import get_device_model_id
from .const import CONF_SOURCE_ENTITY_ID


async def async_get_config_entry_diagnostics(
entry: ConfigEntry,
) -> dict:
hass: HomeAssistant, config_entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
return {
"entry": entry.as_dict(),
}

device_id = config_entry.data.get(CONF_DEVICE_ID, None)
source_entity_id = config_entry.data.get(CONF_SOURCE_ENTITY_ID, None)

device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)

if source_entity_id:
entity = entity_registry.async_get(source_entity_id)
device_id = entity.device_id

device_entry = device_registry.async_get(device_id)

diagnostics = {"entry": config_entry.as_dict()}
if device_entry:
device_info = {
"manufacturer": device_entry.manufacturer,
"model": device_entry.model,
"model_id": get_device_model_id(device_entry),
"hw_version": device_entry.hw_version,
}
diagnostics.update({"device": device_info})

return diagnostics

0 comments on commit 5c9156d

Please sign in to comment.