From 2be578a33fe499abc8f66694635547c4ad0eb69d Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 1 Jan 2025 12:32:35 +0100 Subject: [PATCH] Add diagnostics to Trafikverket Weatherstation (#134314) --- .../diagnostics.py | 17 ++++ .../trafikverket_weatherstation/__init__.py | 8 ++ .../trafikverket_weatherstation/conftest.py | 80 +++++++++++++++++++ .../snapshots/test_diagnostics.ambr | 29 +++++++ .../test_diagnostics.py | 23 ++++++ 5 files changed, 157 insertions(+) create mode 100644 homeassistant/components/trafikverket_weatherstation/diagnostics.py create mode 100644 tests/components/trafikverket_weatherstation/conftest.py create mode 100644 tests/components/trafikverket_weatherstation/snapshots/test_diagnostics.ambr create mode 100644 tests/components/trafikverket_weatherstation/test_diagnostics.py diff --git a/homeassistant/components/trafikverket_weatherstation/diagnostics.py b/homeassistant/components/trafikverket_weatherstation/diagnostics.py new file mode 100644 index 00000000000..e70d60493f6 --- /dev/null +++ b/homeassistant/components/trafikverket_weatherstation/diagnostics.py @@ -0,0 +1,17 @@ +"""Diagnostics support for Trafikverket Weatherstation.""" + +from __future__ import annotations + +from dataclasses import asdict +from typing import Any + +from homeassistant.core import HomeAssistant + +from . import TVWeatherConfigEntry + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: TVWeatherConfigEntry +) -> dict[str, Any]: + """Return diagnostics for Trafikverket Weatherstation config entry.""" + return asdict(entry.runtime_data.data) diff --git a/tests/components/trafikverket_weatherstation/__init__.py b/tests/components/trafikverket_weatherstation/__init__.py index 836ee919195..4639211ed31 100644 --- a/tests/components/trafikverket_weatherstation/__init__.py +++ b/tests/components/trafikverket_weatherstation/__init__.py @@ -1 +1,9 @@ """Tests for the Trafikverket weatherstation integration.""" + +from homeassistant.components.trafikverket_weatherstation.const import CONF_STATION +from homeassistant.const import CONF_API_KEY + +ENTRY_CONFIG = { + CONF_API_KEY: "1234567890", + CONF_STATION: "Arlanda", +} diff --git a/tests/components/trafikverket_weatherstation/conftest.py b/tests/components/trafikverket_weatherstation/conftest.py new file mode 100644 index 00000000000..bd31c31c86b --- /dev/null +++ b/tests/components/trafikverket_weatherstation/conftest.py @@ -0,0 +1,80 @@ +"""Fixtures for the Trafikverket Weatherstation integration.""" + +from __future__ import annotations + +from collections.abc import AsyncGenerator +from datetime import datetime +from unittest.mock import patch + +import pytest +from pytrafikverket import WeatherStationInfoModel + +from homeassistant.components.trafikverket_weatherstation.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.core import HomeAssistant + +from . import ENTRY_CONFIG + +from tests.common import MockConfigEntry + + +@pytest.fixture +async def load_int( + hass: HomeAssistant, mock_response: WeatherStationInfoModel +) -> MockConfigEntry: + """Set up the Trafikverket Weatherstation integration in Home Assistant.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + source=SOURCE_USER, + data=ENTRY_CONFIG, + ) + + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + return config_entry + + +@pytest.fixture(name="mock_response") +async def mock_weather_response( + get_data: WeatherStationInfoModel, +) -> AsyncGenerator[None]: + """Mock a successful response.""" + with patch( + "homeassistant.components.trafikverket_weatherstation.coordinator.TrafikverketWeather.async_get_weather", + return_value=get_data, + ): + yield + + +@pytest.fixture(name="get_data") +async def get_data_from_library(hass: HomeAssistant) -> WeatherStationInfoModel: + """Retrieve data from Trafikverket Weatherstation library.""" + return WeatherStationInfoModel( + station_name="Arlanda", + station_id="227", + road_temp=-3.4, + air_temp=-3.0, + dew_point=-5.0, + humidity=84.1, + visible_distance=20000.0, + precipitationtype="no", + raining=False, + snowing=False, + road_ice=None, + road_ice_depth=None, + road_snow=None, + road_snow_depth=None, + road_water=None, + road_water_depth=None, + road_water_equivalent_depth=None, + winddirection="202", + wind_height=6.0, + windforce=1.2, + windforcemax=2.3, + measure_time=datetime.fromisoformat("2024-12-30T23:00:03+01:00"), + precipitation_amount=0.0, + modified_time=datetime.fromisoformat("2024-12-30T22:03:45.143000+00:00"), + ) diff --git a/tests/components/trafikverket_weatherstation/snapshots/test_diagnostics.ambr b/tests/components/trafikverket_weatherstation/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..95caa014d45 --- /dev/null +++ b/tests/components/trafikverket_weatherstation/snapshots/test_diagnostics.ambr @@ -0,0 +1,29 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'air_temp': -3.0, + 'dew_point': -5.0, + 'humidity': 84.1, + 'measure_time': '2024-12-30T23:00:03+01:00', + 'modified_time': '2024-12-30T22:03:45.143000+00:00', + 'precipitation_amount': 0.0, + 'precipitationtype': 'no', + 'raining': False, + 'road_ice': None, + 'road_ice_depth': None, + 'road_snow': None, + 'road_snow_depth': None, + 'road_temp': -3.4, + 'road_water': None, + 'road_water_depth': None, + 'road_water_equivalent_depth': None, + 'snowing': False, + 'station_id': '227', + 'station_name': 'Arlanda', + 'visible_distance': 20000.0, + 'wind_height': 6.0, + 'winddirection': '202', + 'windforce': 1.2, + 'windforcemax': 2.3, + }) +# --- diff --git a/tests/components/trafikverket_weatherstation/test_diagnostics.py b/tests/components/trafikverket_weatherstation/test_diagnostics.py new file mode 100644 index 00000000000..29b7b906dd0 --- /dev/null +++ b/tests/components/trafikverket_weatherstation/test_diagnostics.py @@ -0,0 +1,23 @@ +"""Test Trafikverket Weatherstation diagnostics.""" + +from __future__ import annotations + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + load_int: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test generating diagnostics for a config entry.""" + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, load_int) == snapshot + )