Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions homeassistant/components/vicare/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
from typing import Any

from PyViCare.PyViCareUtils import PyViCareDeviceCommunicationError

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.const import (
CONF_ACCESS_TOKEN,
Expand Down Expand Up @@ -30,11 +32,26 @@ async def async_get_config_entry_diagnostics(
"""Return diagnostics for a config entry."""

def dump_devices() -> list[dict[str, Any]]:
"""Dump devices."""
return [
json.loads(device.dump_secure())
for device in entry.runtime_data.client.all_devices
]
"""Dump devices, tolerating per-device communication failures."""
devices: list[dict[str, Any]] = []
for device in entry.runtime_data.client.all_devices:
try:
devices.append(json.loads(device.dump_secure()))
except PyViCareDeviceCommunicationError as err:
# One offline gateway must not abort the whole diagnostics dump.
devices.append(
{
"device": {
"id": device.device_id,
"modelId": device.device_model,
"type": device.device_type,
"status": device.status,
"roles": device.roles,
},
"error": f"{type(err).__name__}: {err}",
}
)
return devices

return {
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
Expand Down
28 changes: 28 additions & 0 deletions tests/components/vicare/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from unittest.mock import MagicMock

from PyViCare.PyViCareUtils import PyViCareDeviceCommunicationError
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props

Expand All @@ -23,3 +24,30 @@ async def test_diagnostics(
)

assert diag == snapshot(exclude=props("created_at", "modified_at"))


async def test_diagnostics_with_offline_device(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_vicare_gas_boiler: MagicMock,
) -> None:
"""Test that an offline gateway on one device does not abort diagnostics."""
config_entry = hass.config_entries.async_entries("vicare")[0]
devices = config_entry.runtime_data.client.all_devices

# Force the first device to fail with GATEWAY_OFFLINE; the rest must still dump.
devices[0].dump_secure = MagicMock(
side_effect=PyViCareDeviceCommunicationError(
{"extendedPayload": {"reason": "GATEWAY_OFFLINE"}}
)
)

diag = await get_diagnostics_for_config_entry(
hass, hass_client, mock_vicare_gas_boiler
)

assert len(diag["data"]) == len(devices)
error_entry = diag["data"][0]
assert "error" in error_entry
assert "GATEWAY_OFFLINE" in error_entry["error"]
assert error_entry["device"]["id"] == devices[0].device_id
Loading