From eb988f77921be9fbc9f6dab1c96f9569dcd27107 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 23 May 2022 09:03:30 +0200 Subject: [PATCH] Fix race in MQTT platform setup (#72344) Make sure MQTT platforms setup locks discovery --- homeassistant/components/mqtt/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index dff5e75fa23..9191b22064c 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -816,14 +816,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock() hass.data[CONFIG_ENTRY_IS_SETUP] = set() - async with hass.data[DATA_CONFIG_ENTRY_LOCK]: - for component in PLATFORMS: - config_entries_key = f"{component}.mqtt" - if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]: - hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key) - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, component) - ) + async def async_forward_entry_setup(): + """Forward the config entry setup to the platforms.""" + async with hass.data[DATA_CONFIG_ENTRY_LOCK]: + for component in PLATFORMS: + config_entries_key = f"{component}.mqtt" + if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]: + hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key) + await hass.config_entries.async_forward_entry_setup( + entry, component + ) + + hass.async_create_task(async_forward_entry_setup()) if conf.get(CONF_DISCOVERY): await _async_setup_discovery(hass, conf, entry)