Add diagnostics to Tomorrow.io (#193445)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Joost Lekkerkerker
2026-11-19 17:05:58 +00:00
committed by GitHub
co-authored by Claude
parent 716ad2049f
commit 2f2b0699ff
3 changed files with 275 additions and 0 deletions
@@ -0,0 +1,60 @@
"""Diagnostics support for Tomorrow.io."""
from typing import Any
from pytomorrowio.const import CURRENT, DAILY, FORECASTS, HOURLY, NOWCAST
from homeassistant.core import HomeAssistant
from .const import CONF_TIMESTEP, SUBENTRY_TYPE_LOCATION
from .coordinator import TomorrowioConfigEntry
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: TomorrowioConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator = entry.runtime_data
# The API key, the coordinates and the names of the locations are left
# out. Locations are emitted as a list instead of a dict keyed by subentry
# ID, and the forecasts are summarized instead of dumped, as they hold
# hundreds of entries per location.
locations: list[dict[str, Any]] = []
for subentry in entry.get_subentries_of_type(SUBENTRY_TYPE_LOCATION):
data: dict[str, Any] = coordinator.data.get(subentry.subentry_id) or {}
current: dict[str, Any] = data.get(CURRENT) or {}
forecasts: dict[str, Any] = data.get(FORECASTS) or {}
locations.append(
{
"timestep": subentry.data[CONF_TIMESTEP],
"current_fields": sorted(
key for key, value in current.items() if value is not None
),
"forecasts": {
forecast_type: _summarize_forecast(forecasts.get(forecast_type))
for forecast_type in (NOWCAST, HOURLY, DAILY)
},
}
)
return {
"last_update_success": coordinator.last_update_success,
"update_interval": (
coordinator.update_interval.total_seconds()
if coordinator.update_interval is not None
else None
),
"locations": locations,
}
def _summarize_forecast(forecast: list[dict[str, Any]] | None) -> dict[str, Any]:
"""Summarize a forecast to its size and time range."""
if not forecast:
return {"count": 0}
return {
"count": len(forecast),
"first_start_time": forecast[0].get("startTime"),
"last_start_time": forecast[-1].get("startTime"),
}
@@ -0,0 +1,161 @@
# serializer version: 1
# name: test_diagnostics[empty]
dict({
'last_update_success': True,
'locations': list([
dict({
'current_fields': list([
]),
'forecasts': dict({
'daily': dict({
'count': 0,
}),
'hourly': dict({
'count': 0,
}),
'nowcast': dict({
'count': 0,
}),
}),
'timestep': 60,
}),
dict({
'current_fields': list([
]),
'forecasts': dict({
'daily': dict({
'count': 0,
}),
'hourly': dict({
'count': 0,
}),
'nowcast': dict({
'count': 0,
}),
}),
'timestep': 5,
}),
]),
'update_interval': 3840.0,
})
# ---
# name: test_diagnostics[full]
dict({
'last_update_success': True,
'locations': list([
dict({
'current_fields': list([
'cloudBase',
'cloudCeiling',
'cloudCover',
'dewPoint',
'epaHealthConcern',
'epaIndex',
'epaPrimaryPollutant',
'fireIndex',
'grassIndex',
'humidity',
'mepHealthConcern',
'mepIndex',
'mepPrimaryPollutant',
'particulateMatter10',
'particulateMatter25',
'pollutantCO',
'pollutantNO2',
'pollutantO3',
'pollutantSO2',
'precipitationType',
'pressureSeaLevel',
'pressureSurfaceLevel',
'solarGHI',
'temperature',
'temperatureApparent',
'treeIndex',
'uvHealthConcern',
'uvIndex',
'visibility',
'weatherCode',
'weedIndex',
'windDirection',
'windGust',
'windSpeed',
]),
'forecasts': dict({
'daily': dict({
'count': 15,
'first_start_time': '2021-03-07T11:00:00Z',
'last_start_time': '2021-03-21T10:00:00Z',
}),
'hourly': dict({
'count': 108,
'first_start_time': '2021-03-07T17:48:00Z',
'last_start_time': '2021-03-12T04:48:00Z',
}),
'nowcast': dict({
'count': 72,
'first_start_time': '2021-03-07T17:48:00Z',
'last_start_time': '2021-03-07T23:43:00Z',
}),
}),
'timestep': 60,
}),
dict({
'current_fields': list([
'cloudBase',
'cloudCeiling',
'cloudCover',
'dewPoint',
'epaHealthConcern',
'epaIndex',
'epaPrimaryPollutant',
'fireIndex',
'grassIndex',
'humidity',
'mepHealthConcern',
'mepIndex',
'mepPrimaryPollutant',
'particulateMatter10',
'particulateMatter25',
'pollutantCO',
'pollutantNO2',
'pollutantO3',
'pollutantSO2',
'precipitationType',
'pressureSeaLevel',
'pressureSurfaceLevel',
'solarGHI',
'temperature',
'temperatureApparent',
'treeIndex',
'uvHealthConcern',
'uvIndex',
'visibility',
'weatherCode',
'weedIndex',
'windDirection',
'windGust',
'windSpeed',
]),
'forecasts': dict({
'daily': dict({
'count': 15,
'first_start_time': '2021-03-07T11:00:00Z',
'last_start_time': '2021-03-21T10:00:00Z',
}),
'hourly': dict({
'count': 108,
'first_start_time': '2021-03-07T17:48:00Z',
'last_start_time': '2021-03-12T04:48:00Z',
}),
'nowcast': dict({
'count': 72,
'first_start_time': '2021-03-07T17:48:00Z',
'last_start_time': '2021-03-07T23:43:00Z',
}),
}),
'timestep': 5,
}),
]),
'update_interval': 3840.0,
})
# ---
@@ -0,0 +1,54 @@
"""Tests for the Tomorrow.io diagnostics."""
from typing import Any
from unittest.mock import AsyncMock
import pytest
from pytest_homeassistant.common import load_json_object_fixture
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
from pytest_homeassistant.typing import ClientSessionGenerator
from syrupy.assertion import SnapshotAssertion
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from . import make_location_subentry_data, make_v2_config_entry
@pytest.mark.parametrize(
"api_response",
[
pytest.param(load_json_object_fixture("v4.json", "tomorrowio"), id="full"),
pytest.param({"current": {}, "forecasts": {}}, id="empty"),
],
)
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
tomorrowio_config_entry_update: AsyncMock,
snapshot: SnapshotAssertion,
api_response: dict[str, Any],
) -> None:
"""Test diagnostics."""
tomorrowio_config_entry_update.return_value = api_response
config_entry = make_v2_config_entry(
subentries_data=[
make_location_subentry_data(),
make_location_subentry_data(
location={"latitude": 52.37, "longitude": 4.89},
name="My secret home",
timestep=5,
subentry_id="01JZZZZZZZZZZZZZZZZZZZZZZZ",
),
]
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
assert (
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
== snapshot
)