mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add diagnostics to Speedtest.net (#189156)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
cb35d268f9
commit
80f65b9cd7
@@ -0,0 +1,33 @@
|
||||
"""Diagnostics support for Speedtest.net."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import CONF_SERVER_ID, DEFAULT_SERVER
|
||||
from .coordinator import SpeedTestConfigEntry
|
||||
|
||||
# Client info (IP, ISP, location) and server location/host are left out on purpose
|
||||
RESULT_KEYS = ("download", "upload", "ping", "bytes_sent", "bytes_received")
|
||||
SERVER_KEYS = ("id", "sponsor", "country", "cc", "latency")
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: SpeedTestConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
results: dict[str, Any] | None = None
|
||||
if data := coordinator.data:
|
||||
results = {key: data[key] for key in RESULT_KEYS if key in data}
|
||||
results["server"] = {
|
||||
key: value
|
||||
for key, value in data.get("server", {}).items()
|
||||
if key in SERVER_KEYS
|
||||
}
|
||||
return {
|
||||
"configured_server_id": entry.options.get(CONF_SERVER_ID),
|
||||
"last_update_success": coordinator.last_update_success,
|
||||
"known_servers": len(coordinator.servers.keys() - {DEFAULT_SERVER}),
|
||||
"results": results,
|
||||
}
|
||||
@@ -51,7 +51,7 @@ rules:
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
diagnostics: todo
|
||||
diagnostics: done
|
||||
discovery-update-info:
|
||||
status: exempt
|
||||
comment: Integration is a cloud polling service and does not use discovery.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'configured_server_id': '1',
|
||||
'known_servers': 2,
|
||||
'last_update_success': True,
|
||||
'results': dict({
|
||||
'bytes_received': 19712300,
|
||||
'bytes_sent': 4194304,
|
||||
'download': 1024000,
|
||||
'ping': 18.465,
|
||||
'server': dict({
|
||||
'cc': 'CC',
|
||||
'country': 'Country',
|
||||
'id': '8408',
|
||||
'latency': 18.465,
|
||||
'sponsor': 'Sponsor',
|
||||
}),
|
||||
'upload': 1024000,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Tests for Speedtest.net diagnostics."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from pytest_homeassistant.typing import ClientSessionGenerator
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.speedtestdotnet.const import (
|
||||
CONF_SERVER_ID,
|
||||
CONF_SERVER_NAME,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import MOCK_RESULTS, MOCK_SERVERS
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_api: MagicMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={},
|
||||
options={
|
||||
CONF_SERVER_NAME: "Country1 - Sponsor1 - Server1",
|
||||
CONF_SERVER_ID: "1",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_api.return_value.get_best_server.return_value = MOCK_SERVERS[1][0]
|
||||
mock_api.return_value.results.dict.return_value = {
|
||||
**MOCK_RESULTS,
|
||||
"server": {**MOCK_RESULTS["server"], "sponsor": "Sponsor", "cc": "CC"},
|
||||
"client": {
|
||||
"ip": "1.2.3.4",
|
||||
"lat": "1.0",
|
||||
"lon": "2.0",
|
||||
"isp": "ISP",
|
||||
"country": "CC",
|
||||
},
|
||||
}
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot
|
||||
Reference in New Issue
Block a user