diff --git a/homeassistant/components/myuplink/__init__.py b/homeassistant/components/myuplink/__init__.py index 407e4da5475..8eee978cf18 100644 --- a/homeassistant/components/myuplink/__init__.py +++ b/homeassistant/components/myuplink/__init__.py @@ -12,10 +12,12 @@ from myuplink import MyUplinkAPI, get_manufacturer, get_model, get_system_name from homeassistant.const import Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import ( - aiohttp_client, - config_entry_oauth2_flow, - device_registry as dr, +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, + OAuth2Session, + async_get_config_entry_implementation, ) from homeassistant.helpers.device_registry import DeviceEntry @@ -40,13 +42,16 @@ async def async_setup_entry( ) -> bool: """Set up myUplink from a config entry.""" - implementation = ( - await config_entry_oauth2_flow.async_get_config_entry_implementation( - hass, config_entry - ) - ) - session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, implementation) - auth = AsyncConfigEntryAuth(aiohttp_client.async_get_clientsession(hass), session) + try: + implementation = await async_get_config_entry_implementation(hass, config_entry) + except ImplementationUnavailableError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="implementation_unavailable", + ) from err + + session = OAuth2Session(hass, config_entry, implementation) + auth = AsyncConfigEntryAuth(async_get_clientsession(hass), session) try: await auth.async_get_access_token() diff --git a/homeassistant/components/myuplink/strings.json b/homeassistant/components/myuplink/strings.json index f9434b6b60a..807dbceb43f 100644 --- a/homeassistant/components/myuplink/strings.json +++ b/homeassistant/components/myuplink/strings.json @@ -56,6 +56,9 @@ "config_entry_not_ready": { "message": "Error while loading the integration." }, + "implementation_unavailable": { + "message": "OAuth2 implementation is not available, will retry." + }, "incorrect_oauth2_scope": { "message": "Stored permissions are invalid. Please login again to update permissions." }, diff --git a/tests/components/myuplink/test_init.py b/tests/components/myuplink/test_init.py index 891ba992772..4dff8bfa3e7 100644 --- a/tests/components/myuplink/test_init.py +++ b/tests/components/myuplink/test_init.py @@ -2,7 +2,7 @@ import http import time -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from aiohttp import ClientConnectionError import pytest @@ -12,6 +12,9 @@ from homeassistant.components.myuplink.const import DOMAIN, OAUTH2_TOKEN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, +) from homeassistant.setup import async_setup_component from . import setup_integration @@ -39,6 +42,24 @@ async def test_load_unload_entry( assert entry.state is ConfigEntryState.NOT_LOADED +async def test_oauth2_implementation_unavailable( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """Test OAuth2 implementation unavailable error.""" + + mock_config_entry.add_to_hass(hass) + + with patch( + f"homeassistant.components.{DOMAIN}.async_get_config_entry_implementation", + side_effect=ImplementationUnavailableError, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + @pytest.mark.parametrize( ("expires_at", "status", "expected_state"), [