From 29b502bb0ac900aa4e7e986d9af044c0aecf3002 Mon Sep 17 00:00:00 2001 From: Klaas Schoute <klaas_schoute@hotmail.com> Date: Mon, 22 Aug 2022 22:18:17 +0200 Subject: [PATCH] Add diagnostics for Pure Energie integration (#77151) * Add diagnostics for Pure Energie integration * Update test after feedback --- .../components/pure_energie/diagnostics.py | 36 ++++++++++++++++ .../pure_energie/test_diagnostics.py | 41 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 homeassistant/components/pure_energie/diagnostics.py create mode 100644 tests/components/pure_energie/test_diagnostics.py diff --git a/homeassistant/components/pure_energie/diagnostics.py b/homeassistant/components/pure_energie/diagnostics.py new file mode 100644 index 00000000000..b2e071d58cd --- /dev/null +++ b/homeassistant/components/pure_energie/diagnostics.py @@ -0,0 +1,36 @@ +"""Diagnostics support for Pure Energie.""" +from __future__ import annotations + +from dataclasses import asdict +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_HOST +from homeassistant.core import HomeAssistant + +from . import PureEnergieDataUpdateCoordinator +from .const import DOMAIN + +TO_REDACT = { + CONF_HOST, + "n2g_id", +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator: PureEnergieDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + return { + "entry": { + "title": entry.title, + "data": async_redact_data(entry.data, TO_REDACT), + }, + "data": { + "device": async_redact_data(asdict(coordinator.data.device), TO_REDACT), + "smartbridge": asdict(coordinator.data.smartbridge), + }, + } diff --git a/tests/components/pure_energie/test_diagnostics.py b/tests/components/pure_energie/test_diagnostics.py new file mode 100644 index 00000000000..afb76724447 --- /dev/null +++ b/tests/components/pure_energie/test_diagnostics.py @@ -0,0 +1,41 @@ +"""Tests for the diagnostics data provided by the Pure Energie integration.""" +from aiohttp import ClientSession + +from homeassistant.components.diagnostics import REDACTED +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSession, + init_integration: MockConfigEntry, +) -> None: + """Test diagnostics.""" + assert await get_diagnostics_for_config_entry( + hass, hass_client, init_integration + ) == { + "entry": { + "title": "home", + "data": { + "host": REDACTED, + }, + }, + "data": { + "device": { + "batch": "SBP-HMX-210318", + "firmware": "1.6.16", + "hardware": 1, + "manufacturer": "NET2GRID", + "model": "SBWF3102", + "n2g_id": REDACTED, + }, + "smartbridge": { + "energy_consumption_total": 17762.1, + "energy_production_total": 21214.6, + "power_flow": 338, + }, + }, + } -- GitLab