Prevent setup retry from delaying shutdown (#116328)

This commit is contained in:
J. Nick Koston 2024-04-28 08:13:51 -05:00 committed by GitHub
parent 76639252c9
commit 3f0c0a72db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 23 additions and 19 deletions

View file

@ -698,7 +698,7 @@ class ConfigEntry:
# Check again when we fire in case shutdown # Check again when we fire in case shutdown
# has started so we do not block shutdown # has started so we do not block shutdown
if not hass.is_stopping: if not hass.is_stopping:
hass.async_create_task( hass.async_create_background_task(
self._async_setup_retry(hass), self._async_setup_retry(hass),
f"config entry retry {self.domain} {self.title}", f"config entry retry {self.domain} {self.title}",
eager_start=True, eager_start=True,

View file

@ -57,6 +57,6 @@ async def test_setup_retry(
mock_client.read_char.side_effect = original_read_char mock_client.read_char.side_effect = original_read_char
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert mock_entry.state is ConfigEntryState.LOADED assert mock_entry.state is ConfigEntryState.LOADED

View file

@ -203,6 +203,7 @@ async def test_ecobee3_setup_connection_failure(
# We just advance time by 5 minutes so that the retry happens, rather # We just advance time by 5 minutes so that the retry happens, rather
# than manually invoking async_setup_entry. # than manually invoking async_setup_entry.
await time_changed(hass, 5 * 60) await time_changed(hass, 5 * 60)
await hass.async_block_till_done(wait_background_tasks=True)
climate = entity_registry.async_get("climate.homew") climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16" assert climate.unique_id == "00:00:00:00:00:00_1_16"

View file

@ -160,7 +160,7 @@ async def test_offline_device_raises(hass: HomeAssistant, controller) -> None:
is_connected = True is_connected = True
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
assert hass.states.get("light.testdevice").state == STATE_OFF assert hass.states.get("light.testdevice").state == STATE_OFF
@ -217,16 +217,18 @@ async def test_ble_device_only_checks_is_available(
is_available = True is_available = True
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10)) async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
assert hass.states.get("light.testdevice").state == STATE_OFF assert hass.states.get("light.testdevice").state == STATE_OFF
is_available = False is_available = False
async_fire_time_changed(hass, utcnow() + timedelta(hours=1)) async_fire_time_changed(hass, utcnow() + timedelta(hours=1))
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get("light.testdevice").state == STATE_UNAVAILABLE assert hass.states.get("light.testdevice").state == STATE_UNAVAILABLE
is_available = True is_available = True
async_fire_time_changed(hass, utcnow() + timedelta(hours=1)) async_fire_time_changed(hass, utcnow() + timedelta(hours=1))
await hass.async_block_till_done(wait_background_tasks=True)
assert hass.states.get("light.testdevice").state == STATE_OFF assert hass.states.get("light.testdevice").state == STATE_OFF

View file

@ -74,7 +74,7 @@ async def test_vehicle_first_refresh(
# Wait for the retry # Wait for the retry
freezer.tick(timedelta(seconds=60)) freezer.tick(timedelta(seconds=60))
async_fire_time_changed(hass) async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
# Verify we have loaded # Verify we have loaded
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED

View file

@ -32,9 +32,9 @@ async def test_setup_retry(hass: HomeAssistant) -> None:
bulb.getMac = AsyncMock(return_value=FAKE_MAC) bulb.getMac = AsyncMock(return_value=FAKE_MAC)
with _patch_discovery(), _patch_wizlight(device=bulb): with _patch_discovery(), _patch_wizlight(device=bulb):
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
async_fire_time_changed(hass, utcnow() + datetime.timedelta(minutes=15)) async_fire_time_changed(hass, utcnow() + datetime.timedelta(minutes=15))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED

View file

@ -69,7 +69,7 @@ async def test_ip_changes_fallback_discovery(hass: HomeAssistant) -> None:
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
# The discovery should update the ip address # The discovery should update the ip address
assert config_entry.data[CONF_HOST] == IP_ADDRESS assert config_entry.data[CONF_HOST] == IP_ADDRESS
@ -78,7 +78,7 @@ async def test_ip_changes_fallback_discovery(hass: HomeAssistant) -> None:
with patch(f"{MODULE}.AsyncBulb", return_value=mocked_bulb), _patch_discovery(): with patch(f"{MODULE}.AsyncBulb", return_value=mocked_bulb), _patch_discovery():
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
binary_sensor_entity_id = ENTITY_BINARY_SENSOR_TEMPLATE.format( binary_sensor_entity_id = ENTITY_BINARY_SENSOR_TEMPLATE.format(
@ -362,7 +362,7 @@ async def test_bulb_off_while_adding_in_ha(hass: HomeAssistant) -> None:
_patch_discovery_interval(), _patch_discovery_interval(),
): ):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
@ -380,7 +380,7 @@ async def test_async_listen_error_late_discovery(
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
await hass.async_block_till_done() await hass.async_block_till_done()
@ -388,7 +388,7 @@ async def test_async_listen_error_late_discovery(
with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()): with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
assert config_entry.data[CONF_DETECTED_MODEL] == MODEL assert config_entry.data[CONF_DETECTED_MODEL] == MODEL
@ -411,7 +411,7 @@ async def test_fail_to_fetch_initial_state(
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
await hass.async_block_till_done() await hass.async_block_till_done()
@ -419,7 +419,7 @@ async def test_fail_to_fetch_initial_state(
with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()): with _patch_discovery(), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
@ -502,7 +502,7 @@ async def test_async_setup_with_missing_id(hass: HomeAssistant) -> None:
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert config_entry.data[CONF_ID] == ID assert config_entry.data[CONF_ID] == ID
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
with ( with (
_patch_discovery(), _patch_discovery(),
@ -511,7 +511,7 @@ async def test_async_setup_with_missing_id(hass: HomeAssistant) -> None:
patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()),
): ):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
@ -535,7 +535,7 @@ async def test_async_setup_with_missing_unique_id(hass: HomeAssistant) -> None:
assert config_entry.state is ConfigEntryState.SETUP_RETRY assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert config_entry.unique_id == ID assert config_entry.unique_id == ID
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
with ( with (
_patch_discovery(), _patch_discovery(),
@ -544,7 +544,7 @@ async def test_async_setup_with_missing_unique_id(hass: HomeAssistant) -> None:
patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()), patch(f"{MODULE}.AsyncBulb", return_value=_mocked_bulb()),
): ):
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4))
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED

View file

@ -255,10 +255,11 @@ async def test_zha_retry_unique_ids(
lambda hass, delay, action: async_call_later(hass, 0, action), lambda hass, delay, action: async_call_later(hass, 0, action),
): ):
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done(wait_background_tasks=True)
# Wait for the config entry setup to retry # Wait for the config entry setup to retry
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_connect.mock_calls) == 2 assert len(mock_connect.mock_calls) == 2