diff --git a/homeassistant/components/vicare/__init__.py b/homeassistant/components/vicare/__init__.py index ac81cd4036f0..ff93abedea6e 100644 --- a/homeassistant/components/vicare/__init__.py +++ b/homeassistant/components/vicare/__init__.py @@ -10,10 +10,14 @@ from PyViCare.PyViCare import PyViCare from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareOAuthManager import obtain_token_via_basic_auth_pkce from PyViCare.PyViCareUtils import ( + PyViCareDeviceCommunicationError, + PyViCareInternalServerError, PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError, + PyViCareInvalidDataError, PyViCareRateLimitError, ) +import requests from homeassistant.components.application_credentials import ( ClientCredential, @@ -178,6 +182,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ViCareConfigEntry) -> bo raise ConfigEntryNotReady( f"ViCare API rate limit exceeded, resets at {err.limitResetDate}" ) from err + except ( + PyViCareDeviceCommunicationError, + PyViCareInternalServerError, + PyViCareInvalidDataError, + requests.RequestException, + ) as err: + # The coordinator treats the same errors as transient. + raise ConfigEntryNotReady("Unable to reach the ViCare API") from err # Group devices by gateway: in viaGateway mode one bulk fetch refreshes # every device behind a gateway, so one coordinator serves the gateway. diff --git a/tests/components/vicare/test_init.py b/tests/components/vicare/test_init.py index 15b6195dc6f7..ab144b4f779f 100644 --- a/tests/components/vicare/test_init.py +++ b/tests/components/vicare/test_init.py @@ -7,6 +7,7 @@ from aiohttp import ClientError from freezegun.api import FrozenDateTimeFactory import pytest from PyViCare.PyViCareUtils import ( + PyViCareDeviceCommunicationError, PyViCareInternalServerError, PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError, @@ -14,6 +15,7 @@ from PyViCare.PyViCareUtils import ( PyViCareNotSupportedFeatureError, PyViCareRateLimitError, ) +import requests from homeassistant.components.vicare.const import DEFAULT_CACHE_DURATION, DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState @@ -468,6 +470,43 @@ async def test_setup_entry_rate_limited( assert str(rate_limit_error.limitResetDate) in mock_config_entry.reason +@pytest.mark.parametrize( + "error", + [ + # From a real response, 2026-10-09T17:05:26Z. + PyViCareInternalServerError( + {"statusCode": 502, "message": "Non-JSON 502 response", "viErrorId": "n/a"} + ), + PyViCareDeviceCommunicationError( + {"extendedPayload": {"reason": "GATEWAY_OFFLINE"}} + ), + PyViCareInvalidDataError({"error": "no data"}), + requests.ConnectionError, + ], +) +async def test_setup_entry_transient_api_error( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + error: Exception | type[Exception], +) -> None: + """Test setup retries on a ViCare API error that resolves on its own.""" + mock_config_entry.add_to_hass(hass) + client = MockPyViCare([]) + client.initWithExternalOAuth.side_effect = error + + with ( + patch( + "homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session.async_ensure_token_valid", + ), + patch(f"{MODULE}.PyViCare", return_value=client), + ): + 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 + assert mock_config_entry.reason == "Unable to reach the ViCare API" + + async def test_setup_entry_invalid_configuration( hass: HomeAssistant, mock_config_entry: MockConfigEntry,