From 7326555f03fd4801536d381923d1681962f65218 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 21 Dec 2024 11:38:33 +0100 Subject: [PATCH] Add diagnostic to Peblar Rocksolid EV Chargers integration (#133706) --- .../components/peblar/diagnostics.py | 23 +++++ .../components/peblar/quality_scale.yaml | 2 +- tests/components/peblar/conftest.py | 30 +++++- .../peblar/fixtures/available_versions.json | 4 + .../peblar/fixtures/current_versions.json | 4 + tests/components/peblar/fixtures/meter.json | 14 +++ .../peblar/snapshots/test_diagnostics.ambr | 93 +++++++++++++++++++ tests/components/peblar/test_diagnostics.py | 22 +++++ 8 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/peblar/diagnostics.py create mode 100644 tests/components/peblar/fixtures/available_versions.json create mode 100644 tests/components/peblar/fixtures/current_versions.json create mode 100644 tests/components/peblar/fixtures/meter.json create mode 100644 tests/components/peblar/snapshots/test_diagnostics.ambr create mode 100644 tests/components/peblar/test_diagnostics.py diff --git a/homeassistant/components/peblar/diagnostics.py b/homeassistant/components/peblar/diagnostics.py new file mode 100644 index 00000000000..91cdb5dc811 --- /dev/null +++ b/homeassistant/components/peblar/diagnostics.py @@ -0,0 +1,23 @@ +"""Diagnostics support for Peblar.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.core import HomeAssistant + +from .coordinator import PeblarConfigEntry + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: PeblarConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + return { + "system_information": entry.runtime_data.system_information.to_dict(), + "meter": entry.runtime_data.meter_coordinator.data.to_dict(), + "versions": { + "available": entry.runtime_data.version_coordinator.data.available.to_dict(), + "current": entry.runtime_data.version_coordinator.data.current.to_dict(), + }, + } diff --git a/homeassistant/components/peblar/quality_scale.yaml b/homeassistant/components/peblar/quality_scale.yaml index 51bd60cc4b4..3dc470ce76b 100644 --- a/homeassistant/components/peblar/quality_scale.yaml +++ b/homeassistant/components/peblar/quality_scale.yaml @@ -40,7 +40,7 @@ rules: test-coverage: todo # Gold devices: todo - diagnostics: todo + diagnostics: done discovery-update-info: todo discovery: todo docs-data-update: todo diff --git a/tests/components/peblar/conftest.py b/tests/components/peblar/conftest.py index 583b2cbe7a5..ece9a8d9973 100644 --- a/tests/components/peblar/conftest.py +++ b/tests/components/peblar/conftest.py @@ -5,11 +5,12 @@ from __future__ import annotations from collections.abc import Generator from unittest.mock import MagicMock, patch -from peblar.models import PeblarSystemInformation +from peblar import PeblarMeter, PeblarSystemInformation, PeblarVersions import pytest from homeassistant.components.peblar.const import DOMAIN from homeassistant.const import CONF_HOST, CONF_PASSWORD +from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry, load_fixture @@ -43,7 +44,34 @@ def mock_peblar() -> Generator[MagicMock]: patch("homeassistant.components.peblar.config_flow.Peblar", new=peblar_mock), ): peblar = peblar_mock.return_value + peblar.available_versions.return_value = PeblarVersions.from_json( + load_fixture("available_versions.json", DOMAIN) + ) + peblar.current_versions.return_value = PeblarVersions.from_json( + load_fixture("current_versions.json", DOMAIN) + ) peblar.system_information.return_value = PeblarSystemInformation.from_json( load_fixture("system_information.json", DOMAIN) ) + + api = peblar.rest_api.return_value + api.meter.return_value = PeblarMeter.from_json( + load_fixture("meter.json", DOMAIN) + ) + yield peblar + + +@pytest.fixture +async def init_integration( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_peblar: MagicMock, +) -> MockConfigEntry: + """Set up the Peblar integration for testing.""" + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + return mock_config_entry diff --git a/tests/components/peblar/fixtures/available_versions.json b/tests/components/peblar/fixtures/available_versions.json new file mode 100644 index 00000000000..45b3255167c --- /dev/null +++ b/tests/components/peblar/fixtures/available_versions.json @@ -0,0 +1,4 @@ +{ + "Customization": "Peblar-1.9", + "Firmware": "1.6.2+1+WL-1" +} diff --git a/tests/components/peblar/fixtures/current_versions.json b/tests/components/peblar/fixtures/current_versions.json new file mode 100644 index 00000000000..c54fb71c457 --- /dev/null +++ b/tests/components/peblar/fixtures/current_versions.json @@ -0,0 +1,4 @@ +{ + "Customization": "Peblar-1.9", + "Firmware": "1.6.1+1+WL-1" +} diff --git a/tests/components/peblar/fixtures/meter.json b/tests/components/peblar/fixtures/meter.json new file mode 100644 index 00000000000..1f32a3fbebc --- /dev/null +++ b/tests/components/peblar/fixtures/meter.json @@ -0,0 +1,14 @@ +{ + "CurrentPhase1": 0, + "CurrentPhase2": 0, + "CurrentPhase3": 0, + "EnergySession": 0, + "EnergyTotal": 880321, + "PowerPhase1": 0, + "PowerPhase2": 0, + "PowerPhase3": 0, + "PowerTotal": 0, + "VoltagePhase1": 230, + "VoltagePhase2": null, + "VoltagePhase3": null +} diff --git a/tests/components/peblar/snapshots/test_diagnostics.ambr b/tests/components/peblar/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..7701c1eb159 --- /dev/null +++ b/tests/components/peblar/snapshots/test_diagnostics.ambr @@ -0,0 +1,93 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'meter': dict({ + 'CurrentPhase1': 0, + 'CurrentPhase2': 0, + 'CurrentPhase3': 0, + 'EnergySession': 0, + 'EnergyTotal': 880321, + 'PowerPhase1': 0, + 'PowerPhase2': 0, + 'PowerPhase3': 0, + 'PowerTotal': 0, + 'VoltagePhase1': 230, + }), + 'system_information': dict({ + 'BopCalIGainA': 264625, + 'BopCalIGainB': 267139, + 'BopCalIGainC': 239155, + 'CanChangeChargingPhases': False, + 'CanChargeSinglePhase': True, + 'CanChargeThreePhases': False, + 'CustomerId': 'PBLR-0000645', + 'CustomerUpdatePackagePubKey': ''' + -----BEGIN PUBLIC KEY----- + lorem ipsum + -----END PUBLIC KEY----- + + ''', + 'EthMacAddr': '00:0F:11:58:86:97', + 'FwIdent': '1.6.1+1+WL-1', + 'Hostname': 'PBLR-0000645', + 'HwFixedCableRating': 20, + 'HwFwCompat': 'wlac-2', + 'HwHas4pRelay': False, + 'HwHasBop': True, + 'HwHasBuzzer': True, + 'HwHasDualSocket': False, + 'HwHasEichrechtLaserMarking': False, + 'HwHasEthernet': True, + 'HwHasLed': True, + 'HwHasLte': False, + 'HwHasMeter': True, + 'HwHasMeterDisplay': True, + 'HwHasPlc': False, + 'HwHasRfid': True, + 'HwHasRs485': True, + 'HwHasShutter': False, + 'HwHasSocket': False, + 'HwHasTpm': False, + 'HwHasWlan': True, + 'HwMaxCurrent': 16, + 'HwOneOrThreePhase': 3, + 'HwUKCompliant': False, + 'MainboardPn': '6004-2300-7600', + 'MainboardSn': '23-38-A4E-2MC', + 'MeterCalIGainA': 267369, + 'MeterCalIGainB': 228286, + 'MeterCalIGainC': 246455, + 'MeterCalIRmsOffsetA': 15573, + 'MeterCalIRmsOffsetB': 268422963, + 'MeterCalIRmsOffsetC': 9082, + 'MeterCalPhaseA': 250, + 'MeterCalPhaseB': 271, + 'MeterCalPhaseC': 271, + 'MeterCalVGainA': 250551, + 'MeterCalVGainB': 246074, + 'MeterCalVGainC': 230191, + 'MeterFwIdent': 'b9cbcd', + 'NorFlash': 'True', + 'ProductModelName': 'WLAC1-H11R0WE0ICR00', + 'ProductPn': '6004-2300-8002', + 'ProductSn': '23-45-A4O-MOF', + 'ProductVendorName': 'Peblar', + 'WlanApMacAddr': '00:0F:11:58:86:98', + 'WlanStaMacAddr': '00:0F:11:58:86:99', + }), + 'versions': dict({ + 'available': dict({ + 'Customization': 'Peblar-1.9', + 'Firmware': '1.6.2+1+WL-1', + 'customization_version': '1.9', + 'firmware_version': '1.6.2', + }), + 'current': dict({ + 'Customization': 'Peblar-1.9', + 'Firmware': '1.6.1+1+WL-1', + 'customization_version': '1.9', + 'firmware_version': '1.6.1', + }), + }), + }) +# --- diff --git a/tests/components/peblar/test_diagnostics.py b/tests/components/peblar/test_diagnostics.py new file mode 100644 index 00000000000..11f9af28b2d --- /dev/null +++ b/tests/components/peblar/test_diagnostics.py @@ -0,0 +1,22 @@ +"""Tests for the diagnostics data provided by the Peblar integration.""" + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + init_integration: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics.""" + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, init_integration) + == snapshot + )