-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Fix Starlink's ever updating uptime #155574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
513cc2a
8522d73
118dbab
3291814
a784e8e
c1fa9b9
854f174
6206e1c
678c4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| """Tests Starlink integration init/unload.""" | ||
|
|
||
| import copy | ||
| from datetime import datetime, timedelta | ||
| from unittest.mock import patch | ||
|
|
||
| from homeassistant.components.starlink.const import DOMAIN | ||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.const import CONF_IP_ADDRESS | ||
| from homeassistant.core import HomeAssistant, State | ||
| from homeassistant.util import dt as dt_util | ||
|
|
||
| from .patchers import ( | ||
| HISTORY_STATS_SUCCESS_PATCHER, | ||
| LOCATION_DATA_SUCCESS_PATCHER, | ||
| SLEEP_DATA_SUCCESS_PATCHER, | ||
| STATUS_DATA_FIXTURE, | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| ) | ||
|
|
||
| from tests.common import MockConfigEntry, mock_restore_cache_with_extra_data | ||
| from tests.common import ( | ||
| MockConfigEntry, | ||
| async_fire_time_changed, | ||
| mock_restore_cache_with_extra_data, | ||
| ) | ||
|
|
||
|
|
||
| async def test_successful_entry(hass: HomeAssistant) -> None: | ||
|
|
@@ -25,9 +33,9 @@ async def test_successful_entry(hass: HomeAssistant) -> None: | |
| ) | ||
|
|
||
| with ( | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| LOCATION_DATA_SUCCESS_PATCHER, | ||
| SLEEP_DATA_SUCCESS_PATCHER, | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| HISTORY_STATS_SUCCESS_PATCHER, | ||
| ): | ||
| entry.add_to_hass(hass) | ||
|
|
@@ -48,9 +56,9 @@ async def test_unload_entry(hass: HomeAssistant) -> None: | |
| ) | ||
|
|
||
| with ( | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| LOCATION_DATA_SUCCESS_PATCHER, | ||
| SLEEP_DATA_SUCCESS_PATCHER, | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| HISTORY_STATS_SUCCESS_PATCHER, | ||
| ): | ||
| entry.add_to_hass(hass) | ||
|
|
@@ -65,7 +73,7 @@ async def test_unload_entry(hass: HomeAssistant) -> None: | |
|
|
||
|
|
||
| async def test_restore_cache_with_accumulation(hass: HomeAssistant) -> None: | ||
| """Test configuring Starlink.""" | ||
| """Test Starlink accumulation.""" | ||
| entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| data={CONF_IP_ADDRESS: "1.2.3.4:0000"}, | ||
|
|
@@ -89,9 +97,9 @@ async def test_restore_cache_with_accumulation(hass: HomeAssistant) -> None: | |
| ) | ||
|
|
||
| with ( | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| LOCATION_DATA_SUCCESS_PATCHER, | ||
| SLEEP_DATA_SUCCESS_PATCHER, | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| HISTORY_STATS_SUCCESS_PATCHER, | ||
| ): | ||
| entry.add_to_hass(hass) | ||
|
|
@@ -112,3 +120,97 @@ async def test_restore_cache_with_accumulation(hass: HomeAssistant) -> None: | |
| await entry.runtime_data.async_refresh() | ||
|
|
||
| assert hass.states.get(entity_id).state == str(1 + 0.01572462736977) | ||
|
|
||
|
|
||
| async def test_last_restart_state(hass: HomeAssistant) -> None: | ||
| """Test Starlink last restart state.""" | ||
| entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| data={CONF_IP_ADDRESS: "1.2.3.4:0000"}, | ||
| ) | ||
| entity_id = "sensor.starlink_last_restart" | ||
|
|
||
| with ( | ||
| LOCATION_DATA_SUCCESS_PATCHER, | ||
| SLEEP_DATA_SUCCESS_PATCHER, | ||
| STATUS_DATA_SUCCESS_PATCHER, | ||
| HISTORY_STATS_SUCCESS_PATCHER, | ||
| patch( | ||
| "homeassistant.components.starlink.sensor.now", | ||
| return_value=datetime.fromisoformat("2025-10-22T13:31:29+00:00"), | ||
| ), | ||
| ): | ||
| entry.add_to_hass(hass) | ||
|
|
||
| await hass.config_entries.async_setup(entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert entry.runtime_data | ||
| assert entry.runtime_data.data | ||
| assert entry.runtime_data.data.status["uptime"] == 804138 | ||
|
|
||
|
||
| assert hass.states.get(entity_id).state == "2025-10-13T06:09:11+00:00" | ||
|
|
||
| status_data = copy.deepcopy(STATUS_DATA_FIXTURE) | ||
| status_data[0]["uptime"] = 804144 | ||
|
|
||
| with ( | ||
| patch( | ||
| "homeassistant.components.starlink.coordinator.status_data", | ||
| return_value=status_data, | ||
| ), | ||
| patch( | ||
| "homeassistant.components.starlink.sensor.now", | ||
| return_value=datetime.fromisoformat("2025-10-22T13:31:34+00:00"), | ||
| ), | ||
| ): | ||
| await entry.runtime_data.async_refresh() | ||
|
|
||
| assert entry.runtime_data.data.status["uptime"] == 804144 | ||
|
|
||
| async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30)) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| assert hass.states.get(entity_id).state == "2025-10-13T06:09:11+00:00" | ||
|
|
||
| status_data[0]["uptime"] = 804134 | ||
|
|
||
| with ( | ||
| patch( | ||
| "homeassistant.components.starlink.coordinator.status_data", | ||
| return_value=status_data, | ||
| ), | ||
| patch( | ||
| "homeassistant.components.starlink.sensor.now", | ||
| return_value=datetime.fromisoformat("2025-10-22T13:31:39+00:00"), | ||
| ), | ||
| ): | ||
| await entry.runtime_data.async_refresh() | ||
|
|
||
| assert entry.runtime_data.data.status["uptime"] == 804134 | ||
|
|
||
| async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30)) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| assert hass.states.get(entity_id).state == "2025-10-13T06:09:11+00:00" | ||
|
|
||
| status_data[0]["uptime"] = 100 | ||
|
|
||
| with ( | ||
| patch( | ||
| "homeassistant.components.starlink.coordinator.status_data", | ||
| return_value=status_data, | ||
| ), | ||
| patch( | ||
| "homeassistant.components.starlink.sensor.now", | ||
| return_value=datetime.fromisoformat("2025-10-22T13:31:44+00:00"), | ||
| ), | ||
| ): | ||
| await entry.runtime_data.async_refresh() | ||
|
|
||
| assert entry.runtime_data.data.status["uptime"] == 100 | ||
|
|
||
| async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30)) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| assert hass.states.get(entity_id).state == "2025-10-22T13:30:04+00:00" | ||
Uh oh!
There was an error while loading. Please reload this page.