Add diagnostics support to Zeversolar integration (#118245)
This commit is contained in:
parent
65a740f35e
commit
6045c2bb08
5 changed files with 137 additions and 7 deletions
58
homeassistant/components/zeversolar/diagnostics.py
Normal file
58
homeassistant/components/zeversolar/diagnostics.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
"""Provides diagnostics for Zeversolar."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from zeversolar import ZeverSolarData
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ZeversolarCoordinator
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
|
||||
coordinator: ZeversolarCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
data: ZeverSolarData = coordinator.data
|
||||
|
||||
payload: dict[str, Any] = {
|
||||
"wifi_enabled": data.wifi_enabled,
|
||||
"serial_or_registry_id": data.serial_or_registry_id,
|
||||
"registry_key": data.registry_key,
|
||||
"hardware_version": data.hardware_version,
|
||||
"software_version": data.software_version,
|
||||
"reported_datetime": data.reported_datetime,
|
||||
"communication_status": data.communication_status.value,
|
||||
"num_inverters": data.num_inverters,
|
||||
"serial_number": data.serial_number,
|
||||
"pac": data.pac,
|
||||
"status": data.status.value,
|
||||
"meter_status": data.meter_status.value,
|
||||
}
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
async def async_get_device_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a device entry."""
|
||||
coordinator: ZeversolarCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
updateInterval = (
|
||||
None
|
||||
if coordinator.update_interval is None
|
||||
else coordinator.update_interval.total_seconds()
|
||||
)
|
||||
|
||||
return {
|
||||
"name": coordinator.name,
|
||||
"always_update": coordinator.always_update,
|
||||
"last_update_success": coordinator.last_update_success,
|
||||
"update_interval": updateInterval,
|
||||
}
|
|
@ -12,6 +12,7 @@ from tests.common import MockConfigEntry
|
|||
|
||||
MOCK_HOST_ZEVERSOLAR = "zeversolar-fake-host"
|
||||
MOCK_PORT_ZEVERSOLAR = 10200
|
||||
MOCK_SERIAL_NUMBER = "123456778"
|
||||
|
||||
|
||||
async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
||||
|
@ -19,16 +20,16 @@ async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
|||
|
||||
zeverData = ZeverSolarData(
|
||||
wifi_enabled=False,
|
||||
serial_or_registry_id="1223",
|
||||
registry_key="A-2",
|
||||
serial_or_registry_id="EAB9615C0001",
|
||||
registry_key="WSMQKHTQ3JVYQWA9",
|
||||
hardware_version="M10",
|
||||
software_version="123-23",
|
||||
reported_datetime="19900101 23:00",
|
||||
software_version="19703-826R+17511-707R",
|
||||
reported_datetime="19900101 23:01:45",
|
||||
communication_status=StatusEnum.OK,
|
||||
num_inverters=1,
|
||||
serial_number="123456778",
|
||||
serial_number=MOCK_SERIAL_NUMBER,
|
||||
pac=1234,
|
||||
energy_today=123,
|
||||
energy_today=123.4,
|
||||
status=StatusEnum.OK,
|
||||
meter_status=StatusEnum.OK,
|
||||
)
|
||||
|
|
25
tests/components/zeversolar/snapshots/test_diagnostics.ambr
Normal file
25
tests/components/zeversolar/snapshots/test_diagnostics.ambr
Normal file
|
@ -0,0 +1,25 @@
|
|||
# serializer version: 1
|
||||
# name: test_device_diagnostics
|
||||
dict({
|
||||
'always_update': True,
|
||||
'last_update_success': True,
|
||||
'name': 'zeversolar',
|
||||
'update_interval': 60.0,
|
||||
})
|
||||
# ---
|
||||
# name: test_entry_diagnostics
|
||||
dict({
|
||||
'communication_status': 'OK',
|
||||
'hardware_version': 'M10',
|
||||
'meter_status': 'OK',
|
||||
'num_inverters': 1,
|
||||
'pac': 1234,
|
||||
'registry_key': 'WSMQKHTQ3JVYQWA9',
|
||||
'reported_datetime': '19900101 23:01:45',
|
||||
'serial_number': '123456778',
|
||||
'serial_or_registry_id': 'EAB9615C0001',
|
||||
'software_version': '19703-826R+17511-707R',
|
||||
'status': 'OK',
|
||||
'wifi_enabled': False,
|
||||
})
|
||||
# ---
|
|
@ -67,7 +67,7 @@
|
|||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '123',
|
||||
'state': '123.4',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.zeversolar_sensor_power-entry]
|
||||
|
|
46
tests/components/zeversolar/test_diagnostics.py
Normal file
46
tests/components/zeversolar/test_diagnostics.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
"""Tests for the diagnostics data provided by the Zeversolar integration."""
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.zeversolar import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from . import MOCK_SERIAL_NUMBER, init_integration
|
||||
|
||||
from tests.components.diagnostics import (
|
||||
get_diagnostics_for_config_entry,
|
||||
get_diagnostics_for_device,
|
||||
)
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
|
||||
entry = await init_integration(hass)
|
||||
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot
|
||||
|
||||
|
||||
async def test_device_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test device diagnostics."""
|
||||
|
||||
entry = await init_integration(hass)
|
||||
|
||||
device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, MOCK_SERIAL_NUMBER)}
|
||||
)
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_device(hass, hass_client, entry, device) == snapshot
|
||||
)
|
Loading…
Add table
Reference in a new issue