diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index f9a6ebd025f..417d1400758 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -236,9 +236,7 @@ async def _async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) - await _async_setup_discovery(hass, mqtt_client.conf, entry) -async def async_setup_entry( # noqa: C901 - hass: HomeAssistant, entry: ConfigEntry -) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Load a config entry.""" # Merge basic configuration, and add missing defaults for basic options _merge_basic_config(hass, entry, hass.data.get(DATA_MQTT_CONFIG, {})) @@ -386,25 +384,33 @@ async def async_setup_entry( # noqa: C901 hass.data[DATA_MQTT_UPDATED_CONFIG] = config_yaml.get(DOMAIN, {}) async_dispatcher_send(hass, MQTT_RELOADED) - 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 - ) + async def async_forward_entry_setup_and_setup_discovery(config_entry): + """Forward the config entry setup to the platforms and set up discovery.""" + # Local import to avoid circular dependencies + # pylint: disable-next=import-outside-toplevel + from . import device_automation, tag + + await asyncio.gather( + *( + [ + device_automation.async_setup_entry(hass, config_entry), + tag.async_setup_entry(hass, config_entry), + ] + + [ + hass.config_entries.async_forward_entry_setup(entry, component) + for component in PLATFORMS + ] + ) + ) + # Setup discovery + if conf.get(CONF_DISCOVERY): + await _async_setup_discovery(hass, conf, entry) # Setup reload service after all platforms have loaded entry.async_on_unload( hass.bus.async_listen("event_mqtt_reloaded", _async_reload_platforms) ) - hass.async_create_task(async_forward_entry_setup()) - - if conf.get(CONF_DISCOVERY): - await _async_setup_discovery(hass, conf, entry) + hass.async_create_task(async_forward_entry_setup_and_setup_discovery(entry)) if DATA_MQTT_RELOAD_NEEDED in hass.data: hass.data.pop(DATA_MQTT_RELOAD_NEEDED) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 3bbf8ef61ad..04dd2d7917f 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -27,8 +27,6 @@ from .const import ( ATTR_DISCOVERY_TOPIC, CONF_AVAILABILITY, CONF_TOPIC, - CONFIG_ENTRY_IS_SETUP, - DATA_CONFIG_ENTRY_LOCK, DOMAIN, ) @@ -227,28 +225,6 @@ async def async_start( # noqa: C901 # Add component _LOGGER.info("Found new component: %s %s", component, discovery_id) hass.data[ALREADY_DISCOVERED][discovery_hash] = None - - config_entries_key = f"{component}.mqtt" - async with hass.data[DATA_CONFIG_ENTRY_LOCK]: - if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]: - if component == "device_automation": - # Local import to avoid circular dependencies - # pylint: disable-next=import-outside-toplevel - from . import device_automation - - await device_automation.async_setup_entry(hass, config_entry) - elif component == "tag": - # Local import to avoid circular dependencies - # pylint: disable-next=import-outside-toplevel - from . import tag - - await tag.async_setup_entry(hass, config_entry) - else: - await hass.config_entries.async_forward_entry_setup( - config_entry, component - ) - hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key) - async_dispatcher_send( hass, MQTT_DISCOVERY_NEW.format(component, "mqtt"), payload ) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 861b50d2f71..8159933a9a4 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -1780,13 +1780,12 @@ async def test_setup_entry_with_config_override( # mqtt present in yaml config assert await async_setup_component(hass, mqtt.DOMAIN, {}) await hass.async_block_till_done() - await mqtt_mock_entry_with_yaml_config() # User sets up a config entry entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}) entry.add_to_hass(hass) - with patch("homeassistant.components.mqtt.PLATFORMS", []): - assert await hass.config_entries.async_setup(entry.entry_id) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() # Discover a device to verify the entry was setup correctly async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)