From 9aa7d3057b03c9a309fcb8e96a5a70a9808eb8de Mon Sep 17 00:00:00 2001 From: MatthewFlamm <39341281+MatthewFlamm@users.noreply.github.com> Date: Thu, 16 May 2024 15:26:22 -0400 Subject: [PATCH] Add diagnostics for nws (#117587) * add diagnostics * remove hassfezt exception --- homeassistant/components/nws/diagnostics.py | 32 +++++++ script/hassfest/manifest.py | 1 - .../nws/snapshots/test_diagnostics.ambr | 88 +++++++++++++++++++ tests/components/nws/test_diagnostics.py | 33 +++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/nws/diagnostics.py create mode 100644 tests/components/nws/snapshots/test_diagnostics.ambr create mode 100644 tests/components/nws/test_diagnostics.py diff --git a/homeassistant/components/nws/diagnostics.py b/homeassistant/components/nws/diagnostics.py new file mode 100644 index 00000000000..2ac0b2ef488 --- /dev/null +++ b/homeassistant/components/nws/diagnostics.py @@ -0,0 +1,32 @@ +"""Diagnostics support for NWS.""" + +from __future__ import annotations + +from typing import Any + +from pynws import SimpleNWS + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant + +from .const import CONF_STATION, DOMAIN + +CONFIG_TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_STATION} +OBSERVATION_TO_REDACT = {"station"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, + config_entry: ConfigEntry, +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + nws_data: SimpleNWS = hass.data[DOMAIN][config_entry.entry_id].api + + return { + "info": async_redact_data(config_entry.data, CONFIG_TO_REDACT), + "observation": async_redact_data(nws_data.observation, OBSERVATION_TO_REDACT), + "forecast": nws_data.forecast, + "forecast_hourly": nws_data.forecast_hourly, + } diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index 53baf0d4a17..2796b4d2eb2 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -124,7 +124,6 @@ NO_DIAGNOSTICS = [ "hyperion", "modbus", "nightscout", - "nws", "point", "pvpc_hourly_pricing", "risco", diff --git a/tests/components/nws/snapshots/test_diagnostics.ambr b/tests/components/nws/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..2db73f90054 --- /dev/null +++ b/tests/components/nws/snapshots/test_diagnostics.ambr @@ -0,0 +1,88 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'forecast': list([ + dict({ + 'detailedForecast': 'A detailed forecast.', + 'dewpoint': 4, + 'iconTime': 'night', + 'iconWeather': list([ + list([ + 'lightning-rainy', + 40, + ]), + list([ + 'lightning-rainy', + 90, + ]), + ]), + 'isDaytime': False, + 'name': 'Tonight', + 'number': 1, + 'probabilityOfPrecipitation': 89, + 'relativeHumidity': 75, + 'startTime': '2019-08-12T20:00:00-04:00', + 'temperature': 10, + 'timestamp': '2019-08-12T23:53:00+00:00', + 'windBearing': 180, + 'windSpeedAvg': 10, + }), + ]), + 'forecast_hourly': list([ + dict({ + 'detailedForecast': 'A detailed forecast.', + 'dewpoint': 4, + 'iconTime': 'night', + 'iconWeather': list([ + list([ + 'lightning-rainy', + 40, + ]), + list([ + 'lightning-rainy', + 90, + ]), + ]), + 'isDaytime': False, + 'name': 'Tonight', + 'number': 1, + 'probabilityOfPrecipitation': 89, + 'relativeHumidity': 75, + 'startTime': '2019-08-12T20:00:00-04:00', + 'temperature': 10, + 'timestamp': '2019-08-12T23:53:00+00:00', + 'windBearing': 180, + 'windSpeedAvg': 10, + }), + ]), + 'info': dict({ + 'api_key': '**REDACTED**', + 'latitude': '**REDACTED**', + 'longitude': '**REDACTED**', + 'station': '**REDACTED**', + }), + 'observation': dict({ + 'barometricPressure': 100000, + 'dewpoint': 5, + 'heatIndex': 15, + 'iconTime': 'day', + 'iconWeather': list([ + list([ + 'Fair/clear', + None, + ]), + ]), + 'relativeHumidity': 10, + 'seaLevelPressure': 100000, + 'station': '**REDACTED**', + 'temperature': 10, + 'textDescription': 'A long description', + 'timestamp': '2019-08-12T23:53:00+00:00', + 'visibility': 10000, + 'windChill': 5, + 'windDirection': 180, + 'windGust': 20, + 'windSpeed': 10, + }), + }) +# --- diff --git a/tests/components/nws/test_diagnostics.py b/tests/components/nws/test_diagnostics.py new file mode 100644 index 00000000000..55f7f3100a0 --- /dev/null +++ b/tests/components/nws/test_diagnostics.py @@ -0,0 +1,33 @@ +"""Test NWS diagnostics.""" + +from syrupy import SnapshotAssertion + +from homeassistant.components import nws +from homeassistant.core import HomeAssistant + +from .const import NWS_CONFIG + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, + mock_simple_nws, +) -> None: + """Test config entry diagnostics.""" + + entry = MockConfigEntry( + domain=nws.DOMAIN, + data=NWS_CONFIG, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result == snapshot