Add diagnostics support to Tankerkoenig (#106301)

This commit is contained in:
Michael 2023-12-23 12:20:15 +01:00 committed by GitHub
parent d83dafa14a
commit da684d6a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,32 @@
"""Diagnostics support for Tankerkoenig."""
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_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_UNIQUE_ID,
)
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import TankerkoenigDataUpdateCoordinator
TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_UNIQUE_ID}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator: TankerkoenigDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
diag_data = {
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
"data": coordinator.data,
}
return diag_data

View File

@ -0,0 +1,43 @@
# serializer version: 1
# name: test_entry_diagnostics
dict({
'data': dict({
'3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8': dict({
'diesel': 1.659,
'e10': 1.659,
'e5': 1.719,
'status': 'open',
}),
}),
'entry': dict({
'data': dict({
'api_key': '**REDACTED**',
'fuel_types': list([
'e5',
]),
'location': dict({
'latitude': '**REDACTED**',
'longitude': '**REDACTED**',
}),
'name': 'Home',
'radius': 2.0,
'stations': list([
'3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8',
]),
}),
'disabled_by': None,
'domain': 'tankerkoenig',
'entry_id': '8036b4412f2fae6bb9dbab7fe8e37f87',
'minor_version': 1,
'options': dict({
'show_on_map': True,
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': '**REDACTED**',
'version': 1,
}),
})
# ---

View File

@ -0,0 +1,103 @@
"""Tests for the Tankerkoening integration."""
from __future__ import annotations
from unittest.mock import patch
from syrupy import SnapshotAssertion
from homeassistant.components.tankerkoenig.const import (
CONF_FUEL_TYPES,
CONF_STATIONS,
DOMAIN,
)
from homeassistant.const import (
CONF_API_KEY,
CONF_LATITUDE,
CONF_LOCATION,
CONF_LONGITUDE,
CONF_NAME,
CONF_RADIUS,
CONF_SHOW_ON_MAP,
)
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
MOCK_USER_DATA = {
CONF_NAME: "Home",
CONF_API_KEY: "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx",
CONF_FUEL_TYPES: ["e5"],
CONF_LOCATION: {CONF_LATITUDE: 51.0, CONF_LONGITUDE: 13.0},
CONF_RADIUS: 2.0,
CONF_STATIONS: [
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
],
}
MOCK_OPTIONS = {
CONF_SHOW_ON_MAP: True,
}
MOCK_STATION_DATA = {
"ok": True,
"station": {
"id": "3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
"name": "Station ABC",
"brand": "Station",
"street": "Somewhere Street",
"houseNumber": "1",
"postCode": "01234",
"place": "Somewhere",
"openingTimes": [],
"overrides": [],
"wholeDay": True,
"isOpen": True,
"e5": 1.719,
"e10": 1.659,
"diesel": 1.659,
"lat": 51.1,
"lng": 13.1,
"state": "xxXX",
},
}
MOCK_STATION_PRICES = {
"ok": True,
"prices": {
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8": {
"status": "open",
"e5": 1.719,
"e10": 1.659,
"diesel": 1.659,
},
},
}
async def test_entry_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
with patch(
"homeassistant.components.tankerkoenig.coordinator.pytankerkoenig.getStationData",
return_value=MOCK_STATION_DATA,
), patch(
"homeassistant.components.tankerkoenig.coordinator.pytankerkoenig.getPriceList",
return_value=MOCK_STATION_PRICES,
):
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_USER_DATA,
options=MOCK_OPTIONS,
unique_id="mock.tankerkoenig",
entry_id="8036b4412f2fae6bb9dbab7fe8e37f87",
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert result == snapshot