Skip to content

Commit

Permalink
Add diagnostics to Powerfox integration (home-assistant#132226)
Browse files Browse the repository at this point in the history
* Add diagnostics to Powerfox integration

* Update quality scale list
  • Loading branch information
klaasnicolaas authored and zweckj committed Dec 5, 2024
1 parent 96d7f2e commit 650a500
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
58 changes: 58 additions & 0 deletions homeassistant/components/powerfox/diagnostics.py
Original file line number Diff line number Diff line change
@@ -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
],
}
2 changes: 1 addition & 1 deletion homeassistant/components/powerfox/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rules:

# Gold
devices: done
diagnostics: todo
diagnostics: done
discovery-update-info:
status: exempt
comment: |
Expand Down
26 changes: 26 additions & 0 deletions tests/components/powerfox/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
@@ -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,
}),
}),
]),
})
# ---
30 changes: 30 additions & 0 deletions tests/components/powerfox/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 650a500

Please sign in to comment.