diff --git a/homeassistant/components/mqtt/util.py b/homeassistant/components/mqtt/util.py index 53462f87321..43efc3b8928 100644 --- a/homeassistant/components/mqtt/util.py +++ b/homeassistant/components/mqtt/util.py @@ -53,14 +53,12 @@ async def async_forward_entry_setup_and_setup_discovery( mqtt_data = get_mqtt_data(hass) platforms_loaded = mqtt_data.platforms_loaded new_platforms: set[Platform | str] = platforms - platforms_loaded - platforms_loaded.update(new_platforms) tasks: list[asyncio.Task] = [] if "device_automation" in new_platforms: # Local import to avoid circular dependencies # pylint: disable-next=import-outside-toplevel from . import device_automation - new_platforms.remove("device_automation") tasks.append( create_eager_task(device_automation.async_setup_entry(hass, config_entry)) ) @@ -69,19 +67,19 @@ async def async_forward_entry_setup_and_setup_discovery( # pylint: disable-next=import-outside-toplevel from . import tag - new_platforms.remove("tag") tasks.append(create_eager_task(tag.async_setup_entry(hass, config_entry))) - if new_platforms: + if new_entity_platforms := (new_platforms - {"tag", "device_automation"}): tasks.append( create_eager_task( hass.config_entries.async_forward_entry_setups( - config_entry, new_platforms + config_entry, new_entity_platforms ) ) ) if not tasks: return await asyncio.gather(*tasks) + platforms_loaded.update(new_platforms) def mqtt_config_entry_enabled(hass: HomeAssistant) -> bool | None: diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index a25a670fa34..257b0b256dc 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -936,6 +936,9 @@ async def help_test_encoding_subscribable_topics( hass, f"homeassistant/{domain}/item3/config", json.dumps(config3) ) await hass.async_block_till_done() + await hass.async_block_till_done() + await hass.async_block_till_done() + await hass.async_block_till_done() expected_result = attribute_value or value diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 71b103c8bcc..0f3069ef1a1 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -3598,14 +3598,20 @@ async def test_disabling_and_enabling_entry( config_alarm_control_panel = '{"name": "test_new", "state_topic": "home/alarm", "command_topic": "home/alarm/set"}' config_light = '{"name": "test_new", "command_topic": "test-topic_new"}' - # Discovery of mqtt tag - async_fire_mqtt_message(hass, "homeassistant/tag/abc/config", config_tag) + with patch( + "homeassistant.components.mqtt.mixins.mqtt_config_entry_enabled", + return_value=False, + ): + # Discovery of mqtt tag + async_fire_mqtt_message(hass, "homeassistant/tag/abc/config", config_tag) - # Late discovery of mqtt entities - async_fire_mqtt_message( - hass, "homeassistant/alarm_control_panel/abc/config", config_alarm_control_panel - ) - async_fire_mqtt_message(hass, "homeassistant/light/abc/config", config_light) + # Late discovery of mqtt entities + async_fire_mqtt_message( + hass, + "homeassistant/alarm_control_panel/abc/config", + config_alarm_control_panel, + ) + async_fire_mqtt_message(hass, "homeassistant/light/abc/config", config_light) # Disable MQTT config entry await hass.config_entries.async_set_disabled_by(