mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Handle InvalidRegion in Tesla Fleet (#123958)
This commit is contained in:
parent
72e235ad9f
commit
9b78ae5908
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user