diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 1392b72f16b..9338c28da1c 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -37,8 +37,10 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, OAuth2Session, async_get_config_entry_implementation, ) @@ -104,7 +106,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: WithingsConfigEntry) -> ) session = async_get_clientsession(hass) client = WithingsClient(session=session) - implementation = await async_get_config_entry_implementation(hass, entry) + try: + implementation = await async_get_config_entry_implementation(hass, entry) + except ImplementationUnavailableError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="oauth2_implementation_unavailable", + ) from err oauth_session = OAuth2Session(hass, entry, implementation) refresh_lock = asyncio.Lock() diff --git a/homeassistant/components/withings/strings.json b/homeassistant/components/withings/strings.json index 8d442005666..a8559b72172 100644 --- a/homeassistant/components/withings/strings.json +++ b/homeassistant/components/withings/strings.json @@ -331,5 +331,10 @@ } } } + }, + "exceptions": { + "oauth2_implementation_unavailable": { + "message": "OAuth2 implementation unavailable, will retry" + } } } diff --git a/tests/components/withings/test_init.py b/tests/components/withings/test_init.py index e71402b8a98..86c4f1d8cbb 100644 --- a/tests/components/withings/test_init.py +++ b/tests/components/withings/test_init.py @@ -22,9 +22,13 @@ from homeassistant.components import cloud from homeassistant.components.cloud import CloudNotAvailable from homeassistant.components.webhook import async_generate_url from homeassistant.components.withings.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, +) from homeassistant.util import dt as dt_util from . import call_webhook, prepare_webhook_setup, setup_integration @@ -643,3 +647,20 @@ async def test_devices( device = device_registry.async_get_device({(DOMAIN, device_id)}) assert device is not None assert device == snapshot(name=device_id) + + +async def test_oauth_implementation_not_available( + hass: HomeAssistant, + webhook_config_entry: MockConfigEntry, +) -> None: + """Test that unavailable OAuth implementation raises ConfigEntryNotReady.""" + webhook_config_entry.add_to_hass(hass) + + with patch( + "homeassistant.components.withings.async_get_config_entry_implementation", + side_effect=ImplementationUnavailableError, + ): + await hass.config_entries.async_setup(webhook_config_entry.entry_id) + await hass.async_block_till_done() + + assert webhook_config_entry.state is ConfigEntryState.SETUP_RETRY