From e20b88a54f016e0f3a9b01dd24a58f71a5cfbe4a Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 24 Nov 2025 12:32:55 +0100 Subject: [PATCH] Revert MQTT subscribe on_subscribe arg (#157168) --- homeassistant/components/mqtt/client.py | 22 +------- tests/components/mqtt/test_client.py | 67 ------------------------- 2 files changed, 1 insertion(+), 88 deletions(-) diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index d9fea573850..ccd287f184d 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -228,32 +228,12 @@ async def async_subscribe( msg_callback: Callable[[ReceiveMessage], Coroutine[Any, Any, None] | None], qos: int = DEFAULT_QOS, encoding: str | None = DEFAULT_ENCODING, - on_subscribe: CALLBACK_TYPE | None = None, ) -> CALLBACK_TYPE: """Subscribe to an MQTT topic. - If the on_subcribe callback hook is set, it will be called once - when the subscription has been completed. - Call the return value to unsubscribe. """ - handler: CALLBACK_TYPE | None = None - - def _on_subscribe_done() -> None: - """Call once when the subscription was completed.""" - if TYPE_CHECKING: - assert on_subscribe is not None and handler is not None - - handler() - on_subscribe() - - subscription_handler = async_subscribe_internal( - hass, topic, msg_callback, qos, encoding - ) - if on_subscribe is not None: - handler = async_on_subscribe_done(hass, topic, qos, _on_subscribe_done) - - return subscription_handler + return async_subscribe_internal(hass, topic, msg_callback, qos, encoding) @callback diff --git a/tests/components/mqtt/test_client.py b/tests/components/mqtt/test_client.py index fd29f427ef4..219c4724cf3 100644 --- a/tests/components/mqtt/test_client.py +++ b/tests/components/mqtt/test_client.py @@ -330,73 +330,6 @@ async def test_status_subscription_done( subscribe_callback() -async def test_subscribe_topic_with_subscribe_done( - hass: HomeAssistant, - mqtt_mock_entry: MqttMockHAClientGenerator, - recorded_calls: list[ReceiveMessage], - record_calls: MessageCallbackType, -) -> None: - """Test the subscription of a topic.""" - await mqtt_mock_entry() - - on_status = asyncio.Event() - - def _on_subscribe() -> None: - hass.async_create_task(mqtt.async_publish(hass, "test-topic", "beer ready", 0)) - on_status.set() - - # Start a first subscription - unsub1 = await mqtt.async_subscribe( - hass, "test-topic", record_calls, on_subscribe=_on_subscribe - ) - await on_status.wait() - await hass.async_block_till_done() - assert len(recorded_calls) == 1 - assert recorded_calls[0].topic == "test-topic" - assert recorded_calls[0].payload == "beer ready" - assert recorded_calls[0].qos == 0 - recorded_calls.clear() - - # Start a second subscription to the same topic - on_status.clear() - unsub2 = await mqtt.async_subscribe( - hass, "test-topic", record_calls, on_subscribe=_on_subscribe - ) - await on_status.wait() - await hass.async_block_till_done() - assert len(recorded_calls) == 2 - assert recorded_calls[0].topic == "test-topic" - assert recorded_calls[0].payload == "beer ready" - assert recorded_calls[0].qos == 0 - assert recorded_calls[1].topic == "test-topic" - assert recorded_calls[1].payload == "beer ready" - assert recorded_calls[1].qos == 0 - - unsub1() - unsub2() - - -@pytest.mark.usefixtures("mqtt_mock_entry") -async def test_subscribe_topic_not_initialize( - hass: HomeAssistant, record_calls: MessageCallbackType -) -> None: - """Test the subscription of a topic when MQTT was not initialized.""" - with pytest.raises( - HomeAssistantError, match=r".*make sure MQTT is set up correctly" - ): - await mqtt.async_subscribe(hass, "test-topic", record_calls) - - def _on_subscribe_callback() -> None: - pass - - with pytest.raises( - HomeAssistantError, match=r".*make sure MQTT is set up correctly" - ): - await mqtt.async_subscribe( - hass, "test-topic", record_calls, on_subscribe=_on_subscribe_callback - ) - - async def test_subscribe_mqtt_config_entry_disabled( hass: HomeAssistant, mqtt_mock: MqttMockHAClient, record_calls: MessageCallbackType ) -> None: