From 7aced2522aa82dd6fe35cd841317fcbdac5f7948 Mon Sep 17 00:00:00 2001 From: Will Moss Date: Sun, 9 Nov 2025 08:44:44 -0800 Subject: [PATCH] Use error introduced in #154579 in yolink integration (#156092) --- homeassistant/components/yolink/__init__.py | 20 ++++++++++------ homeassistant/components/yolink/strings.json | 3 +++ tests/components/yolink/test_config_flow.py | 3 +-- tests/components/yolink/test_init.py | 24 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/yolink/__init__.py b/homeassistant/components/yolink/__init__.py index 65ca84863f1..54a903302d3 100644 --- a/homeassistant/components/yolink/__init__.py +++ b/homeassistant/components/yolink/__init__.py @@ -19,10 +19,14 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import ( aiohttp_client, - config_entry_oauth2_flow, config_validation as cv, device_registry as dr, ) +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, + OAuth2Session, + async_get_config_entry_implementation, +) from homeassistant.helpers.typing import ConfigType from . import api @@ -120,13 +124,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up yolink from a config entry.""" hass.data.setdefault(DOMAIN, {}) - implementation = ( - await config_entry_oauth2_flow.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 = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation) + session = OAuth2Session(hass, entry, implementation) auth_mgr = api.ConfigEntryAuth( hass, aiohttp_client.async_get_clientsession(hass), session diff --git a/homeassistant/components/yolink/strings.json b/homeassistant/components/yolink/strings.json index 2f2393cb7f5..b6e6b48e661 100644 --- a/homeassistant/components/yolink/strings.json +++ b/homeassistant/components/yolink/strings.json @@ -133,6 +133,9 @@ "invalid_config_entry": { "message": "Config entry not found or not loaded!" }, + "oauth2_implementation_unavailable": { + "message": "OAuth2 implementation temporarily unavailable, will retry" + }, "valve_inoperable_currently": { "message": "The Valve cannot be operated currently." } diff --git a/tests/components/yolink/test_config_flow.py b/tests/components/yolink/test_config_flow.py index 1dd71368d73..df7f420ad4b 100644 --- a/tests/components/yolink/test_config_flow.py +++ b/tests/components/yolink/test_config_flow.py @@ -130,8 +130,7 @@ async def test_abort_if_authorization_timeout(hass: HomeAssistant) -> None: application_credentials.ClientCredential(CLIENT_ID, CLIENT_SECRET), ) with patch( - "homeassistant.components.yolink.config_entry_oauth2_flow." - "LocalOAuth2Implementation.async_generate_authorize_url", + "homeassistant.helpers.config_entry_oauth2_flow.LocalOAuth2Implementation.async_generate_authorize_url", side_effect=TimeoutError, ): result = await hass.config_entries.flow.async_init( diff --git a/tests/components/yolink/test_init.py b/tests/components/yolink/test_init.py index 11d0528dcce..eb91786cda4 100644 --- a/tests/components/yolink/test_init.py +++ b/tests/components/yolink/test_init.py @@ -1,10 +1,17 @@ """Tests for the yolink integration.""" +from unittest.mock import patch + import pytest from homeassistant.components.yolink import DOMAIN +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 tests.common import MockConfigEntry @@ -36,3 +43,20 @@ async def test_device_remove_devices( device_registry, mock_config_entry.entry_id ) assert len(device_entries) == 0 + + +@pytest.mark.usefixtures("setup_credentials", "mock_auth_manager", "mock_yolink_home") +async def test_oauth_implementation_not_available( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> None: + """Test that an unavailable OAuth implementation raises ConfigEntryNotReady.""" + assert await async_setup_component(hass, "cloud", {}) + + with patch( + "homeassistant.components.yolink.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