From 8dbf7f7ad725a4b96df59339e88238dd589e378a Mon Sep 17 00:00:00 2001 From: Christian Lackas Date: Tue, 24 Feb 2026 16:25:04 +0100 Subject: [PATCH] Add diagnostics support to homematicip_cloud (#163829) --- .../homematicip_cloud/diagnostics.py | 27 ++++++++++++++++ .../fixtures/diagnostics.json | 29 +++++++++++++++++ .../snapshots/test_diagnostics.ambr | 32 +++++++++++++++++++ .../homematicip_cloud/test_diagnostics.py | 28 ++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 homeassistant/components/homematicip_cloud/diagnostics.py create mode 100644 tests/components/homematicip_cloud/fixtures/diagnostics.json create mode 100644 tests/components/homematicip_cloud/snapshots/test_diagnostics.ambr create mode 100644 tests/components/homematicip_cloud/test_diagnostics.py diff --git a/homeassistant/components/homematicip_cloud/diagnostics.py b/homeassistant/components/homematicip_cloud/diagnostics.py new file mode 100644 index 00000000000..64f418cbcc0 --- /dev/null +++ b/homeassistant/components/homematicip_cloud/diagnostics.py @@ -0,0 +1,27 @@ +"""Diagnostics support for HomematicIP Cloud.""" + +from __future__ import annotations + +import json +from typing import Any + +from homematicip.base.helpers import handle_config + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from .hap import HomematicIPConfigEntry + +TO_REDACT_CONFIG = {"city", "latitude", "longitude", "refreshToken"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: HomematicIPConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + hap = config_entry.runtime_data + json_state = await hap.home.download_configuration_async() + anonymized = handle_config(json_state, anonymize=True) + config = json.loads(anonymized) + + return async_redact_data(config, TO_REDACT_CONFIG) diff --git a/tests/components/homematicip_cloud/fixtures/diagnostics.json b/tests/components/homematicip_cloud/fixtures/diagnostics.json new file mode 100644 index 00000000000..24e541e4030 --- /dev/null +++ b/tests/components/homematicip_cloud/fixtures/diagnostics.json @@ -0,0 +1,29 @@ +{ + "accessPointId": "3014F7110000000000000001", + "home": { + "id": "a1b2c3d4-e5f6-1234-abcd-ef0123456789", + "location": { + "city": "Berlin, Germany", + "latitude": "52.520008", + "longitude": "13.404954" + }, + "weather": { + "temperature": 18.3 + } + }, + "devices": { + "3014F7110000000000000002": { + "id": "3014F7110000000000000002", + "label": "Living Room Thermostat", + "type": "WALL_MOUNTED_THERMOSTAT_PRO", + "serializedGlobalTradeItemNumber": "ABCDEFGHIJKLMNOPQRSTUVWX" + } + }, + "clients": { + "a1b2c3d4-e5f6-1234-abcd-ef0123456789": { + "id": "a1b2c3d4-e5f6-1234-abcd-ef0123456789", + "label": "Home Assistant", + "refreshToken": "secret-refresh-token" + } + } +} diff --git a/tests/components/homematicip_cloud/snapshots/test_diagnostics.ambr b/tests/components/homematicip_cloud/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..4cdedde3273 --- /dev/null +++ b/tests/components/homematicip_cloud/snapshots/test_diagnostics.ambr @@ -0,0 +1,32 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'accessPointId': '3014F7110000000000000000', + 'clients': dict({ + '00000000-0000-0000-0000-000000000000': dict({ + 'id': '00000000-0000-0000-0000-000000000000', + 'label': 'Home Assistant', + 'refreshToken': None, + }), + }), + 'devices': dict({ + '3014F7110000000000000001': dict({ + 'id': '3014F7110000000000000001', + 'label': 'Living Room Thermostat', + 'serializedGlobalTradeItemNumber': '3014F7110000000000000002', + 'type': 'WALL_MOUNTED_THERMOSTAT_PRO', + }), + }), + 'home': dict({ + 'id': '00000000-0000-0000-0000-000000000000', + 'location': dict({ + 'city': '**REDACTED**', + 'latitude': '**REDACTED**', + 'longitude': '**REDACTED**', + }), + 'weather': dict({ + 'temperature': 18.3, + }), + }), + }) +# --- diff --git a/tests/components/homematicip_cloud/test_diagnostics.py b/tests/components/homematicip_cloud/test_diagnostics.py new file mode 100644 index 00000000000..7ec9e67fd4d --- /dev/null +++ b/tests/components/homematicip_cloud/test_diagnostics.py @@ -0,0 +1,28 @@ +"""Tests for HomematicIP Cloud diagnostics.""" + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.homematicip_cloud.const import DOMAIN +from homeassistant.core import HomeAssistant + +from tests.common import async_load_json_object_fixture +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_hap_with_service, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics for config entry.""" + mock_hap_with_service.home.download_configuration_async.return_value = ( + await async_load_json_object_fixture(hass, "diagnostics.json", DOMAIN) + ) + + entry = hass.config_entries.async_entries(DOMAIN)[0] + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result == snapshot