Only (re)subscribe MQTT topics using the debouncer (#119995)

* Only (re)subscribe using the debouncer

* Update test

* Fix test

* Reset mock
This commit is contained in:
Jan Bouwhuis
2024-06-19 23:57:18 +02:00
committed by GitHub
parent 0053c92d2b
commit 60ba80a28d
2 changed files with 58 additions and 40 deletions

View File

@ -1035,7 +1035,8 @@ class MQTT:
self, birth_message: PublishMessage
) -> None:
"""Resubscribe to all topics and publish birth message."""
await self._async_perform_subscriptions()
self._async_queue_resubscribe()
self._subscribe_debouncer.async_schedule()
await self._ha_started.wait() # Wait for Home Assistant to start
await self._discovery_cooldown() # Wait for MQTT discovery to cool down
# Update subscribe cooldown period to a shorter time
@ -1091,7 +1092,6 @@ class MQTT:
result_code,
)
self._async_queue_resubscribe()
birth: dict[str, Any]
if birth := self.conf.get(CONF_BIRTH_MESSAGE, DEFAULT_BIRTH):
birth_message = PublishMessage(**birth)
@ -1102,13 +1102,8 @@ class MQTT:
)
else:
# Update subscribe cooldown period to a shorter time
self.config_entry.async_create_background_task(
self.hass,
self._async_perform_subscriptions(),
name="mqtt re-subscribe",
)
self._subscribe_debouncer.set_timeout(SUBSCRIBE_COOLDOWN)
_LOGGER.info("MQTT client initialized")
self._async_queue_resubscribe()
self._subscribe_debouncer.async_schedule()
self._async_connection_result(True)