Ensure MQTT subscriptions can be made when the broker is disconnected (#132270)

This commit is contained in:
Jan Bouwhuis 2024-12-04 15:18:04 +01:00 committed by Franck Nijhof
parent 4fd4ba7813
commit 333ada7670
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 8 additions and 1 deletions

View File

@ -227,7 +227,7 @@ def async_subscribe_internal(
translation_placeholders={"topic": topic},
) from exc
client = mqtt_data.client
if not client.connected and not mqtt_config_entry_enabled(hass):
if not mqtt_config_entry_enabled(hass):
raise HomeAssistantError(
f"Cannot subscribe to topic '{topic}', MQTT is not enabled",
translation_key="mqtt_not_setup_cannot_subscribe",

View File

@ -1045,10 +1045,17 @@ async def test_restore_subscriptions_on_reconnect(
mqtt_client_mock.reset_mock()
mqtt_client_mock.on_disconnect(None, None, 0)
# Test to subscribe orther topic while the client is not connected
await mqtt.async_subscribe(hass, "test/other", record_calls)
async_fire_time_changed(hass, utcnow() + timedelta(seconds=3)) # cooldown
assert ("test/other", 0) not in help_all_subscribe_calls(mqtt_client_mock)
mock_debouncer.clear()
mqtt_client_mock.on_connect(None, None, None, 0)
await mock_debouncer.wait()
# Assert all subscriptions are performed at the broker
assert ("test/state", 0) in help_all_subscribe_calls(mqtt_client_mock)
assert ("test/other", 0) in help_all_subscribe_calls(mqtt_client_mock)
@pytest.mark.parametrize(