Add diagnostics to GeoNet NZ Quakes integration (#125320)

* add diagnostics platform

* add tests

* add snapshot data

* remove from no diagnostics list
This commit is contained in:
Malte Franken 2024-09-05 19:35:36 +10:00 committed by GitHub
parent 511ecf98d5
commit ba7f36328d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 93 additions and 1 deletions

View File

@ -0,0 +1,39 @@
"""Diagnostics support for GeoNet NZ Quakes Feeds integration."""
from __future__ import annotations
from typing import Any
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant
from . import GeonetnzQuakesFeedEntityManager
from .const import DOMAIN, FEED
TO_REDACT = {CONF_LATITUDE, CONF_LONGITUDE}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
data: dict[str, Any] = {
"info": async_redact_data(config_entry.data, TO_REDACT),
}
manager: GeonetnzQuakesFeedEntityManager = hass.data[DOMAIN][FEED][
config_entry.entry_id
]
status_info = manager.status_info()
if status_info:
data["service"] = {
"status": status_info.status,
"total": status_info.total,
"last_update": status_info.last_update,
"last_update_successful": status_info.last_update_successful,
"last_timestamp": status_info.last_timestamp,
}
return data

View File

@ -117,7 +117,6 @@ NO_IOT_CLASS = [
# https://github.com/home-assistant/developers.home-assistant/pull/1512
NO_DIAGNOSTICS = [
"dlna_dms",
"geonetnz_quakes",
"hyperion",
"nightscout",
"pvpc_hourly_pricing",

View File

@ -0,0 +1,21 @@
# serializer version: 1
# name: test_entry_diagnostics
dict({
'info': dict({
'latitude': '**REDACTED**',
'longitude': '**REDACTED**',
'minimum_magnitude': 0.0,
'mmi': 4,
'radius': 25,
'scan_interval': 300.0,
'unit_system': 'metric',
}),
'service': dict({
'last_timestamp': None,
'last_update': '2024-09-05T15:00:00',
'last_update_successful': '2024-09-05T15:00:00',
'status': 'OK',
'total': 0,
}),
})
# ---

View File

@ -0,0 +1,33 @@
"""Test GeoNet NZ Quakes diagnostics."""
from __future__ import annotations
from unittest.mock import patch
import pytest
from syrupy 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
@pytest.mark.freeze_time("2024-09-05 15:00:00")
async def test_entry_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
config_entry: MockConfigEntry,
) -> None:
"""Test config entry diagnostics."""
with patch("aio_geojson_client.feed.GeoJsonFeed.update") as mock_feed_update:
mock_feed_update.return_value = "OK", []
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
assert result == snapshot