diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index 55db1da6ff7..cd73ee8efb6 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -740,6 +740,9 @@ class MQTT: asyncio.run_coroutine_threadsafe( publish_birth_message(birth_message), self.hass.loop ) + else: + # Update subscribe cooldown period to a shorter time + self._subscribe_debouncer.set_timeout(SUBSCRIBE_COOLDOWN) async def _async_resubscribe(self) -> None: """Resubscribe on reconnect.""" diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 62ad74b6a09..498365de4a3 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -2062,6 +2062,19 @@ async def test_no_birth_message( await asyncio.sleep(0.2) mqtt_client_mock.publish.assert_not_called() + async def callback(msg: ReceiveMessage) -> None: + """Handle birth message.""" + + # Assert the subscribe debouncer subscribes after + # about SUBSCRIBE_COOLDOWN (0.1) sec + # but sooner than INITIAL_SUBSCRIBE_COOLDOWN (1.0) + + mqtt_client_mock.reset_mock() + await mqtt.async_subscribe(hass, "homeassistant/some-topic", callback) + await hass.async_block_till_done() + await asyncio.sleep(0.2) + mqtt_client_mock.subscribe.assert_called() + @pytest.mark.parametrize( "mqtt_config_entry_data",