diff --git a/homeassistant/components/youtube/__init__.py b/homeassistant/components/youtube/__init__.py index ec8a3f325cb..32863f5a772 100644 --- a/homeassistant/components/youtube/__init__.py +++ b/homeassistant/components/youtube/__init__.py @@ -10,6 +10,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, OAuth2Session, async_get_config_entry_implementation, ) @@ -23,7 +24,13 @@ PLATFORMS = [Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up YouTube from a config entry.""" - 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 session = OAuth2Session(hass, entry, implementation) auth = AsyncConfigEntryAuth(hass, session) try: diff --git a/homeassistant/components/youtube/strings.json b/homeassistant/components/youtube/strings.json index a9aee20e2ca..1ed8abc86fd 100644 --- a/homeassistant/components/youtube/strings.json +++ b/homeassistant/components/youtube/strings.json @@ -41,6 +41,11 @@ "views": { "name": "Views" } } }, + "exceptions": { + "oauth2_implementation_unavailable": { + "message": "OAuth2 implementation unavailable, will retry" + } + }, "options": { "step": { "init": { diff --git a/tests/components/youtube/test_init.py b/tests/components/youtube/test_init.py index 400ce515176..a7d2639902c 100644 --- a/tests/components/youtube/test_init.py +++ b/tests/components/youtube/test_init.py @@ -12,6 +12,9 @@ from homeassistant.components.youtube.const import CONF_CHANNELS 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 .conftest import GOOGLE_TOKEN_URI, ComponentSetup @@ -135,3 +138,19 @@ async def test_device_info( assert device.identifiers == {(DOMAIN, f"{entry.entry_id}_{channel_id}")} assert device.manufacturer == "Google, Inc." assert device.name == "Google for Developers" + + +async def test_oauth_implementation_not_available( + hass: HomeAssistant, setup_integration: ComponentSetup +) -> None: + """Test that unavailable OAuth implementation raises ConfigEntryNotReady.""" + entry = hass.config_entries.async_entries(DOMAIN)[0] + + with patch( + "homeassistant.components.youtube.async_get_config_entry_implementation", + side_effect=ImplementationUnavailableError, + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.SETUP_RETRY