Add diagnostics to my-PV (#187683)

This commit is contained in:
rrooggiieerr
2026-10-22 19:56:23 +02:00
committed by GitHub
parent 123fadf9c9
commit 35bf2db683
4 changed files with 257 additions and 1 deletions
@@ -0,0 +1,47 @@
"""Diagnostics support for the my-PV integration."""
from typing import Any
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from .coordinator import MyPVConfigEntry
TO_REDACT = {CONF_HOST, CONF_PASSWORD, "mac_address", "serial_number"}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: MyPVConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
device = entry.runtime_data.device
diagnostics: dict[str, Any] = {
"entry_data": dict(entry.data),
"device": {
"model": device.model,
"serial_number": device.serial_number,
# The prefix identifies the device type and configuration, not the device
"serial_number_prefix": device.serial_number[:6],
"mac_address": device.mac_address,
"hardware_version": device.hardware_version,
"firmware_version": device.firmware_version,
"connected": device.connected,
},
"setup_configurations": device.get_setup_configurations(),
"data_configurations": device.get_data_configurations(),
}
# The device raises an error when reading values while not connected
if device.connected:
diagnostics["setup_values"] = {
key: device.get_setup_value(key)
for key in diagnostics["setup_configurations"]
}
diagnostics["data_values"] = {
key: device.get_data_value(key)
for key in diagnostics["data_configurations"]
}
return async_redact_data(diagnostics, TO_REDACT)
@@ -47,7 +47,7 @@ rules:
# Gold tier rules
devices: done
diagnostics: todo
diagnostics: done
discovery-update-info: done
discovery: done
docs-data-update: done
@@ -0,0 +1,141 @@
# serializer version: 1
# name: test_diagnostics
dict({
'data_configurations': dict({
'cur_eth_mode': dict({
'options': dict({
'0': 'LAN',
'1': 'WLAN',
'2': 'AP',
}),
'type': 'enumeration',
}),
'freq': dict({
'type': 'number',
'unit': 'Hz',
}),
'power': dict({
'type': 'number',
'unit': 'W',
}),
'screen_mode_flag': dict({
'options': dict({
'0': 'Standby',
'1': 'Heat',
'2': 'Boost',
'3': 'Heating finished',
'4': 'No connection / Disabled',
'5': 'Error',
'6': 'Block active',
}),
'type': 'enumeration',
}),
'temp1': dict({
'type': 'number',
'unit': '°C',
}),
'temp2': dict({
'type': 'number',
'unit': '°C',
}),
'temp3': dict({
'type': 'number',
'unit': '°C',
}),
'temp4': dict({
'type': 'number',
'unit': '°C',
}),
'temp_ps': dict({
'type': 'number',
'unit': '°C',
}),
'uptime': dict({
'type': 'number',
'unit': 'h',
}),
'volt_mains': dict({
'type': 'number',
'unit': 'V',
}),
}),
'data_values': dict({
'cur_eth_mode': '0',
'freq': 49.965,
'power': 3445,
'screen_mode_flag': '4',
'temp1': 12.3,
'temp2': 23.4,
'temp3': 34.5,
'temp4': 45.6,
'temp_ps': 56.7,
'uptime': 2,
'volt_mains': 238,
}),
'device': dict({
'connected': True,
'firmware_version': 'e0002200',
'hardware_version': 'v1.5A',
'mac_address': '**REDACTED**',
'model': 'AC ELWA 2',
'serial_number': '**REDACTED**',
'serial_number_prefix': '160150',
}),
'entry_data': dict({
'host': '**REDACTED**',
'password': '**REDACTED**',
}),
'setup_configurations': dict({
'bstmode': dict({
'options': dict({
'0': 'Off',
'1': 'On',
'3': 'Relais',
}),
'type': 'enumeration',
}),
'bsttemp': dict({
'max': 95.0,
'min': 5.0,
'step': 0.1,
'type': 'number',
'unit': '°C',
}),
'ww1boost': dict({
'max': 95.0,
'min': 5.0,
'step': 0.1,
'type': 'number',
'unit': '°C',
}),
'ww1target': dict({
'max': 95.0,
'min': 5.0,
'step': 0.1,
'unit': '°C',
}),
'ww_boost_h': dict({
'max': 9.9,
'min': 0.1,
'step': 0.1,
'type': 'number',
'unit': '°C',
}),
'ww_targ_h': dict({
'max': 9.9,
'min': 0.1,
'step': 0.1,
'type': 'number',
'unit': '°C',
}),
}),
'setup_values': dict({
'bstmode': '0',
'bsttemp': 55.0,
'ww1boost': 65.0,
'ww1target': None,
'ww_boost_h': 3.5,
'ww_targ_h': 4.5,
}),
})
# ---
@@ -0,0 +1,68 @@
"""Tests for the diagnostics data provided by the my-PV integration."""
from unittest.mock import AsyncMock, Mock
from my_pv.exceptions import MyPVConnectionError
import pytest
from pytest_homeassistant.common import MockConfigEntry
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
from pytest_homeassistant.typing import ClientSessionGenerator
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.diagnostics import REDACTED
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.core import HomeAssistant
@pytest.mark.usefixtures("mock_my_pv_client")
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert (
await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
== snapshot
)
async def test_diagnostics_disconnected(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
mock_my_pv_client: AsyncMock,
) -> None:
"""Test diagnostics when the device disconnected after setup."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
# Reading values from a disconnected device raises an error
mock_my_pv_client.connected = False
mock_my_pv_client.get_setup_value = Mock(side_effect=MyPVConnectionError)
mock_my_pv_client.get_data_value = Mock(side_effect=MyPVConnectionError)
diagnostics = await get_diagnostics_for_config_entry(
hass, hass_client, mock_config_entry
)
mock_my_pv_client.get_setup_value.assert_not_called()
mock_my_pv_client.get_data_value.assert_not_called()
assert "setup_values" not in diagnostics
assert "data_values" not in diagnostics
assert diagnostics["entry_data"][CONF_HOST] == REDACTED
assert diagnostics["entry_data"][CONF_PASSWORD] == REDACTED
assert diagnostics["device"]["serial_number"] == REDACTED
assert diagnostics["device"]["mac_address"] == REDACTED
assert diagnostics["device"]["serial_number_prefix"] == "160150"
assert diagnostics["device"]["connected"] is False
assert diagnostics["setup_configurations"]
assert diagnostics["data_configurations"]