diff --git a/homeassistant/components/smappee/__init__.py b/homeassistant/components/smappee/__init__.py index 7fa30965aa8..372441ec586 100644 --- a/homeassistant/components/smappee/__init__.py +++ b/homeassistant/components/smappee/__init__.py @@ -11,6 +11,7 @@ from homeassistant.const import ( CONF_PLATFORM, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.util import Throttle @@ -94,11 +95,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmappeeConfigEntry) -> b ) await hass.async_add_executor_job(smappee.load_local_service_location) else: - implementation = ( - await config_entry_oauth2_flow.async_get_config_entry_implementation( - hass, entry + try: + implementation = ( + await config_entry_oauth2_flow.async_get_config_entry_implementation( + hass, entry + ) ) - ) + except config_entry_oauth2_flow.ImplementationUnavailableError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="oauth2_implementation_unavailable", + ) from err smappee_api = api.ConfigEntrySmappeeApi(hass, entry, implementation) diff --git a/homeassistant/components/smappee/strings.json b/homeassistant/components/smappee/strings.json index d1f333ffcdc..0df1c68ecba 100644 --- a/homeassistant/components/smappee/strings.json +++ b/homeassistant/components/smappee/strings.json @@ -43,5 +43,10 @@ "title": "Discovered Smappee device" } } + }, + "exceptions": { + "oauth2_implementation_unavailable": { + "message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]" + } } } diff --git a/tests/components/smappee/test_init.py b/tests/components/smappee/test_init.py index cc752964a27..b419662d0fc 100644 --- a/tests/components/smappee/test_init.py +++ b/tests/components/smappee/test_init.py @@ -3,8 +3,11 @@ from unittest.mock import patch from homeassistant.components.smappee.const import DOMAIN -from homeassistant.config_entries import SOURCE_ZEROCONF +from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF, ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, +) from tests.common import MockConfigEntry @@ -38,3 +41,31 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None: await hass.config_entries.async_unload(entry.entry_id) await hass.async_block_till_done() assert not hass.data.get(DOMAIN) + + +async def test_oauth_implementation_not_available(hass: HomeAssistant) -> None: + """Test that unavailable OAuth implementation raises ConfigEntryNotReady.""" + config_entry = MockConfigEntry( + domain=DOMAIN, + unique_id="smappeeCloud", + source=SOURCE_USER, + data={ + "auth_implementation": DOMAIN, + "token": { + "access_token": "mock-access-token", + "refresh_token": "mock-refresh-token", + "expires_at": 9999999999, + "token_type": "Bearer", + }, + }, + ) + config_entry.add_to_hass(hass) + + with patch( + "homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation", + side_effect=ImplementationUnavailableError, + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.SETUP_RETRY