From bdff418c829b0708f307276454805a554f64e646 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 28 Oct 2026 11:32:43 +0100 Subject: [PATCH] Add diagnostics to OpenWeatherMap (#189154) Co-authored-by: Claude --- .../components/openweathermap/diagnostics.py | 21 +++++ .../snapshots/test_diagnostics.ambr | 93 +++++++++++++++++++ .../openweathermap/test_diagnostics.py | 32 +++++++ 3 files changed, 146 insertions(+) create mode 100644 homeassistant/components/openweathermap/diagnostics.py create mode 100644 tests/components/openweathermap/snapshots/test_diagnostics.ambr create mode 100644 tests/components/openweathermap/test_diagnostics.py diff --git a/homeassistant/components/openweathermap/diagnostics.py b/homeassistant/components/openweathermap/diagnostics.py new file mode 100644 index 000000000000..1111e392d827 --- /dev/null +++ b/homeassistant/components/openweathermap/diagnostics.py @@ -0,0 +1,21 @@ +"""Diagnostics support for OpenWeatherMap.""" + +from typing import Any + +from homeassistant.const import CONF_LANGUAGE, CONF_MODE +from homeassistant.core import HomeAssistant + +from . import OpenweathermapConfigEntry + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: OpenweathermapConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator = entry.runtime_data.coordinator + return { + "mode": entry.options[CONF_MODE], + "language": entry.options[CONF_LANGUAGE], + "last_update_success": coordinator.last_update_success, + "data": coordinator.data, + } diff --git a/tests/components/openweathermap/snapshots/test_diagnostics.ambr b/tests/components/openweathermap/snapshots/test_diagnostics.ambr new file mode 100644 index 000000000000..a4bd78990fe5 --- /dev/null +++ b/tests/components/openweathermap/snapshots/test_diagnostics.ambr @@ -0,0 +1,93 @@ +# serializer version: 1 +# name: test_diagnostics[air_pollution] + dict({ + 'data': dict({ + 'current': dict({ + 'aqi': 3, + 'co': 125.55, + 'nh3': 4.62, + 'no': 0.11, + 'no2': 0.78, + 'o3': 101.98, + 'pm10': 4.77, + 'pm2_5': 4.48, + 'so2': 0.59, + }), + }), + 'language': 'en', + 'last_update_success': True, + 'mode': 'air_pollution', + }) +# --- +# name: test_diagnostics[v3.0] + dict({ + 'data': dict({ + 'current': dict({ + 'clouds': 75, + 'condition': 'cloudy', + 'dew_point': 3.99, + 'feels_like_temperature': 2.07, + 'humidity': 82, + 'precipitation_kind': 'Rain', + 'pressure': 1000, + 'rain': 1.21, + 'snow': 0, + 'temperature': 6.84, + 'uv_index': 0.13, + 'visibility_distance': 10000, + 'weather': 'broken clouds', + 'weather_code': 803, + 'wind_bearing': 199, + 'wind_gust': 11.81, + 'wind_speed': 9.83, + }), + 'daily_forecast': list([ + dict({ + 'cloud_coverage': 84, + 'condition': 'cloudy', + 'datetime': '2024-04-25T16:45:36+00:00', + 'humidity': 62, + 'native_apparent_temperature': dict({ + '__type': "", + 'repr': 'DailyTemperature(day=18.76, min=8.11, max=21.26, night=13.06, evening=20.51, morning=8.47)', + }), + 'native_dew_point': 11.34, + 'native_wind_gust_speed': 11.81, + 'precipitation': 0, + 'precipitation_probability': 0, + 'pressure': 1015, + 'temperature': 21.26, + 'templow': 8.11, + 'uv_index': 4.06, + 'wind_bearing': 168, + 'wind_speed': 8.14, + }), + ]), + 'hourly_forecast': list([ + ]), + 'minute_forecast': dict({ + 'forecast': list([ + dict({ + 'datetime': 1728672360, + 'precipitation': 0, + }), + dict({ + 'datetime': 1728672420, + 'precipitation': 1.23, + }), + dict({ + 'datetime': 1728672480, + 'precipitation': 4.5, + }), + dict({ + 'datetime': 1728672540, + 'precipitation': 0, + }), + ]), + }), + }), + 'language': 'en', + 'last_update_success': True, + 'mode': 'v3.0', + }) +# --- diff --git a/tests/components/openweathermap/test_diagnostics.py b/tests/components/openweathermap/test_diagnostics.py new file mode 100644 index 000000000000..ab4dfc38169e --- /dev/null +++ b/tests/components/openweathermap/test_diagnostics.py @@ -0,0 +1,32 @@ +"""Tests for the diagnostics data provided by the OpenWeatherMap integration.""" + +import pytest +from pytest_homeassistant.common import MockConfigEntry +from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry +from pytest_homeassistant.typing import ClientSessionGenerator +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.openweathermap.const import ( + OWM_MODE_AIRPOLLUTION, + OWM_MODE_V30, +) +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from . import setup_platform + + +@pytest.mark.parametrize("mode", [OWM_MODE_V30, OWM_MODE_AIRPOLLUTION], indirect=True) +@pytest.mark.usefixtures("owm_client_mock") +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics.""" + await setup_platform(hass, mock_config_entry, [Platform.WEATHER]) + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry) + == snapshot + )