Add diagnostics for nws (#117587)

* add diagnostics

* remove hassfezt exception
pull/117593/head
MatthewFlamm 2024-05-16 15:26:22 -04:00 committed by GitHub
parent 5635bcce86
commit 9aa7d3057b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 153 additions and 1 deletions

View File

@ -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,
}

View File

@ -124,7 +124,6 @@ NO_DIAGNOSTICS = [
"hyperion",
"modbus",
"nightscout",
"nws",
"point",
"pvpc_hourly_pricing",
"risco",

View File

@ -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,
}),
})
# ---

View File

@ -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