Ensure config entries are not unloaded while their platforms are setting up (#118767)

* Report non-awaited/non-locked config entry platform forwards

Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.

In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.

If config platform forwards are happening during setup, they should be awaited

If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup

* Report non-awaited/non-locked config entry platform forwards

Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.

In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.

If config platform forwards are happening during setup, they should be awaited

If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup

* run with error on to find them

* cert_exp, hold lock

* cert_exp, hold lock

* shelly async_late_forward_entry_setups

* compact

* compact

* found another

* patch up mobileapp

* patch up hue tests

* patch up smartthings

* fix mqtt

* fix esphome

* zwave_js

* mqtt

* rework

* fixes

* fix mocking

* fix mocking

* do not call async_forward_entry_setup directly

* docstrings

* docstrings

* docstrings

* add comments

* doc strings

* fixed all in core, turn off strict

* coverage

* coverage

* missing

* coverage
This commit is contained in:
J. Nick Koston 2024-06-04 20:34:39 -05:00 committed by GitHub
parent 67b3be8432
commit ed0568c655
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 457 additions and 111 deletions

View file

@ -15,7 +15,7 @@ from homeassistant.components.assist_pipeline.select import (
VadSensitivitySelect,
)
from homeassistant.components.assist_pipeline.vad import VadSensitivity
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
@ -49,9 +49,11 @@ class SelectPlatform(MockPlatform):
async def init_select(hass: HomeAssistant, init_components) -> ConfigEntry:
"""Initialize select entity."""
mock_platform(hass, "assist_pipeline.select", SelectPlatform())
config_entry = MockConfigEntry(domain="assist_pipeline")
config_entry = MockConfigEntry(
domain="assist_pipeline", state=ConfigEntryState.LOADED
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_forward_entry_setup(config_entry, "select")
await hass.config_entries.async_late_forward_entry_setups(config_entry, ["select"])
return config_entry
@ -123,13 +125,14 @@ async def test_select_entity_registering_device(
async def test_select_entity_changing_pipelines(
hass: HomeAssistant,
init_select: ConfigEntry,
init_select: MockConfigEntry,
pipeline_1: Pipeline,
pipeline_2: Pipeline,
pipeline_storage: PipelineStorageCollection,
) -> None:
"""Test entity tracking pipeline changes."""
config_entry = init_select # nicer naming
config_entry.mock_state(hass, ConfigEntryState.LOADED)
state = hass.states.get("select.assist_pipeline_test_prefix_pipeline")
assert state is not None
@ -158,7 +161,7 @@ async def test_select_entity_changing_pipelines(
# Reload config entry to test selected option persists
assert await hass.config_entries.async_forward_entry_unload(config_entry, "select")
assert await hass.config_entries.async_forward_entry_setup(config_entry, "select")
await hass.config_entries.async_late_forward_entry_setups(config_entry, ["select"])
state = hass.states.get("select.assist_pipeline_test_prefix_pipeline")
assert state is not None
@ -179,10 +182,11 @@ async def test_select_entity_changing_pipelines(
async def test_select_entity_changing_vad_sensitivity(
hass: HomeAssistant,
init_select: ConfigEntry,
init_select: MockConfigEntry,
) -> None:
"""Test entity tracking pipeline changes."""
config_entry = init_select # nicer naming
config_entry.mock_state(hass, ConfigEntryState.LOADED)
state = hass.states.get("select.assist_pipeline_test_vad_sensitivity")
assert state is not None
@ -205,7 +209,7 @@ async def test_select_entity_changing_vad_sensitivity(
# Reload config entry to test selected option persists
assert await hass.config_entries.async_forward_entry_unload(config_entry, "select")
assert await hass.config_entries.async_forward_entry_setup(config_entry, "select")
await hass.config_entries.async_late_forward_entry_setups(config_entry, ["select"])
state = hass.states.get("select.assist_pipeline_test_vad_sensitivity")
assert state is not None