diff --git a/homeassistant/components/aladdin_connect/__init__.py b/homeassistant/components/aladdin_connect/__init__.py index 2ea8ceef834b..516988da4510 100644 --- a/homeassistant/components/aladdin_connect/__init__.py +++ b/homeassistant/components/aladdin_connect/__init__.py @@ -5,7 +5,12 @@ from genie_partner_sdk.client import AladdinConnectClient from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.exceptions import ( + ConfigEntryAuthFailed, + ConfigEntryNotReady, + OAuth2TokenRequestError, + OAuth2TokenRequestReauthError, +) from homeassistant.helpers import ( aiohttp_client, config_entry_oauth2_flow, @@ -42,11 +47,9 @@ async def async_setup_entry( try: await session.async_ensure_token_valid() - except aiohttp.ClientResponseError as err: - if 400 <= err.status < 500: - raise ConfigEntryAuthFailed(err) from err - raise ConfigEntryNotReady from err - except aiohttp.ClientError as err: + except OAuth2TokenRequestReauthError as err: + raise ConfigEntryAuthFailed(err) from err + except (OAuth2TokenRequestError, aiohttp.ClientError) as err: raise ConfigEntryNotReady from err client = AladdinConnectClient( diff --git a/tests/components/aladdin_connect/test_init.py b/tests/components/aladdin_connect/test_init.py index f3cd6042f326..0fb40a0acfaa 100644 --- a/tests/components/aladdin_connect/test_init.py +++ b/tests/components/aladdin_connect/test_init.py @@ -11,6 +11,10 @@ import pytest from homeassistant.components.aladdin_connect import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ( + OAuth2TokenRequestError, + OAuth2TokenRequestReauthError, +) from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.config_entry_oauth2_flow import ( ImplementationUnavailableError, @@ -59,25 +63,37 @@ async def test_unload_entry( @pytest.mark.parametrize( - ("status", "expected_state"), + ("exc", "expected_state"), [ - (http.HTTPStatus.UNAUTHORIZED, ConfigEntryState.SETUP_ERROR), - (http.HTTPStatus.INTERNAL_SERVER_ERROR, ConfigEntryState.SETUP_RETRY), + ( + OAuth2TokenRequestReauthError( + request_info=RequestInfo("", "POST", {}, ""), + status=http.HTTPStatus.UNAUTHORIZED, + domain=DOMAIN, + ), + ConfigEntryState.SETUP_ERROR, + ), + ( + OAuth2TokenRequestError( + request_info=RequestInfo("", "POST", {}, ""), + status=http.HTTPStatus.INTERNAL_SERVER_ERROR, + domain=DOMAIN, + ), + ConfigEntryState.SETUP_RETRY, + ), ], ids=["auth_failure", "server_error"], ) async def test_setup_entry_token_error( hass: HomeAssistant, mock_config_entry: MockConfigEntry, - status: http.HTTPStatus, + exc: OAuth2TokenRequestError, expected_state: ConfigEntryState, ) -> None: """Test setup entry fails when token validation fails.""" with patch( "homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session.async_ensure_token_valid", - side_effect=ClientResponseError( - RequestInfo("", "POST", {}, ""), None, status=status - ), + side_effect=exc, ): await init_integration(hass, mock_config_entry)