Skip to content

Commit

Permalink
Ensure MQTT subscriptions can be made when the broker is disconnected (
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored and frenck committed Dec 4, 2024
1 parent 4fd4ba7 commit 333ada7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions tests/components/mqtt/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 333ada7

Please sign in to comment.