Skip to content

Slow down polling in Tesla Fleet #142130

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

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/tesla_fleet/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from .const import ENERGY_HISTORY_FIELDS, LOGGER, TeslaFleetState

VEHICLE_INTERVAL_SECONDS = 300
VEHICLE_INTERVAL_SECONDS = 600
VEHICLE_INTERVAL = timedelta(seconds=VEHICLE_INTERVAL_SECONDS)
VEHICLE_WAIT = timedelta(minutes=15)

Expand Down
100 changes: 51 additions & 49 deletions tests/components/tesla_fleet/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the Tesla Fleet init."""

from copy import deepcopy
from datetime import timedelta
from unittest.mock import AsyncMock, patch

from aiohttp import RequestInfo
Expand Down Expand Up @@ -231,57 +232,58 @@ async def test_vehicle_sleep(
freezer: FrozenDateTimeFactory,
) -> None:
"""Test coordinator refresh with an error."""
await setup_platform(hass, normal_config_entry)
assert mock_vehicle_data.call_count == 1

freezer.tick(VEHICLE_WAIT + VEHICLE_INTERVAL)
async_fire_time_changed(hass)
# Let vehicle sleep, no updates for 15 minutes
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 2

freezer.tick(VEHICLE_INTERVAL)
async_fire_time_changed(hass)
# No polling, call_count should not increase
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 2
TEST_INTERVAL = timedelta(seconds=120)

freezer.tick(VEHICLE_INTERVAL)
async_fire_time_changed(hass)
# No polling, call_count should not increase
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 2

freezer.tick(VEHICLE_WAIT)
async_fire_time_changed(hass)
# Vehicle didn't sleep, go back to normal
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 3

freezer.tick(VEHICLE_INTERVAL)
async_fire_time_changed(hass)
# Regular polling
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 4

mock_vehicle_data.return_value = VEHICLE_DATA_ALT
freezer.tick(VEHICLE_INTERVAL)
async_fire_time_changed(hass)
# Vehicle active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 5

freezer.tick(VEHICLE_WAIT)
async_fire_time_changed(hass)
# Dont let sleep when active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 6

freezer.tick(VEHICLE_WAIT)
async_fire_time_changed(hass)
# Dont let sleep when active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 7
with patch(
"homeassistant.components.tesla_fleet.coordinator.VEHICLE_INTERVAL",
TEST_INTERVAL,
):
await setup_platform(hass, normal_config_entry)
assert mock_vehicle_data.call_count == 1

freezer.tick(VEHICLE_WAIT + TEST_INTERVAL)
async_fire_time_changed(hass)
# Let vehicle sleep, no updates for 15 minutes
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 2

freezer.tick(TEST_INTERVAL)
async_fire_time_changed(hass)
# No polling, call_count should not increase
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 2

freezer.tick(VEHICLE_WAIT)
async_fire_time_changed(hass)
# Vehicle didn't sleep, go back to normal
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 3

freezer.tick(TEST_INTERVAL)
async_fire_time_changed(hass)
# Regular polling
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 4

mock_vehicle_data.return_value = VEHICLE_DATA_ALT
freezer.tick(TEST_INTERVAL)
async_fire_time_changed(hass)
# Vehicle active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 5

freezer.tick(TEST_INTERVAL)
async_fire_time_changed(hass)
# Dont let sleep when active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 6

freezer.tick(TEST_INTERVAL)
async_fire_time_changed(hass)
# Dont let sleep when active
await hass.async_block_till_done()
assert mock_vehicle_data.call_count == 7


# Test Energy Live Coordinator
Expand Down