mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Allow ConfigEntryNotReady to carry a retry delay (#183895)
Co-authored-by: Markus Tuominen <3738613+Markus98@users.noreply.github.com>
This commit is contained in:
co-authored by
Markus Tuominen
parent
e166a2e5e2
commit
211e93a7ef
@@ -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
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user