1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-19 18:38:58 +00:00

Revert MQTT subscribe on_subscribe arg (#157168)

This commit is contained in:
Jan Bouwhuis
2025-11-24 12:32:55 +01:00
committed by GitHub
parent ac69712a51
commit e20b88a54f
2 changed files with 1 additions and 88 deletions

View File

@@ -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

View File

@@ -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: