mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Avoid creating two tasks to setup components at startup (#110828)
This commit is contained in:
parent
85587f995b
commit
314123e0ab
@ -131,17 +131,23 @@ async def async_setup_component(
|
|||||||
if domain in hass.config.components:
|
if domain in hass.config.components:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
setup_tasks: dict[str, asyncio.Task[bool]] = hass.data.setdefault(DATA_SETUP, {})
|
setup_futures: dict[str, asyncio.Future[bool]] = hass.data.setdefault(
|
||||||
|
DATA_SETUP, {}
|
||||||
if domain in setup_tasks:
|
|
||||||
return await setup_tasks[domain]
|
|
||||||
|
|
||||||
task = setup_tasks[domain] = hass.async_create_task(
|
|
||||||
_async_setup_component(hass, domain, config), f"setup component {domain}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if existing_future := setup_futures.get(domain):
|
||||||
|
return await existing_future
|
||||||
|
|
||||||
|
future = hass.loop.create_future()
|
||||||
|
setup_futures[domain] = future
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return await task
|
result = await _async_setup_component(hass, domain, config)
|
||||||
|
future.set_result(result)
|
||||||
|
return result
|
||||||
|
except BaseException as err: # pylint: disable=broad-except
|
||||||
|
future.set_exception(err)
|
||||||
|
raise
|
||||||
finally:
|
finally:
|
||||||
if domain in hass.data.get(DATA_SETUP_DONE, {}):
|
if domain in hass.data.get(DATA_SETUP_DONE, {}):
|
||||||
hass.data[DATA_SETUP_DONE].pop(domain).set()
|
hass.data[DATA_SETUP_DONE].pop(domain).set()
|
||||||
|
@ -20,13 +20,7 @@ from homeassistant.const import (
|
|||||||
EVENT_HOMEASSISTANT_STARTED,
|
EVENT_HOMEASSISTANT_STARTED,
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
)
|
)
|
||||||
from homeassistant.core import (
|
from homeassistant.core import DOMAIN as HA_DOMAIN, CoreState, HomeAssistant, callback
|
||||||
DOMAIN as HA_DOMAIN,
|
|
||||||
CoreState,
|
|
||||||
Event,
|
|
||||||
HomeAssistant,
|
|
||||||
callback,
|
|
||||||
)
|
|
||||||
from homeassistant.data_entry_flow import BaseServiceInfo, FlowResult, FlowResultType
|
from homeassistant.data_entry_flow import BaseServiceInfo, FlowResult, FlowResultType
|
||||||
from homeassistant.exceptions import (
|
from homeassistant.exceptions import (
|
||||||
ConfigEntryAuthFailed,
|
ConfigEntryAuthFailed,
|
||||||
@ -46,6 +40,7 @@ from .common import (
|
|||||||
MockEntity,
|
MockEntity,
|
||||||
MockModule,
|
MockModule,
|
||||||
MockPlatform,
|
MockPlatform,
|
||||||
|
async_capture_events,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_config_flow,
|
mock_config_flow,
|
||||||
mock_integration,
|
mock_integration,
|
||||||
@ -2811,14 +2806,7 @@ async def test_async_setup_init_entry_completes_before_loaded_event_fires(
|
|||||||
hass: HomeAssistant, manager: config_entries.ConfigEntries
|
hass: HomeAssistant, manager: config_entries.ConfigEntries
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test a config entry being initialized during integration setup before the loaded event fires."""
|
"""Test a config entry being initialized during integration setup before the loaded event fires."""
|
||||||
load_events: list[Event] = []
|
load_events = async_capture_events(hass, EVENT_COMPONENT_LOADED)
|
||||||
|
|
||||||
@callback
|
|
||||||
def _record_load(event: Event) -> None:
|
|
||||||
nonlocal load_events
|
|
||||||
load_events.append(event)
|
|
||||||
|
|
||||||
listener = hass.bus.async_listen(EVENT_COMPONENT_LOADED, _record_load)
|
|
||||||
|
|
||||||
async def mock_async_setup(hass, config):
|
async def mock_async_setup(hass, config):
|
||||||
"""Mock setup."""
|
"""Mock setup."""
|
||||||
@ -2872,8 +2860,6 @@ async def test_async_setup_init_entry_completes_before_loaded_event_fires(
|
|||||||
assert len(entries) == 1
|
assert len(entries) == 1
|
||||||
assert entries[0].state is config_entries.ConfigEntryState.LOADED
|
assert entries[0].state is config_entries.ConfigEntryState.LOADED
|
||||||
|
|
||||||
listener()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_async_setup_update_entry(hass: HomeAssistant) -> None:
|
async def test_async_setup_update_entry(hass: HomeAssistant) -> None:
|
||||||
"""Test a config entry being updated during integration setup."""
|
"""Test a config entry being updated during integration setup."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user