Skip to content

Commit dd34d45

Browse files
authored
Improved error handling for oauth2 configuration in tesla_fleet integration (home-assistant#156219)
1 parent 603d4bc commit dd34d45

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

homeassistant/components/tesla_fleet/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from homeassistant.helpers import config_validation as cv, device_registry as dr
2424
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2525
from homeassistant.helpers.config_entry_oauth2_flow import (
26+
ImplementationUnavailableError,
2627
OAuth2Session,
2728
async_get_config_entry_implementation,
2829
)
@@ -61,6 +62,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) -
6162

6263
try:
6364
implementation = await async_get_config_entry_implementation(hass, entry)
65+
except ImplementationUnavailableError as err:
66+
raise ConfigEntryNotReady(
67+
translation_domain=DOMAIN,
68+
translation_key="oauth2_implementation_unavailable",
69+
) from err
6470
except ValueError as e:
6571
# Remove invalid implementation from config entry then raise AuthFailed
6672
hass.config_entries.async_update_entry(

homeassistant/components/tesla_fleet/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@
609609
"no_cable": {
610610
"message": "Charge cable will lock automatically when connected"
611611
},
612+
"oauth2_implementation_unavailable": {
613+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
614+
},
612615
"update_failed": {
613616
"message": "{endpoint} data request failed: {message}"
614617
}

tests/components/tesla_fleet/test_init.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from homeassistant.core import HomeAssistant
3636
from homeassistant.data_entry_flow import FlowResultType
3737
from homeassistant.helpers import device_registry as dr
38+
from homeassistant.helpers.config_entry_oauth2_flow import (
39+
ImplementationUnavailableError,
40+
)
3841

3942
from . import setup_platform
4043
from .conftest import create_config_entry
@@ -561,3 +564,20 @@ async def test_vehicle_with_location_scope(
561564
assert VehicleDataEndpoint.DRIVE_STATE in endpoints
562565
assert VehicleDataEndpoint.VEHICLE_STATE in endpoints
563566
assert VehicleDataEndpoint.VEHICLE_CONFIG in endpoints
567+
568+
569+
async def test_oauth_implementation_not_available(
570+
hass: HomeAssistant,
571+
normal_config_entry: MockConfigEntry,
572+
) -> None:
573+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
574+
normal_config_entry.add_to_hass(hass)
575+
576+
with patch(
577+
"homeassistant.components.tesla_fleet.async_get_config_entry_implementation",
578+
side_effect=ImplementationUnavailableError,
579+
):
580+
await hass.config_entries.async_setup(normal_config_entry.entry_id)
581+
await hass.async_block_till_done()
582+
583+
assert normal_config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)