From 4e7397dc9d53bbdeed9e0e6edbfdeaacc2a3e7ad Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 30 Oct 2024 14:38:44 +0100 Subject: [PATCH] Test discovery subscriptions not done when discovery is disabled (#129458) Test discovery subscriptions not performend when discovery is disabled --- tests/components/mqtt/conftest.py | 3 ++- tests/components/mqtt/test_client.py | 33 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/components/mqtt/conftest.py b/tests/components/mqtt/conftest.py index 7395767aeae..e22ae297498 100644 --- a/tests/components/mqtt/conftest.py +++ b/tests/components/mqtt/conftest.py @@ -87,7 +87,8 @@ async def setup_with_birth_msg_client_mock( patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0), ): entry = MockConfigEntry( - domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"} + domain=mqtt.DOMAIN, + data=mqtt_config_entry_data or {mqtt.CONF_BROKER: "test-broker"}, ) entry.add_to_hass(hass) hass.config.components.add(mqtt.DOMAIN) diff --git a/tests/components/mqtt/test_client.py b/tests/components/mqtt/test_client.py index e02719991f8..f2af337bc5e 100644 --- a/tests/components/mqtt/test_client.py +++ b/tests/components/mqtt/test_client.py @@ -1716,6 +1716,39 @@ async def test_mqtt_subscribes_topics_on_connect( assert ("still/pending", 1) in subscribe_calls +@pytest.mark.parametrize( + "mqtt_config_entry_data", + [ENTRY_DEFAULT_BIRTH_MESSAGE | {mqtt.CONF_DISCOVERY: False}], +) +async def test_mqtt_discovery_not_subscribes_when_disabled( + hass: HomeAssistant, + mock_debouncer: asyncio.Event, + setup_with_birth_msg_client_mock: MqttMockPahoClient, +) -> None: + """Test discovery subscriptions not performend when discovery is disabled.""" + mqtt_client_mock = setup_with_birth_msg_client_mock + + await mock_debouncer.wait() + + subscribe_calls = help_all_subscribe_calls(mqtt_client_mock) + for component in SUPPORTED_COMPONENTS: + assert (f"homeassistant/{component}/+/config", 0) not in subscribe_calls + assert (f"homeassistant/{component}/+/+/config", 0) not in subscribe_calls + + mqtt_client_mock.on_disconnect(Mock(), None, 0) + + mqtt_client_mock.reset_mock() + + mock_debouncer.clear() + mqtt_client_mock.on_connect(Mock(), None, 0, 0) + await mock_debouncer.wait() + + subscribe_calls = help_all_subscribe_calls(mqtt_client_mock) + for component in SUPPORTED_COMPONENTS: + assert (f"homeassistant/{component}/+/config", 0) not in subscribe_calls + assert (f"homeassistant/{component}/+/+/config", 0) not in subscribe_calls + + @pytest.mark.parametrize( "mqtt_config_entry_data", [ENTRY_DEFAULT_BIRTH_MESSAGE],