From d5e5a1630361e7d4e6faddb6bce111c4620721ad Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Thu, 18 Apr 2024 14:08:23 +0200 Subject: [PATCH] Add diagnostics platform to DSMR Reader (#115805) * Add diagnostics platform * Feedback --------- Co-authored-by: Joost Lekkerkerker --- .../components/dsmr_reader/diagnostics.py | 27 +++++++++++++ .../snapshots/test_diagnostics.ambr | 23 +++++++++++ .../dsmr_reader/test_diagnostics.py | 39 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 homeassistant/components/dsmr_reader/diagnostics.py create mode 100644 tests/components/dsmr_reader/snapshots/test_diagnostics.ambr create mode 100644 tests/components/dsmr_reader/test_diagnostics.py diff --git a/homeassistant/components/dsmr_reader/diagnostics.py b/homeassistant/components/dsmr_reader/diagnostics.py new file mode 100644 index 00000000000..554d90cc5dd --- /dev/null +++ b/homeassistant/components/dsmr_reader/diagnostics.py @@ -0,0 +1,27 @@ +"""Diagnostics support for DSMR Reader.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for the config entry.""" + ent_reg = er.async_get(hass) + entities = [ + entity.entity_id + for entity in er.async_entries_for_config_entry(ent_reg, entry.entry_id) + ] + + entity_states = {entity: hass.states.get(entity) for entity in entities} + + return { + "entry": entry.as_dict(), + "entities": entity_states, + } diff --git a/tests/components/dsmr_reader/snapshots/test_diagnostics.ambr b/tests/components/dsmr_reader/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..c6bc616ffd3 --- /dev/null +++ b/tests/components/dsmr_reader/snapshots/test_diagnostics.ambr @@ -0,0 +1,23 @@ +# serializer version: 1 +# name: test_get_config_entry_diagnostics + dict({ + 'entities': dict({ + }), + 'entry': dict({ + 'data': dict({ + }), + 'disabled_by': None, + 'domain': 'dsmr_reader', + 'entry_id': 'TEST_ENTRY_ID', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'dsmr_reader', + 'unique_id': 'UNIQUE_TEST_ID', + 'version': 1, + }), + }) +# --- diff --git a/tests/components/dsmr_reader/test_diagnostics.py b/tests/components/dsmr_reader/test_diagnostics.py new file mode 100644 index 00000000000..553efd0b38b --- /dev/null +++ b/tests/components/dsmr_reader/test_diagnostics.py @@ -0,0 +1,39 @@ +"""Test the DSMR Reader component diagnostics.""" + +from unittest.mock import patch + +from syrupy import SnapshotAssertion + +from homeassistant.components.dsmr_reader.const import DOMAIN +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_get_config_entry_diagnostics( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + hass_client: ClientSessionGenerator, +) -> None: + """Test if get_config_entry_diagnostics returns the correct data.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + title=DOMAIN, + options={}, + entry_id="TEST_ENTRY_ID", + unique_id="UNIQUE_TEST_ID", + ) + config_entry.add_to_hass(hass) + + with patch( + "homeassistant.components.dsmr_reader.async_setup_entry", return_value=True + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + diagnostics = await get_diagnostics_for_config_entry( + hass, hass_client, config_entry + ) + assert diagnostics == snapshot