From fe2c11a698d6cf97d7ec9cf3f3a6e99c3be4ab65 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:29:28 +0200 Subject: [PATCH] Add diagnostics to PI-Hole (#91383) --- .../components/pi_hole/diagnostics.py | 28 +++++++++++ .../pi_hole/snapshots/test_diagnostics.ambr | 49 +++++++++++++++++++ tests/components/pi_hole/test_diagnostics.py | 31 ++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 homeassistant/components/pi_hole/diagnostics.py create mode 100644 tests/components/pi_hole/snapshots/test_diagnostics.ambr create mode 100644 tests/components/pi_hole/test_diagnostics.py diff --git a/homeassistant/components/pi_hole/diagnostics.py b/homeassistant/components/pi_hole/diagnostics.py new file mode 100644 index 00000000000..8b3c32b0ac2 --- /dev/null +++ b/homeassistant/components/pi_hole/diagnostics.py @@ -0,0 +1,28 @@ +"""Diagnostics support for the Pi-hole integration.""" +from __future__ import annotations + +from typing import Any + +from hole import Hole + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_API_KEY +from homeassistant.core import HomeAssistant + +from .const import DATA_KEY_API, DOMAIN + +TO_REDACT = {CONF_API_KEY} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + api: Hole = hass.data[DOMAIN][entry.entry_id][DATA_KEY_API] + + return { + "entry": async_redact_data(entry.as_dict(), TO_REDACT), + "data": api.data, + "versions": api.versions, + } diff --git a/tests/components/pi_hole/snapshots/test_diagnostics.ambr b/tests/components/pi_hole/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..69c77acc64a --- /dev/null +++ b/tests/components/pi_hole/snapshots/test_diagnostics.ambr @@ -0,0 +1,49 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'data': dict({ + 'ads_blocked_today': 0, + 'ads_percentage_today': 0, + 'clients_ever_seen': 0, + 'dns_queries_today': 0, + 'domains_being_blocked': 0, + 'queries_cached': 0, + 'queries_forwarded': 0, + 'status': 'disabled', + 'unique_clients': 0, + 'unique_domains': 0, + }), + 'entry': dict({ + 'data': dict({ + 'api_key': '**REDACTED**', + 'host': '1.2.3.4:80', + 'location': 'admin', + 'name': 'Pi-Hole', + 'ssl': False, + 'verify_ssl': True, + }), + 'disabled_by': None, + 'domain': 'pi_hole', + 'entry_id': 'pi_hole_mock_entry', + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'Mock Title', + 'unique_id': None, + 'version': 1, + }), + 'versions': dict({ + 'FTL_current': 'v5.10', + 'FTL_latest': 'v5.11', + 'FTL_update': True, + 'core_current': 'v5.5', + 'core_latest': 'v5.6', + 'core_update': True, + 'web_current': 'v5.7', + 'web_latest': 'v5.8', + 'web_update': True, + }), + }) +# --- diff --git a/tests/components/pi_hole/test_diagnostics.py b/tests/components/pi_hole/test_diagnostics.py new file mode 100644 index 00000000000..c9fc9a0a9b8 --- /dev/null +++ b/tests/components/pi_hole/test_diagnostics.py @@ -0,0 +1,31 @@ +"""Test pi_hole component.""" + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components import pi_hole +from homeassistant.core import HomeAssistant + +from . import CONFIG_DATA_DEFAULTS, _create_mocked_hole, _patch_init_hole + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Tests diagnostics.""" + mocked_hole = _create_mocked_hole() + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS, entry_id="pi_hole_mock_entry" + ) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + await hass.async_block_till_done() + + assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot