Skip to content

Commit 1bd7045

Browse files
authored
Improved error handling for oauth2 configuration in onedrive integration (home-assistant#156216)
1 parent dbc53b9 commit 1bd7045

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

homeassistant/components/onedrive/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from homeassistant.helpers import config_validation as cv
2323
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2424
from homeassistant.helpers.config_entry_oauth2_flow import (
25+
ImplementationUnavailableError,
2526
OAuth2Session,
2627
async_get_config_entry_implementation,
2728
)
@@ -189,7 +190,13 @@ async def _get_onedrive_client(
189190
hass: HomeAssistant, entry: OneDriveConfigEntry
190191
) -> tuple[OneDriveClient, Callable[[], Awaitable[str]]]:
191192
"""Get OneDrive client."""
192-
implementation = await async_get_config_entry_implementation(hass, entry)
193+
try:
194+
implementation = await async_get_config_entry_implementation(hass, entry)
195+
except ImplementationUnavailableError as err:
196+
raise ConfigEntryNotReady(
197+
translation_domain=DOMAIN,
198+
translation_key="oauth2_implementation_unavailable",
199+
) from err
193200
session = OAuth2Session(hass, entry, implementation)
194201

195202
async def get_access_token() -> str:

homeassistant/components/onedrive/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
"no_access_to_path": {
109109
"message": "Cannot read {filename}, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`"
110110
},
111+
"oauth2_implementation_unavailable": {
112+
"message": "OAuth2 implementation unavailable, will retry"
113+
},
111114
"update_failed": {
112115
"message": "Failed to update drive state"
113116
},

tests/components/onedrive/test_init.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from copy import copy
44
from html import escape
55
from json import dumps
6-
from unittest.mock import MagicMock
6+
from unittest.mock import MagicMock, patch
77

88
from onedrive_personal_sdk.const import DriveState
99
from onedrive_personal_sdk.exceptions import (
@@ -23,6 +23,9 @@
2323
from homeassistant.config_entries import ConfigEntryState
2424
from homeassistant.core import HomeAssistant
2525
from homeassistant.helpers import device_registry as dr, issue_registry as ir
26+
from homeassistant.helpers.config_entry_oauth2_flow import (
27+
ImplementationUnavailableError,
28+
)
2629

2730
from . import setup_integration
2831
from .const import BACKUP_METADATA, INSTANCE_ID
@@ -298,3 +301,20 @@ async def test_migration_guard_against_major_downgrade(
298301

299302
await setup_integration(hass, old_config_entry)
300303
assert old_config_entry.state is ConfigEntryState.MIGRATION_ERROR
304+
305+
306+
async def test_oauth_implementation_not_available(
307+
hass: HomeAssistant,
308+
mock_config_entry: MockConfigEntry,
309+
) -> None:
310+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
311+
mock_config_entry.add_to_hass(hass)
312+
313+
with patch(
314+
"homeassistant.components.onedrive.async_get_config_entry_implementation",
315+
side_effect=ImplementationUnavailableError,
316+
):
317+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
318+
await hass.async_block_till_done()
319+
320+
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)