mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +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 import EnergySpecific, TeslaFleetApi, VehicleSpecific
|
||||||
from tesla_fleet_api.const import Scope
|
from tesla_fleet_api.const import Scope
|
||||||
from tesla_fleet_api.exceptions import (
|
from tesla_fleet_api.exceptions import (
|
||||||
|
InvalidRegion,
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
|
LibraryError,
|
||||||
LoginRequired,
|
LoginRequired,
|
||||||
OAuthExpired,
|
OAuthExpired,
|
||||||
TeslaFleetError,
|
TeslaFleetError,
|
||||||
@ -75,7 +77,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) -
|
|||||||
region=region,
|
region=region,
|
||||||
charging_scope=False,
|
charging_scope=False,
|
||||||
partner_scope=False,
|
partner_scope=False,
|
||||||
user_scope=False,
|
|
||||||
energy_scope=Scope.ENERGY_DEVICE_DATA in scopes,
|
energy_scope=Scope.ENERGY_DEVICE_DATA in scopes,
|
||||||
vehicle_scope=Scope.VEHICLE_DEVICE_DATA in scopes,
|
vehicle_scope=Scope.VEHICLE_DEVICE_DATA in scopes,
|
||||||
refresh_hook=_refresh_token,
|
refresh_hook=_refresh_token,
|
||||||
@ -84,6 +85,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) -
|
|||||||
products = (await tesla.products())["response"]
|
products = (await tesla.products())["response"]
|
||||||
except (InvalidToken, OAuthExpired, LoginRequired) as e:
|
except (InvalidToken, OAuthExpired, LoginRequired) as e:
|
||||||
raise ConfigEntryAuthFailed from 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:
|
except TeslaFleetError as e:
|
||||||
raise ConfigEntryNotReady from e
|
raise ConfigEntryNotReady from e
|
||||||
|
|
||||||
|
@ -122,3 +122,12 @@ def mock_site_info() -> Generator[AsyncMock]:
|
|||||||
side_effect=lambda: deepcopy(SITE_INFO),
|
side_effect=lambda: deepcopy(SITE_INFO),
|
||||||
) as mock_live_status:
|
) as mock_live_status:
|
||||||
yield 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
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
from tesla_fleet_api.exceptions import (
|
from tesla_fleet_api.exceptions import (
|
||||||
|
InvalidRegion,
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
|
LibraryError,
|
||||||
LoginRequired,
|
LoginRequired,
|
||||||
OAuthExpired,
|
OAuthExpired,
|
||||||
RateLimited,
|
RateLimited,
|
||||||
@ -326,3 +328,32 @@ async def test_energy_info_refresh_ratelimited(
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert mock_site_info.call_count == 3
|
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