From 4296f227cfed08fbc9ab136bb27369b8577f3969 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Dec 2022 15:59:42 -1000 Subject: [PATCH] Fix thundering heard in setup_again when there are many integrations (#84670) --- homeassistant/config_entries.py | 11 +++++++++-- tests/test_config_entries.py | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 0f3fb0a3aea..f2e2be97e42 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -9,6 +9,7 @@ from copy import deepcopy from enum import Enum import functools import logging +from random import randint from types import MappingProxyType, MethodType from typing import TYPE_CHECKING, Any, Optional, TypeVar, cast import weakref @@ -27,7 +28,11 @@ from .exceptions import ( ) from .helpers import device_registry, entity_registry, storage from .helpers.dispatcher import async_dispatcher_send -from .helpers.event import async_call_later +from .helpers.event import ( + RANDOM_MICROSECOND_MAX, + RANDOM_MICROSECOND_MIN, + async_call_later, +) from .helpers.frame import report from .helpers.typing import UNDEFINED, ConfigType, DiscoveryInfoType, UndefinedType from .setup import DATA_SETUP_DONE, async_process_deps_reqs, async_setup_component @@ -409,7 +414,9 @@ class ConfigEntry: result = False except ConfigEntryNotReady as ex: self.async_set_state(hass, ConfigEntryState.SETUP_RETRY, str(ex) or None) - wait_time = 2 ** min(tries, 4) * 5 + wait_time = 2 ** min(tries, 4) * 5 + ( + randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000 + ) tries += 1 message = str(ex) ready_message = f"ready yet: {message}" if message else "ready yet" diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index b1e3fd760d5..994c220adc4 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -903,7 +903,7 @@ async def test_setup_raise_not_ready(hass, caplog): p_hass, p_wait_time, p_setup = mock_call.mock_calls[0][1] assert p_hass is hass - assert p_wait_time == 5 + assert 5 <= p_wait_time <= 5.5 assert entry.state is config_entries.ConfigEntryState.SETUP_RETRY assert entry.reason == "The internet connection is offline"