Add diagnostics platform to Fastdotcom (#111525)
This commit is contained in:
parent
4b4258881b
commit
1ff049cc66
7 changed files with 83 additions and 12 deletions
|
@ -14,7 +14,7 @@ from homeassistant.helpers.start import async_at_started
|
|||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import CONF_MANUAL, DEFAULT_INTERVAL, DOMAIN, PLATFORMS
|
||||
from .coordinator import FastdotcomDataUpdateCoordindator
|
||||
from .coordinator import FastdotcomDataUpdateCoordinator
|
||||
from .services import async_setup_services
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -50,7 +50,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Fast.com from a config entry."""
|
||||
coordinator = FastdotcomDataUpdateCoordindator(hass)
|
||||
coordinator = FastdotcomDataUpdateCoordinator(hass)
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(
|
||||
|
|
|
@ -12,7 +12,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||
from .const import DEFAULT_INTERVAL, DOMAIN, LOGGER
|
||||
|
||||
|
||||
class FastdotcomDataUpdateCoordindator(DataUpdateCoordinator[float]):
|
||||
class FastdotcomDataUpdateCoordinator(DataUpdateCoordinator[float]):
|
||||
"""Class to manage fetching Fast.com data API."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
|
|
24
homeassistant/components/fastdotcom/diagnostics.py
Normal file
24
homeassistant/components/fastdotcom/diagnostics.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
"""Diagnostics support for Fast.com."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import FastdotcomDataUpdateCoordinator
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for the config entry."""
|
||||
coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][
|
||||
config_entry.entry_id
|
||||
]
|
||||
|
||||
return {
|
||||
"coordinator_data": coordinator.data,
|
||||
}
|
|
@ -15,7 +15,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import FastdotcomDataUpdateCoordindator
|
||||
from .coordinator import FastdotcomDataUpdateCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
@ -24,13 +24,11 @@ async def async_setup_entry(
|
|||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Fast.com sensor."""
|
||||
coordinator: FastdotcomDataUpdateCoordindator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities([SpeedtestSensor(entry.entry_id, coordinator)])
|
||||
|
||||
|
||||
class SpeedtestSensor(
|
||||
CoordinatorEntity[FastdotcomDataUpdateCoordindator], SensorEntity
|
||||
):
|
||||
class SpeedtestSensor(CoordinatorEntity[FastdotcomDataUpdateCoordinator], SensorEntity):
|
||||
"""Implementation of a Fast.com sensor."""
|
||||
|
||||
_attr_translation_key = "download"
|
||||
|
@ -41,7 +39,7 @@ class SpeedtestSensor(
|
|||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self, entry_id: str, coordinator: FastdotcomDataUpdateCoordindator
|
||||
self, entry_id: str, coordinator: FastdotcomDataUpdateCoordinator
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
|
|
|
@ -8,14 +8,14 @@ from homeassistant.exceptions import HomeAssistantError
|
|||
from homeassistant.helpers import issue_registry as ir
|
||||
|
||||
from .const import DOMAIN, SERVICE_NAME
|
||||
from .coordinator import FastdotcomDataUpdateCoordindator
|
||||
from .coordinator import FastdotcomDataUpdateCoordinator
|
||||
|
||||
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up the service for the Fastdotcom integration."""
|
||||
|
||||
@callback
|
||||
def collect_coordinator() -> FastdotcomDataUpdateCoordindator:
|
||||
def collect_coordinator() -> FastdotcomDataUpdateCoordinator:
|
||||
"""Collect the coordinator Fastdotcom."""
|
||||
config_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
if not config_entries:
|
||||
|
@ -24,7 +24,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
|||
for config_entry in config_entries:
|
||||
if config_entry.state != ConfigEntryState.LOADED:
|
||||
raise HomeAssistantError(f"{config_entry.title} is not loaded")
|
||||
coordinator: FastdotcomDataUpdateCoordindator = hass.data[DOMAIN][
|
||||
coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][
|
||||
config_entry.entry_id
|
||||
]
|
||||
break
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# serializer version: 1
|
||||
# name: test_get_config_entry_diagnostics
|
||||
dict({
|
||||
'coordinator_data': 50.3,
|
||||
})
|
||||
# ---
|
43
tests/components/fastdotcom/test_diagnostics.py
Normal file
43
tests/components/fastdotcom/test_diagnostics.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
"""Test the Fast.com component diagnostics."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.fastdotcom.const import DEFAULT_NAME, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
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_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test if get_config_entry_diagnostics returns the correct data."""
|
||||
config_entry = MockConfigEntry(
|
||||
version=1,
|
||||
domain=DOMAIN,
|
||||
title=DEFAULT_NAME,
|
||||
source=SOURCE_USER,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
minor_version=1,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.fastdotcom.coordinator.fast_com", return_value=50.3
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||
== snapshot
|
||||
)
|
Loading…
Add table
Reference in a new issue