diff --git a/homeassistant/components/weheat/diagnostics.py b/homeassistant/components/weheat/diagnostics.py new file mode 100644 index 000000000000..ea119c86bae2 --- /dev/null +++ b/homeassistant/components/weheat/diagnostics.py @@ -0,0 +1,30 @@ +"""Diagnostics support for Weheat.""" + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from .coordinator import WeheatConfigEntry + +TO_REDACT = {"heat_pump_id", "uuid", "sn"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: WeheatConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + return { + "heat_pumps": [ + { + "info": async_redact_data(vars(weheatdata.heat_pump_info), TO_REDACT), + "logs": async_redact_data( + weheatdata.data_coordinator.data.raw_content or {}, TO_REDACT + ), + "energy": async_redact_data( + weheatdata.energy_coordinator.data.raw_content or {}, TO_REDACT + ), + } + for weheatdata in entry.runtime_data + ] + } diff --git a/homeassistant/components/weheat/quality_scale.yaml b/homeassistant/components/weheat/quality_scale.yaml index 2d4e4b892543..ad4bfee02cbd 100644 --- a/homeassistant/components/weheat/quality_scale.yaml +++ b/homeassistant/components/weheat/quality_scale.yaml @@ -54,7 +54,7 @@ rules: # Gold devices: done - diagnostics: todo + diagnostics: done discovery-update-info: status: exempt comment: | diff --git a/tests/components/weheat/conftest.py b/tests/components/weheat/conftest.py index a7dc38f3c593..93aea72c3e5a 100644 --- a/tests/components/weheat/conftest.py +++ b/tests/components/weheat/conftest.py @@ -140,6 +140,11 @@ def mock_weheat_heat_pump_instance() -> MagicMock: mock_heat_pump_instance.indoor_unit_dhw_valve_or_pump_state = None mock_heat_pump_instance.indoor_unit_gas_boiler_state = False mock_heat_pump_instance.indoor_unit_electric_heater_state = True + mock_heat_pump_instance.raw_content = { + "heat_pump_id": TEST_HP_UUID, + "t_water_in": 11, + "total_ein_heating": 12345, + } return mock_heat_pump_instance diff --git a/tests/components/weheat/snapshots/test_diagnostics.ambr b/tests/components/weheat/snapshots/test_diagnostics.ambr new file mode 100644 index 000000000000..6db4e23f0e93 --- /dev/null +++ b/tests/components/weheat/snapshots/test_diagnostics.ambr @@ -0,0 +1,27 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'heat_pumps': list([ + dict({ + 'energy': dict({ + 'heat_pump_id': '**REDACTED**', + 't_water_in': 11, + 'total_ein_heating': 12345, + }), + 'info': dict({ + 'device_name': None, + 'has_ch_boiler': False, + 'has_dhw': True, + 'model': 'Test Model', + 'sn': '**REDACTED**', + 'uuid': '**REDACTED**', + }), + 'logs': dict({ + 'heat_pump_id': '**REDACTED**', + 't_water_in': 11, + 'total_ein_heating': 12345, + }), + }), + ]), + }) +# --- diff --git a/tests/components/weheat/test_diagnostics.py b/tests/components/weheat/test_diagnostics.py new file mode 100644 index 000000000000..ed4868af9738 --- /dev/null +++ b/tests/components/weheat/test_diagnostics.py @@ -0,0 +1,50 @@ +"""Tests for the weheat diagnostics.""" + +from unittest.mock import AsyncMock + +import pytest +from syrupy.assertion 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 + + +@pytest.mark.usefixtures("mock_weheat_discover", "mock_weheat_heat_pump") +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test the diagnostics of a config entry.""" + await setup_integration(hass, mock_config_entry) + + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry) + == snapshot + ) + + +@pytest.mark.usefixtures("mock_weheat_discover") +async def test_diagnostics_without_data( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + mock_weheat_heat_pump: AsyncMock, +) -> None: + """Test the diagnostics of a heat pump that reported nothing yet.""" + mock_weheat_heat_pump.raw_content = None + + await setup_integration(hass, mock_config_entry) + + diagnostics = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + + assert diagnostics["heat_pumps"][0]["logs"] == {} + assert diagnostics["heat_pumps"][0]["energy"] == {}