From 669dd36daf57169c04dad5171ceb7d3201c1e9f4 Mon Sep 17 00:00:00 2001 From: Diogo Gomes Date: Wed, 13 Mar 2024 11:07:28 +0000 Subject: [PATCH] Add diagnostics platform to IPMA (#105697) --- homeassistant/components/ipma/diagnostics.py | 32 ++++++++++ tests/components/ipma/conftest.py | 38 +++++++----- .../ipma/snapshots/test_diagnostics.ambr | 60 +++++++++++++++++++ tests/components/ipma/test_config_flow.py | 2 +- tests/components/ipma/test_diagnostics.py | 22 +++++++ 5 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 homeassistant/components/ipma/diagnostics.py create mode 100644 tests/components/ipma/snapshots/test_diagnostics.ambr create mode 100644 tests/components/ipma/test_diagnostics.py diff --git a/homeassistant/components/ipma/diagnostics.py b/homeassistant/components/ipma/diagnostics.py new file mode 100644 index 00000000000..30c22d6481a --- /dev/null +++ b/homeassistant/components/ipma/diagnostics.py @@ -0,0 +1,32 @@ +"""Diagnostics support for IPMA.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant + +from .const import DATA_API, DATA_LOCATION, DOMAIN + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + + location = hass.data[DOMAIN][entry.entry_id][DATA_LOCATION] + api = hass.data[DOMAIN][entry.entry_id][DATA_API] + + return { + "location_information": { + "latitude": round(float(entry.data[CONF_LATITUDE]), 3), + "longitude": round(float(entry.data[CONF_LONGITUDE]), 3), + "global_id_local": location.global_id_local, + "id_station": location.id_station, + "name": location.name, + "station": location.station, + }, + "current_weather": await location.observation(api), + "weather_forecast": await location.forecast(api, 1), + } diff --git a/tests/components/ipma/conftest.py b/tests/components/ipma/conftest.py index dda0e69d118..7f3e82a8819 100644 --- a/tests/components/ipma/conftest.py +++ b/tests/components/ipma/conftest.py @@ -1,36 +1,42 @@ """Define test fixtures for IPMA.""" +from unittest.mock import patch + import pytest from homeassistant.components.ipma import DOMAIN from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME +from homeassistant.core import HomeAssistant + +from . import MockLocation from tests.common import MockConfigEntry -@pytest.fixture(name="config_entry") -def config_entry_fixture(hass, config): +@pytest.fixture +def config_entry(hass): """Define a config entry fixture.""" entry = MockConfigEntry( domain=DOMAIN, - data=config, + data={ + CONF_NAME: "Home", + CONF_LATITUDE: 0, + CONF_LONGITUDE: 0, + }, ) entry.add_to_hass(hass) return entry -@pytest.fixture(name="config") -def config_fixture(): - """Define a config entry data fixture.""" - return { - CONF_NAME: "Home", - CONF_LATITUDE: 0, - CONF_LONGITUDE: 0, - } +@pytest.fixture +async def init_integration( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> MockConfigEntry: + """Set up the IPMA integration for testing.""" + config_entry.add_to_hass(hass) + with patch("pyipma.location.Location.get", return_value=MockLocation()): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() -@pytest.fixture(name="setup_config_entry") -async def setup_config_entry_fixture(hass, config_entry): - """Define a fixture to set up ipma.""" - assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() + return config_entry diff --git a/tests/components/ipma/snapshots/test_diagnostics.ambr b/tests/components/ipma/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..c95364b6e4a --- /dev/null +++ b/tests/components/ipma/snapshots/test_diagnostics.ambr @@ -0,0 +1,60 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'current_weather': list([ + 0.0, + 71.0, + 1000.0, + 0.0, + 18.0, + 'NW', + 3.94, + ]), + 'location_information': dict({ + 'global_id_local': 1130600, + 'id_station': 1200545, + 'latitude': 0.0, + 'longitude': 0.0, + 'name': 'HomeTown', + 'station': 'HomeTown Station', + }), + 'weather_forecast': list([ + list([ + '7.7', + '2020-01-15T01:00:00+00:00', + 1, + '86.9', + 12.0, + None, + 80.0, + 10.6, + '2020-01-15T02:51:00', + list([ + 10, + 'Light rain', + 'Chuva fraca ou chuvisco', + ]), + 'S', + '32.7', + ]), + list([ + '5.7', + '2020-01-15T02:00:00+00:00', + 1, + '86.9', + 12.0, + None, + 80.0, + 10.6, + '2020-01-15T02:51:00', + list([ + 1, + 'Clear sky', + 'Céu limpo', + ]), + 'S', + '32.7', + ]), + ]), + }) +# --- diff --git a/tests/components/ipma/test_config_flow.py b/tests/components/ipma/test_config_flow.py index 4ca4abdaa2e..e17c8d011a9 100644 --- a/tests/components/ipma/test_config_flow.py +++ b/tests/components/ipma/test_config_flow.py @@ -92,7 +92,7 @@ async def test_config_flow_failures(hass: HomeAssistant) -> None: } -async def test_flow_entry_already_exists(hass: HomeAssistant, config_entry) -> None: +async def test_flow_entry_already_exists(hass: HomeAssistant, init_integration) -> None: """Test user input for config_entry that already exists. Test when the form should show when user puts existing location diff --git a/tests/components/ipma/test_diagnostics.py b/tests/components/ipma/test_diagnostics.py new file mode 100644 index 00000000000..b7d421a2ee5 --- /dev/null +++ b/tests/components/ipma/test_diagnostics.py @@ -0,0 +1,22 @@ +"""Test IPMA diagnostics.""" + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + init_integration, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics.""" + + assert ( + await get_diagnostics_for_config_entry(hass, hass_client, init_integration) + == snapshot + )