Improve API calls in Teslemetry (#122449)

* Improve API calls

* Small tweak

* typing fixtures
This commit is contained in:
Brett Adams 2024-07-23 20:59:25 +10:00 committed by GitHub
parent 77282ed4b0
commit 8d14095cb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 5 deletions

View File

@ -75,7 +75,16 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
self.update_interval = VEHICLE_INTERVAL
try:
data = (await self.api.vehicle_data(endpoints=ENDPOINTS))["response"]
if self.data["state"] != TeslemetryState.ONLINE:
response = await self.api.vehicle()
self.data["state"] = response["response"]["state"]
if self.data["state"] != TeslemetryState.ONLINE:
return self.data
response = await self.api.vehicle_data(endpoints=ENDPOINTS)
data = response["response"]
except VehicleOffline:
self.data["state"] = TeslemetryState.OFFLINE
return self.data

View File

@ -2,8 +2,9 @@
from __future__ import annotations
from collections.abc import Generator
from copy import deepcopy
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
import pytest
@ -37,7 +38,7 @@ def mock_products():
@pytest.fixture(autouse=True)
def mock_vehicle_data():
def mock_vehicle_data() -> Generator[AsyncMock]:
"""Mock Tesla Fleet API Vehicle Specific vehicle_data method."""
with patch(
"homeassistant.components.teslemetry.VehicleSpecific.vehicle_data",
@ -57,7 +58,7 @@ def mock_wake_up():
@pytest.fixture(autouse=True)
def mock_vehicle():
def mock_vehicle() -> Generator[AsyncMock]:
"""Mock Tesla Fleet API Vehicle Specific vehicle method."""
with patch(
"homeassistant.components.teslemetry.VehicleSpecific.vehicle",

View File

@ -1,5 +1,7 @@
"""Test the Teslemetry init."""
from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion
@ -21,7 +23,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from . import setup_platform
from .const import VEHICLE_DATA_ALT
from .const import VEHICLE_DATA_ALT, WAKE_UP_ASLEEP
from tests.common import async_fire_time_changed
@ -68,6 +70,21 @@ async def test_devices(
# Vehicle Coordinator
async def test_vehicle_refresh_asleep(
hass: HomeAssistant,
mock_vehicle: AsyncMock,
mock_vehicle_data: AsyncMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test coordinator refresh with an error."""
mock_vehicle.return_value = WAKE_UP_ASLEEP
entry = await setup_platform(hass, [Platform.CLIMATE])
assert entry.state is ConfigEntryState.LOADED
mock_vehicle.assert_called_once()
mock_vehicle_data.assert_not_called()
async def test_vehicle_refresh_offline(
hass: HomeAssistant, mock_vehicle_data, freezer: FrozenDateTimeFactory
) -> None: