Skip to content

Commit 8ff10ad

Browse files
authored
Fix MQTT threading bug (home-assistant#43667)
1 parent e1de36f commit 8ff10ad

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

homeassistant/components/mqtt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ async def publish_birth_message(birth_message):
944944
)
945945

946946
birth_message = Message(**self.conf[CONF_BIRTH_MESSAGE])
947-
self.hass.loop.create_task(publish_birth_message(birth_message))
947+
self.hass.add_job(publish_birth_message(birth_message))
948948

949949
def _mqtt_on_message(self, _mqttc, _userdata, msg) -> None:
950950
"""Message received callback."""

tests/components/mqtt/test_init.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,9 @@ async def test_restore_subscriptions_on_reconnect(hass, mqtt_client_mock, mqtt_m
638638
assert mqtt_client_mock.subscribe.call_count == 1
639639

640640
mqtt_mock._mqtt_on_disconnect(None, None, 0)
641-
mqtt_mock._mqtt_on_connect(None, None, None, 0)
642-
await hass.async_block_till_done()
641+
with patch("homeassistant.components.mqtt.DISCOVERY_COOLDOWN", 0):
642+
mqtt_mock._mqtt_on_connect(None, None, None, 0)
643+
await hass.async_block_till_done()
643644
assert mqtt_client_mock.subscribe.call_count == 2
644645

645646

@@ -671,8 +672,9 @@ async def test_restore_all_active_subscriptions_on_reconnect(
671672
assert mqtt_client_mock.unsubscribe.call_count == 0
672673

673674
mqtt_mock._mqtt_on_disconnect(None, None, 0)
674-
mqtt_mock._mqtt_on_connect(None, None, None, 0)
675-
await hass.async_block_till_done()
675+
with patch("homeassistant.components.mqtt.DISCOVERY_COOLDOWN", 0):
676+
mqtt_mock._mqtt_on_connect(None, None, None, 0)
677+
await hass.async_block_till_done()
676678

677679
expected.append(call("test/state", 1))
678680
assert mqtt_client_mock.subscribe.mock_calls == expected

0 commit comments

Comments
 (0)