mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve API calls in Teslemetry (#122449)
* Improve API calls * Small tweak * typing fixtures
This commit is contained in:
parent
77282ed4b0
commit
8d14095cb9
@ -75,7 +75,16 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||||||
self.update_interval = VEHICLE_INTERVAL
|
self.update_interval = VEHICLE_INTERVAL
|
||||||
|
|
||||||
try:
|
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:
|
except VehicleOffline:
|
||||||
self.data["state"] = TeslemetryState.OFFLINE
|
self.data["state"] = TeslemetryState.OFFLINE
|
||||||
return self.data
|
return self.data
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Generator
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ def mock_products():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def mock_vehicle_data():
|
def mock_vehicle_data() -> Generator[AsyncMock]:
|
||||||
"""Mock Tesla Fleet API Vehicle Specific vehicle_data method."""
|
"""Mock Tesla Fleet API Vehicle Specific vehicle_data method."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.teslemetry.VehicleSpecific.vehicle_data",
|
"homeassistant.components.teslemetry.VehicleSpecific.vehicle_data",
|
||||||
@ -57,7 +58,7 @@ def mock_wake_up():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def mock_vehicle():
|
def mock_vehicle() -> Generator[AsyncMock]:
|
||||||
"""Mock Tesla Fleet API Vehicle Specific vehicle method."""
|
"""Mock Tesla Fleet API Vehicle Specific vehicle method."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.teslemetry.VehicleSpecific.vehicle",
|
"homeassistant.components.teslemetry.VehicleSpecific.vehicle",
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"""Test the Teslemetry init."""
|
"""Test the Teslemetry init."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
@ -21,7 +23,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
from . import setup_platform
|
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
|
from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
@ -68,6 +70,21 @@ async def test_devices(
|
|||||||
|
|
||||||
|
|
||||||
# Vehicle Coordinator
|
# 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(
|
async def test_vehicle_refresh_offline(
|
||||||
hass: HomeAssistant, mock_vehicle_data, freezer: FrozenDateTimeFactory
|
hass: HomeAssistant, mock_vehicle_data, freezer: FrozenDateTimeFactory
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user