From cff7829a8d72034a8f1a069ec0ca17e86bea068f Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 28 Apr 2023 18:56:22 +0200 Subject: [PATCH] Fix mqtt subscribe debouncer initial delay too long when birth message is disabled (#92188) Fix mqtt subscribe deboucer initial delay --- homeassistant/components/mqtt/client.py | 3 +++ tests/components/mqtt/test_init.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) 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",