diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 5898b1918a2..27b43142f7c 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -944,7 +944,7 @@ class MQTT: ) birth_message = Message(**self.conf[CONF_BIRTH_MESSAGE]) - self.hass.loop.create_task(publish_birth_message(birth_message)) + self.hass.add_job(publish_birth_message(birth_message)) def _mqtt_on_message(self, _mqttc, _userdata, msg) -> None: """Message received callback.""" diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 39ea8bc92ec..86b905f2b0f 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -638,8 +638,9 @@ async def test_restore_subscriptions_on_reconnect(hass, mqtt_client_mock, mqtt_m assert mqtt_client_mock.subscribe.call_count == 1 mqtt_mock._mqtt_on_disconnect(None, None, 0) - mqtt_mock._mqtt_on_connect(None, None, None, 0) - await hass.async_block_till_done() + with patch("homeassistant.components.mqtt.DISCOVERY_COOLDOWN", 0): + mqtt_mock._mqtt_on_connect(None, None, None, 0) + await hass.async_block_till_done() assert mqtt_client_mock.subscribe.call_count == 2 @@ -671,8 +672,9 @@ async def test_restore_all_active_subscriptions_on_reconnect( assert mqtt_client_mock.unsubscribe.call_count == 0 mqtt_mock._mqtt_on_disconnect(None, None, 0) - mqtt_mock._mqtt_on_connect(None, None, None, 0) - await hass.async_block_till_done() + with patch("homeassistant.components.mqtt.DISCOVERY_COOLDOWN", 0): + mqtt_mock._mqtt_on_connect(None, None, None, 0) + await hass.async_block_till_done() expected.append(call("test/state", 1)) assert mqtt_client_mock.subscribe.mock_calls == expected