From 09213d8933e9d9dd6dc72eb827106c267595eb96 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 21 May 2024 16:39:23 -1000 Subject: [PATCH] Avoid creating tasks to subscribe to discovery in MQTT (#117890) --- homeassistant/components/mqtt/discovery.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 4717f297d1654..6b6cc7c99963b 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -362,16 +362,15 @@ def discovery_done(_: Any) -> None: hass, MQTT_DISCOVERY_DONE.format(*discovery_hash), None ) - discovery_topics = [ - f"{discovery_topic}/+/+/config", - f"{discovery_topic}/+/+/+/config", - ] - mqtt_data.discovery_unsubscribe = await asyncio.gather( - *( - mqtt.async_subscribe(hass, topic, async_discovery_message_received, 0) - for topic in discovery_topics + # async_subscribe will never suspend so there is no need to create a task + # here and its faster to await them in sequence + mqtt_data.discovery_unsubscribe = [ + await mqtt.async_subscribe(hass, topic, async_discovery_message_received, 0) + for topic in ( + f"{discovery_topic}/+/+/config", + f"{discovery_topic}/+/+/+/config", ) - ) + ] mqtt_data.last_discovery = time.monotonic() mqtt_integrations = await async_get_mqtt(hass)