From ab1f03f392b0570aaae577d518fa6c41831e3ae6 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Wed, 4 Dec 2024 09:37:17 +0100 Subject: [PATCH] Add diagnostics to Powerfox integration (#132226) * Add diagnostics to Powerfox integration * Update quality scale list --- .../components/powerfox/diagnostics.py | 58 +++++++++++++++++++ .../components/powerfox/quality_scale.yaml | 2 +- .../powerfox/snapshots/test_diagnostics.ambr | 26 +++++++++ tests/components/powerfox/test_diagnostics.py | 30 ++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/powerfox/diagnostics.py create mode 100644 tests/components/powerfox/snapshots/test_diagnostics.ambr create mode 100644 tests/components/powerfox/test_diagnostics.py diff --git a/homeassistant/components/powerfox/diagnostics.py b/homeassistant/components/powerfox/diagnostics.py new file mode 100644 index 00000000000000..8f6b847fca08c9 --- /dev/null +++ b/homeassistant/components/powerfox/diagnostics.py @@ -0,0 +1,58 @@ +"""Support for Powerfox diagnostics.""" + +from __future__ import annotations + +from datetime import datetime +from typing import Any + +from powerfox import PowerMeter, WaterMeter + +from homeassistant.core import HomeAssistant + +from . import PowerfoxConfigEntry, PowerfoxDataUpdateCoordinator + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: PowerfoxConfigEntry +) -> dict[str, Any]: + """Return diagnostics for Powerfox config entry.""" + powerfox_data: list[PowerfoxDataUpdateCoordinator] = entry.runtime_data + + return { + "devices": [ + { + **( + { + "power_meter": { + "outdated": coordinator.data.outdated, + "timestamp": datetime.strftime( + coordinator.data.timestamp, "%Y-%m-%d %H:%M:%S" + ), + "power": coordinator.data.power, + "energy_usage": coordinator.data.energy_usage, + "energy_return": coordinator.data.energy_return, + "energy_usage_high_tariff": coordinator.data.energy_usage_high_tariff, + "energy_usage_low_tariff": coordinator.data.energy_usage_low_tariff, + } + } + if isinstance(coordinator.data, PowerMeter) + else {} + ), + **( + { + "water_meter": { + "outdated": coordinator.data.outdated, + "timestamp": datetime.strftime( + coordinator.data.timestamp, "%Y-%m-%d %H:%M:%S" + ), + "cold_water": coordinator.data.cold_water, + "warm_water": coordinator.data.warm_water, + } + } + if isinstance(coordinator.data, WaterMeter) + else {} + ), + } + for coordinator in powerfox_data + ], + } diff --git a/homeassistant/components/powerfox/quality_scale.yaml b/homeassistant/components/powerfox/quality_scale.yaml index 43172a2e84a108..5a14264940fc06 100644 --- a/homeassistant/components/powerfox/quality_scale.yaml +++ b/homeassistant/components/powerfox/quality_scale.yaml @@ -51,7 +51,7 @@ rules: # Gold devices: done - diagnostics: todo + diagnostics: done discovery-update-info: status: exempt comment: | diff --git a/tests/components/powerfox/snapshots/test_diagnostics.ambr b/tests/components/powerfox/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..781e7b8c0d542e --- /dev/null +++ b/tests/components/powerfox/snapshots/test_diagnostics.ambr @@ -0,0 +1,26 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'devices': list([ + dict({ + 'power_meter': dict({ + 'energy_return': 111.111, + 'energy_usage': 1111.111, + 'energy_usage_high_tariff': 111.111, + 'energy_usage_low_tariff': 111.111, + 'outdated': False, + 'power': 111, + 'timestamp': '2024-11-26 10:48:51', + }), + }), + dict({ + 'water_meter': dict({ + 'cold_water': 1111.111, + 'outdated': False, + 'timestamp': '2024-11-26 10:48:51', + 'warm_water': 0.0, + }), + }), + ]), + }) +# --- diff --git a/tests/components/powerfox/test_diagnostics.py b/tests/components/powerfox/test_diagnostics.py new file mode 100644 index 00000000000000..7dc2c3c7263abd --- /dev/null +++ b/tests/components/powerfox/test_diagnostics.py @@ -0,0 +1,30 @@ +"""Test for PowerFox diagnostics.""" + +from unittest.mock import AsyncMock + +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from . import setup_integration + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_powerfox_client: AsyncMock, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test the PowerFox entry diagnostics.""" + await setup_integration(hass, mock_config_entry) + + result = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + + assert result == snapshot