From 78e66b6cbe2ce34f8cc77c5ada50947957e615ee Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Tue, 18 Oct 2022 10:25:07 -0600 Subject: [PATCH] Add diagnostics to Enphase Envoy (#79950) * Streamline Enphase Envoy config flow tests * Don't test data results using constants * Add diagnostics to Enphase Envoy * Use whole config entry * Redact serial number * One call --- .../components/enphase_envoy/diagnostics.py | 38 +++++++++++++ .../enphase_envoy/test_diagnostics.py | 56 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 homeassistant/components/enphase_envoy/diagnostics.py create mode 100644 tests/components/enphase_envoy/test_diagnostics.py diff --git a/homeassistant/components/enphase_envoy/diagnostics.py b/homeassistant/components/enphase_envoy/diagnostics.py new file mode 100644 index 00000000000..daba57e9488 --- /dev/null +++ b/homeassistant/components/enphase_envoy/diagnostics.py @@ -0,0 +1,38 @@ +"""Diagnostics support for Enphase Envoy.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_UNIQUE_ID, CONF_USERNAME +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import COORDINATOR, DOMAIN + +CONF_TITLE = "title" + +TO_REDACT = { + CONF_NAME, + CONF_PASSWORD, + # Config entry title and unique ID may contain sensitive data: + CONF_TITLE, + CONF_UNIQUE_ID, + CONF_USERNAME, +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR] + + return async_redact_data( + { + "entry": entry.as_dict(), + "data": coordinator.data, + }, + TO_REDACT, + ) diff --git a/tests/components/enphase_envoy/test_diagnostics.py b/tests/components/enphase_envoy/test_diagnostics.py new file mode 100644 index 00000000000..caa7d66fc95 --- /dev/null +++ b/tests/components/enphase_envoy/test_diagnostics.py @@ -0,0 +1,56 @@ +"""Test Enphase Envoy diagnostics.""" +from homeassistant.components.diagnostics import REDACTED + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_entry_diagnostics(hass, config_entry, hass_client, setup_enphase_envoy): + """Test config entry diagnostics.""" + assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { + "entry": { + "entry_id": config_entry.entry_id, + "version": 1, + "domain": "enphase_envoy", + "title": REDACTED, + "data": { + "host": "1.1.1.1", + "name": REDACTED, + "username": REDACTED, + "password": REDACTED, + }, + "options": {}, + "pref_disable_new_entities": False, + "pref_disable_polling": False, + "source": "user", + "unique_id": REDACTED, + "disabled_by": None, + }, + "data": { + "production": 1840, + "daily_production": 28223, + "seven_days_production": 174482, + "lifetime_production": 5924391, + "consumption": 1840, + "daily_consumption": 5923857, + "seven_days_consumption": 5923857, + "lifetime_consumption": 5923857, + "inverters_production": { + "202140024014": [136, "2022-10-08 16:43:36"], + "202140023294": [163, "2022-10-08 16:43:41"], + "202140013819": [130, "2022-10-08 16:43:31"], + "202140023794": [139, "2022-10-08 16:43:38"], + "202140023381": [130, "2022-10-08 16:43:47"], + "202140024176": [54, "2022-10-08 16:43:59"], + "202140003284": [132, "2022-10-08 16:43:55"], + "202140019854": [129, "2022-10-08 16:43:58"], + "202140020743": [131, "2022-10-08 16:43:49"], + "202140023531": [28, "2022-10-08 16:43:53"], + "202140024241": [164, "2022-10-08 16:43:33"], + "202140022963": [164, "2022-10-08 16:43:41"], + "202140023149": [118, "2022-10-08 16:43:47"], + "202140024828": [129, "2022-10-08 16:43:36"], + "202140023269": [133, "2022-10-08 16:43:43"], + "202140024157": [112, "2022-10-08 16:43:52"], + }, + }, + }