Add diagnostics to devolo Home Control (#86069)

* Add diagnostics to devolo Home Control

* Apply feedback
This commit is contained in:
Guido Schmitz 2023-05-05 21:24:42 +02:00 committed by GitHub
parent 774f1c8ef9
commit 2b3f7ad70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 0 deletions

View File

@ -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

View File

@ -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."""

View File

@ -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": [],
},
],
}