From c33ca4f664a4e3eea801170be07a35b134f45e1d Mon Sep 17 00:00:00 2001 From: Jack Boswell Date: Wed, 15 Mar 2023 10:24:47 +1300 Subject: [PATCH] Add diagnostics to Starlink (#86328) --- homeassistant/components/starlink/__init__.py | 2 +- .../components/starlink/diagnostics.py | 21 ++++++ .../fixtures/status_data_success.json | 70 ++++++++++++++++++ tests/components/starlink/patchers.py | 9 +-- .../starlink/snapshots/test_diagnostics.ambr | 73 +++++++++++++++++++ tests/components/starlink/test_diagnostics.py | 34 +++++++++ 6 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 homeassistant/components/starlink/diagnostics.py create mode 100644 tests/components/starlink/fixtures/status_data_success.json create mode 100644 tests/components/starlink/snapshots/test_diagnostics.ambr create mode 100644 tests/components/starlink/test_diagnostics.py diff --git a/homeassistant/components/starlink/__init__.py b/homeassistant/components/starlink/__init__.py index ceb962c88cd..c59269d2e07 100644 --- a/homeassistant/components/starlink/__init__.py +++ b/homeassistant/components/starlink/__init__.py @@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant from .const import DOMAIN from .coordinator import StarlinkUpdateCoordinator -PLATFORMS: list[Platform] = [ +PLATFORMS = [ Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, diff --git a/homeassistant/components/starlink/diagnostics.py b/homeassistant/components/starlink/diagnostics.py new file mode 100644 index 00000000000..10711e7155e --- /dev/null +++ b/homeassistant/components/starlink/diagnostics.py @@ -0,0 +1,21 @@ +"""Fetches diagnostic data for Starlink systems.""" + +from dataclasses import asdict +from typing import Any + +from homeassistant.components.diagnostics.util import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +from .const import DOMAIN +from .coordinator import StarlinkUpdateCoordinator + +TO_REDACT = {"id"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for Starlink config entries.""" + coordinator: StarlinkUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + return async_redact_data(asdict(coordinator.data), TO_REDACT) diff --git a/tests/components/starlink/fixtures/status_data_success.json b/tests/components/starlink/fixtures/status_data_success.json new file mode 100644 index 00000000000..e8cdc27e625 --- /dev/null +++ b/tests/components/starlink/fixtures/status_data_success.json @@ -0,0 +1,70 @@ +[ + { + "id": "ut00000000-00000000-000000aa", + "hardware_version": "rev3_proto2", + "software_version": "191e4dfa-d63a-46b1-a73b-9fa907733864.uterm.release", + "state": "CONNECTED", + "uptime": 804138, + "snr": null, + "seconds_to_first_nonempty_slot": 0.0, + "pop_ping_drop_rate": 0.0, + "downlink_throughput_bps": 10108.2724609375, + "uplink_throughput_bps": 11802.771484375, + "pop_ping_latency_ms": 30.285715103149414, + "alerts": 0, + "fraction_obstructed": 0.0, + "currently_obstructed": false, + "seconds_obstructed": null, + "obstruction_duration": null, + "obstruction_interval": null, + "direction_azimuth": -179.00344848632812, + "direction_elevation": 68.67173767089844, + "is_snr_above_noise_floor": true + }, + { + "wedges_fraction_obstructed[]": [ + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ], + "raw_wedges_fraction_obstructed[]": [ + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ], + "valid_s": 803872.0 + }, + { + "alert_motors_stuck": false, + "alert_thermal_throttle": false, + "alert_thermal_shutdown": false, + "alert_mast_not_near_vertical": false, + "alert_unexpected_location": false, + "alert_slow_ethernet_speeds": false, + "alert_roaming": false, + "alert_install_pending": false, + "alert_is_heating": false, + "alert_power_supply_thermal_throttle": false, + "alert_is_power_save_idle": false, + "alert_moving_while_not_mobile": false, + "alert_moving_fast_while_not_aviation": false + } +] diff --git a/tests/components/starlink/patchers.py b/tests/components/starlink/patchers.py index 0013b4e56ba..dfc0d2415df 100644 --- a/tests/components/starlink/patchers.py +++ b/tests/components/starlink/patchers.py @@ -1,7 +1,8 @@ """General Starlink patchers.""" +import json from unittest.mock import patch -from starlink_grpc import StatusDict +from tests.common import load_fixture SETUP_ENTRY_PATCHER = patch( "homeassistant.components.starlink.async_setup_entry", return_value=True @@ -9,11 +10,7 @@ SETUP_ENTRY_PATCHER = patch( COORDINATOR_SUCCESS_PATCHER = patch( "homeassistant.components.starlink.coordinator.status_data", - return_value=[ - StatusDict(id="1", software_version="1", hardware_version="1"), - {}, - {}, - ], + return_value=json.loads(load_fixture("status_data_success.json", "starlink")), ) DEVICE_FOUND_PATCHER = patch( diff --git a/tests/components/starlink/snapshots/test_diagnostics.ambr b/tests/components/starlink/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000..6f859aaf50d --- /dev/null +++ b/tests/components/starlink/snapshots/test_diagnostics.ambr @@ -0,0 +1,73 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'alert': dict({ + 'alert_install_pending': False, + 'alert_is_heating': False, + 'alert_is_power_save_idle': False, + 'alert_mast_not_near_vertical': False, + 'alert_motors_stuck': False, + 'alert_moving_fast_while_not_aviation': False, + 'alert_moving_while_not_mobile': False, + 'alert_power_supply_thermal_throttle': False, + 'alert_roaming': False, + 'alert_slow_ethernet_speeds': False, + 'alert_thermal_shutdown': False, + 'alert_thermal_throttle': False, + 'alert_unexpected_location': False, + }), + 'obstruction': dict({ + 'raw_wedges_fraction_obstructed[]': list([ + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + ]), + 'valid_s': 803872.0, + 'wedges_fraction_obstructed[]': list([ + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + ]), + }), + 'status': dict({ + 'alerts': 0, + 'currently_obstructed': False, + 'direction_azimuth': -179.00344848632812, + 'direction_elevation': 68.67173767089844, + 'downlink_throughput_bps': 10108.2724609375, + 'fraction_obstructed': 0.0, + 'hardware_version': 'rev3_proto2', + 'id': '**REDACTED**', + 'is_snr_above_noise_floor': True, + 'obstruction_duration': None, + 'obstruction_interval': None, + 'pop_ping_drop_rate': 0.0, + 'pop_ping_latency_ms': 30.285715103149414, + 'seconds_obstructed': None, + 'seconds_to_first_nonempty_slot': 0.0, + 'snr': None, + 'software_version': '191e4dfa-d63a-46b1-a73b-9fa907733864.uterm.release', + 'state': 'CONNECTED', + 'uplink_throughput_bps': 11802.771484375, + 'uptime': 804138, + }), + }) +# --- diff --git a/tests/components/starlink/test_diagnostics.py b/tests/components/starlink/test_diagnostics.py new file mode 100644 index 00000000000..4bf8a619c88 --- /dev/null +++ b/tests/components/starlink/test_diagnostics.py @@ -0,0 +1,34 @@ +"""Tests for Starlink diagnostics.""" +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.starlink.const import DOMAIN +from homeassistant.const import CONF_IP_ADDRESS +from homeassistant.core import HomeAssistant + +from .patchers import COORDINATOR_SUCCESS_PATCHER + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Test generating diagnostics for a config entry.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_IP_ADDRESS: "1.2.3.4:0000"}, + ) + + with COORDINATOR_SUCCESS_PATCHER: + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + diag = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert diag == snapshot