From 211e93a7ef4e86cdc5aa364ca620cdfb845c33db Mon Sep 17 00:00:00 2001 From: Christian Lackas Date: Fri, 9 Oct 2026 17:09:24 +0200 Subject: [PATCH] Allow ConfigEntryNotReady to carry a retry delay (#183895) Co-authored-by: Markus Tuominen <3738613+Markus98@users.noreply.github.com> --- homeassistant/config_entries.py | 10 +++++++++- homeassistant/exceptions.py | 12 ++++++++++++ tests/test_config_entries.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index e6a4d4a6e4d0..fc02393b91b8 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -143,6 +143,7 @@ SAVE_DELAY = 1 DISCOVERY_COOLDOWN = 1 SETUP_RETRY_MAX_WAIT = 600 # 10 minutes +SETUP_RETRY_AFTER_MAX_WAIT = 86400 # 1 day ISSUE_UNIQUE_ID_COLLISION = "config_entry_unique_id_collision" UNIQUE_ID_COLLISION_TITLE_LIMIT = 5 @@ -802,7 +803,14 @@ class ConfigEntry[_DataT = Any]: reason.translation_placeholders, reason.translation_domain, ) - wait_time = min(2**self._tries * 5, SETUP_RETRY_MAX_WAIT) + ( + wait_time = min(2**self._tries * 5, SETUP_RETRY_MAX_WAIT) + if exc.retry_after is not None: + # The backoff stays the floor, so a delay that has already + # passed does not retry immediately. + wait_time = max( + wait_time, min(exc.retry_after, SETUP_RETRY_AFTER_MAX_WAIT) + ) + wait_time += ( randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000 ) self._tries += 1 diff --git a/homeassistant/exceptions.py b/homeassistant/exceptions.py index 4682bbfa1178..13ece7e3cf15 100644 --- a/homeassistant/exceptions.py +++ b/homeassistant/exceptions.py @@ -248,6 +248,18 @@ class ConfigEntryError(IntegrationError): class ConfigEntryNotReady(IntegrationError): """Error to indicate that config entry is not ready.""" + retry_after: float | None = None + + def __init__( + self, + *args: object, + retry_after: float | None = None, + **kwargs: Any, + ) -> None: + """Initialize exception.""" + super().__init__(*args, **kwargs) + self.retry_after = retry_after + class ConfigEntryAuthFailed(IntegrationError): """Error to indicate that config entry could not authenticate.""" diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index bb3a9f2cdc68..78f7e43813f2 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -2067,6 +2067,38 @@ async def test_setup_not_ready_exponential_backoff( assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY +@pytest.mark.parametrize( + ("retry_after", "expected_wait"), + [ + pytest.param(3600, 3600, id="honored"), + pytest.param(1, 5, id="backoff_is_floor"), + pytest.param(-60, 5, id="already_passed"), + pytest.param(10**9, 86400, id="capped_at_one_day"), + ], +) +async def test_setup_not_ready_retry_after( + hass: HomeAssistant, + manager: config_entries.ConfigEntries, + retry_after: float, + expected_wait: int, +) -> None: + """Test setup retry honors retry_after between the backoff and one day.""" + entry = MockConfigEntry(domain="test") + entry.add_to_hass(hass) + + mock_setup_entry = AsyncMock( + side_effect=ConfigEntryNotReady(retry_after=retry_after) + ) + mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry)) + mock_platform(hass, "test.config_flow", None) + + with patch("homeassistant.config_entries.async_call_later") as mock_call: + await manager.async_setup(entry.entry_id) + + assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY + assert int(mock_call.call_args.args[1]) == expected_wait + + async def test_setup_raise_not_ready_from_exception( hass: HomeAssistant, manager: config_entries.ConfigEntries,