diff --git a/homeassistant/components/bond/diagnostics.py b/homeassistant/components/bond/diagnostics.py new file mode 100644 index 00000000000..6af62c3fb24 --- /dev/null +++ b/homeassistant/components/bond/diagnostics.py @@ -0,0 +1,39 @@ +"""Diagnostics support for bond.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +from .const import DOMAIN, HUB +from .utils import BondHub + +TO_REDACT = {"access_token"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data = hass.data[DOMAIN][entry.entry_id] + hub: BondHub = data[HUB] + return { + "entry": { + "title": entry.title, + "data": async_redact_data(entry.data, TO_REDACT), + }, + "hub": { + "version": hub._version, # pylint: disable=protected-access + }, + "devices": [ + { + "device_id": device.device_id, + "props": device.props, + "attrs": device._attrs, # pylint: disable=protected-access + "supported_actions": device._supported_actions, # pylint: disable=protected-access + } + for device in hub.devices + ], + } diff --git a/tests/components/bond/common.py b/tests/components/bond/common.py index 0400b466e34..9c53c0afb8b 100644 --- a/tests/components/bond/common.py +++ b/tests/components/bond/common.py @@ -8,6 +8,7 @@ from typing import Any from unittest.mock import MagicMock, patch from aiohttp.client_exceptions import ClientResponseError +from bond_api import DeviceType from homeassistant import core from homeassistant.components.bond.const import DOMAIN as BOND_DOMAIN @@ -18,6 +19,15 @@ from homeassistant.util import utcnow from tests.common import MockConfigEntry, async_fire_time_changed +def ceiling_fan_with_breeze(name: str): + """Create a ceiling fan with given name with breeze support.""" + return { + "name": name, + "type": DeviceType.CEILING_FAN, + "actions": ["SetSpeed", "SetDirection", "BreezeOn"], + } + + def patch_setup_entry(domain: str, *, enabled: bool = True): """Patch async_setup_entry for specified domain.""" if not enabled: diff --git a/tests/components/bond/test_diagnostics.py b/tests/components/bond/test_diagnostics.py new file mode 100644 index 00000000000..88d33ff2cc0 --- /dev/null +++ b/tests/components/bond/test_diagnostics.py @@ -0,0 +1,43 @@ +"""Test bond diagnostics.""" + +from homeassistant.components.fan import DOMAIN as FAN_DOMAIN + +from .common import ceiling_fan_with_breeze, setup_platform + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_diagnostics(hass, hass_client): + """Test generating diagnostics for a config entry.""" + + entry = await setup_platform( + hass, + FAN_DOMAIN, + ceiling_fan_with_breeze("name-1"), + bond_device_id="test-device-id", + props={"max_speed": 6}, + ) + diag = await get_diagnostics_for_config_entry(hass, hass_client, entry) + mock_device = diag["devices"][0] + mock_device["attrs"]["actions"] = set(mock_device["attrs"]["actions"]) + mock_device["supported_actions"] = set(mock_device["supported_actions"]) + + assert diag == { + "devices": [ + { + "attrs": { + "actions": {"SetSpeed", "SetDirection", "BreezeOn"}, + "name": "name-1", + "type": "CF", + }, + "device_id": "test-device-id", + "props": {"max_speed": 6}, + "supported_actions": {"BreezeOn", "SetSpeed", "SetDirection"}, + } + ], + "entry": { + "data": {"access_token": "**REDACTED**", "host": "some host"}, + "title": "Mock Title", + }, + "hub": {"version": {"bondid": "test-bond-id"}}, + }