diff --git a/homeassistant/components/devolo_home_control/diagnostics.py b/homeassistant/components/devolo_home_control/diagnostics.py new file mode 100644 index 00000000000..412effcd5ed --- /dev/null +++ b/homeassistant/components/devolo_home_control/diagnostics.py @@ -0,0 +1,49 @@ +"""Diagnostics support for devolo Home Control.""" +from __future__ import annotations + +from typing import Any + +from devolo_home_control_api.homecontrol import HomeControl + +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 .const import DOMAIN + +TO_REDACT = {CONF_PASSWORD, CONF_USERNAME} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + gateways: list[HomeControl] = hass.data[DOMAIN][entry.entry_id]["gateways"] + + device_info = [] + for gateway in gateways: + device_info.append( + { + "gateway": { + "local_connection": gateway.gateway.local_connection, + "firmware_version": gateway.gateway.firmware_version, + }, + "devices": [ + { + "device_id": device_id, + "device_model_uid": properties.device_model_uid, + "device_type": properties.device_type, + "name": properties.name, + } + for device_id, properties in gateway.devices.items() + ], + } + ) + + diag_data = { + "entry": async_redact_data(entry.as_dict(), TO_REDACT), + "device_info": device_info, + } + + return diag_data diff --git a/tests/components/devolo_home_control/mocks.py b/tests/components/devolo_home_control/mocks.py index aef687936e4..61a9f1c7d8c 100644 --- a/tests/components/devolo_home_control/mocks.py +++ b/tests/components/devolo_home_control/mocks.py @@ -115,6 +115,8 @@ class DeviceMock(Zwave): self.brand = "devolo" self.name = "Test Device" self.uid = "Test" + self.device_model_uid = "Test" + self.device_type = "Test" self.settings_property = {"general_device_settings": SettingsMock()} self.href = "https://www.mydevolo.com" @@ -252,6 +254,9 @@ class HomeControlMock(HomeControl): """Initialize the mock.""" self.devices = {} self.publisher = MagicMock() + self.gateway = MagicMock() + self.gateway.local_connection = True + self.gateway.firmware_version = "8.94.0" def websocket_disconnect(self, event: str = "") -> None: """Mock disconnect of the websocket.""" diff --git a/tests/components/devolo_home_control/test_diagnostics.py b/tests/components/devolo_home_control/test_diagnostics.py new file mode 100644 index 00000000000..00c582ebf92 --- /dev/null +++ b/tests/components/devolo_home_control/test_diagnostics.py @@ -0,0 +1,65 @@ +"""Tests for the devolo Home Control diagnostics.""" +from __future__ import annotations + +from unittest.mock import patch + +from aiohttp import ClientSession + +from homeassistant.components.devolo_home_control.diagnostics import TO_REDACT +from homeassistant.components.diagnostics import REDACTED +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from . import configure_integration +from .mocks import HomeControlMock, HomeControlMockBinarySensor + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_entry_diagnostics(hass: HomeAssistant, hass_client: ClientSession): + """Test setup and state change of a climate device.""" + entry = configure_integration(hass) + gateway_1 = HomeControlMockBinarySensor() + gateway_2 = HomeControlMock() + with patch( + "homeassistant.components.devolo_home_control.HomeControl", + side_effect=[gateway_1, gateway_2], + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state == ConfigEntryState.LOADED + + entry_dict = entry.as_dict() + for key in TO_REDACT: + entry_dict["data"][key] = REDACTED + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result == { + "entry": entry_dict, + "device_info": [ + { + "gateway": { + "local_connection": gateway_1.gateway.local_connection, + "firmware_version": gateway_1.gateway.firmware_version, + }, + "devices": [ + { + "device_id": device_id, + "device_model_uid": properties.device_model_uid, + "device_type": properties.device_type, + "name": properties.name, + } + for device_id, properties in gateway_1.devices.items() + ], + }, + { + "gateway": { + "local_connection": gateway_2.gateway.local_connection, + "firmware_version": gateway_2.gateway.firmware_version, + }, + "devices": [], + }, + ], + }