Handle InvalidRegion in Tesla Fleet (#123958)

This commit is contained in:
Brett Adams 2024-08-15 19:00:07 +10:00 committed by GitHub
parent 72e235ad9f
commit 9b78ae5908
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 1 deletions

View File

@ -7,7 +7,9 @@ import jwt
from tesla_fleet_api import EnergySpecific, TeslaFleetApi, VehicleSpecific
from tesla_fleet_api.const import Scope
from tesla_fleet_api.exceptions import (
InvalidRegion,
InvalidToken,
LibraryError,
LoginRequired,
OAuthExpired,
TeslaFleetError,
@ -75,7 +77,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) -
region=region,
charging_scope=False,
partner_scope=False,
user_scope=False,
energy_scope=Scope.ENERGY_DEVICE_DATA in scopes,
vehicle_scope=Scope.VEHICLE_DEVICE_DATA in scopes,
refresh_hook=_refresh_token,
@ -84,6 +85,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) -
products = (await tesla.products())["response"]
except (InvalidToken, OAuthExpired, LoginRequired) as e:
raise ConfigEntryAuthFailed from e
except InvalidRegion:
try:
LOGGER.info("Region is invalid, trying to find the correct region")
await tesla.find_server()
try:
products = (await tesla.products())["response"]
except TeslaFleetError as e:
raise ConfigEntryNotReady from e
except LibraryError as e:
raise ConfigEntryAuthFailed from e
except TeslaFleetError as e:
raise ConfigEntryNotReady from e

View File

@ -122,3 +122,12 @@ def mock_site_info() -> Generator[AsyncMock]:
side_effect=lambda: deepcopy(SITE_INFO),
) as mock_live_status:
yield mock_live_status
@pytest.fixture(autouse=True)
def mock_find_server() -> Generator[AsyncMock]:
"""Mock Tesla Fleet find server method."""
with patch(
"homeassistant.components.tesla_fleet.TeslaFleetApi.find_server",
) as mock_find_server:
yield mock_find_server

View File

@ -6,7 +6,9 @@ from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import (
InvalidRegion,
InvalidToken,
LibraryError,
LoginRequired,
OAuthExpired,
RateLimited,
@ -326,3 +328,32 @@ async def test_energy_info_refresh_ratelimited(
await hass.async_block_till_done()
assert mock_site_info.call_count == 3
async def test_init_region_issue(
hass: HomeAssistant,
normal_config_entry: MockConfigEntry,
mock_products: AsyncMock,
mock_find_server: AsyncMock,
) -> None:
"""Test init with region issue."""
mock_products.side_effect = InvalidRegion
await setup_platform(hass, normal_config_entry)
mock_find_server.assert_called_once()
assert normal_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_init_region_issue_failed(
hass: HomeAssistant,
normal_config_entry: MockConfigEntry,
mock_products: AsyncMock,
mock_find_server: AsyncMock,
) -> None:
"""Test init with unresolvable region issue."""
mock_products.side_effect = InvalidRegion
mock_find_server.side_effect = LibraryError
await setup_platform(hass, normal_config_entry)
mock_find_server.assert_called_once()
assert normal_config_entry.state is ConfigEntryState.SETUP_ERROR