"""Diagnostics support for Asuswrt."""
from __future__ import annotations

from typing import Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er

from .const import DATA_ASUSWRT, DOMAIN
from .router import AsusWrtRouter

TO_REDACT = {CONF_PASSWORD, CONF_USERNAME}


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

    router: AsusWrtRouter = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]

    # Gather information how this AsusWrt device is represented in Home Assistant
    device_registry = dr.async_get(hass)
    entity_registry = er.async_get(hass)
    hass_device = device_registry.async_get_device(
        identifiers=router.device_info["identifiers"]
    )
    if not hass_device:
        return data

    data["device"] = {
        "name": hass_device.name,
        "name_by_user": hass_device.name_by_user,
        "disabled": hass_device.disabled,
        "disabled_by": hass_device.disabled_by,
        "device_info": async_redact_data(dict(router.device_info), {"identifiers"}),
        "entities": {},
        "tracked_devices": [],
    }

    hass_entities = er.async_entries_for_device(
        entity_registry,
        device_id=hass_device.id,
        include_disabled_entities=True,
    )

    for entity_entry in hass_entities:
        state = hass.states.get(entity_entry.entity_id)
        state_dict = None
        if state:
            state_dict = dict(state.as_dict())
            # The entity_id is already provided at root level.
            state_dict.pop("entity_id", None)
            # The context doesn't provide useful information in this case.
            state_dict.pop("context", None)

        data["device"]["entities"][entity_entry.entity_id] = {
            "name": entity_entry.name,
            "original_name": entity_entry.original_name,
            "disabled": entity_entry.disabled,
            "disabled_by": entity_entry.disabled_by,
            "entity_category": entity_entry.entity_category,
            "device_class": entity_entry.device_class,
            "original_device_class": entity_entry.original_device_class,
            "icon": entity_entry.icon,
            "original_icon": entity_entry.original_icon,
            "unit_of_measurement": entity_entry.unit_of_measurement,
            "state": state_dict,
        }

    for device in router.devices.values():
        data["device"]["tracked_devices"].append(
            {
                "name": device.name or "Unknown device",
                "ip_address": device.ip_address,
                "last_activity": device.last_activity,
            }
        )

    return data

@@ -92,16 +92,29 @@
 rt Any%0A%0A
+import attr%0A%0A
 from hom
@@ -257,16 +257,66 @@
  import 
+(%0A    ATTR_CONNECTIONS,%0A    ATTR_IDENTIFIERS,%0A    
 CONF_PAS
@@ -313,32 +313,36 @@
   CONF_PASSWORD,
+%0A   
  CONF_USERNAME%0Af
@@ -339,16 +339,19 @@
 USERNAME
+,%0A)
 %0Afrom ho
@@ -586,16 +586,69 @@
 ERNAME%7D%0A
+TO_REDACT_DEV = %7BATTR_CONNECTIONS, ATTR_IDENTIFIERS%7D%0A
 %0A%0Aasync 
@@ -1314,256 +1314,67 @@
     
-%22name%22: hass_device.name,%0A        %22name_by_user%22: hass_device.name_by_user,%0A        %22disabled%22: hass_device.disabled,%0A        %22disabled_by%22: hass_device.disabled_by,%0A        %22device_info%22: async_redact_data(dict(router.device_info), %7B%22identifiers%22%7D
+**async_redact_data(attr.asdict(hass_device), TO_REDACT_DEV
 ),%0A 
@@ -2083,544 +2083,203 @@
     
-%22name%22: entity_entry.name,%0A            %22original_name%22: entity_entry.original_name,%0A            %22disabled%22: entity_entry.disabled,%0A            %22disabled_by%22: entity_entry.disabled_by,%0A
+**async_redact_data(%0A                attr.asdict(%0A             
        
-     %22
 entity_
-category%22: entity_entry.entity_category,%0A            %22device_class%22: entity_entry.device_class,%0A            %22original_device_class%22: entity_entry.original_device_class,%0A            %22icon%22: entity_entry.icon,%0A            %22original_icon%22: entity_entry.original_icon,%0A            %22unit_of_measurement%22: entity_entry.unit_of_measurement
+entry, filter=lambda attr, value: attr.name != %22entity_id%22%0A                ),%0A                TO_REDACT,%0A            )
 ,%0A  

Improve AsusWRT diagnostics (#69826)