Add diagnostics platform to IPMA (#105697)

This commit is contained in:
Diogo Gomes 2024-03-13 11:07:28 +00:00 committed by GitHub
parent 6666f6a8a5
commit 669dd36daf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 137 additions and 17 deletions

View File

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

View File

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

View File

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

View File

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

View File

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