-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Blog post for new MQTT subscribe status callback option #2807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2db8b0f
3e7dc22
5a240c3
6319146
b9bb856
b44731e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||
| --- | ||||||||||
| author: Jan Bouwhuis | ||||||||||
| authorURL: https://github.com/jbouwh | ||||||||||
| authorImageURL: https://avatars.githubusercontent.com/u/7188918?s=96&v=4 | ||||||||||
| title: Option added to add a callback for status updates on an MQTT subscription | ||||||||||
| --- | ||||||||||
|
|
||||||||||
| ## Option added to add a callback on an MQTT subscription | ||||||||||
|
|
||||||||||
| Integrations that use MQTT might need to wait for a subscription to complete before they initiate actions. The default behavior is that a subscription is queued and debounced, so callers usually do not wait for broker confirmation. Some integrations must guarantee that the broker finished the subscription. To support this requirement, the MQTT subscribe API adds a `on_subscribe_status` callback option. | ||||||||||
|
|
||||||||||
| The new async signature for MQTT subscribe that supports the new callback option is: | ||||||||||
|
|
||||||||||
| ```python | ||||||||||
| @bind_hass | ||||||||||
| async def async_subscribe( | ||||||||||
| hass: HomeAssistant, | ||||||||||
| topic: str, | ||||||||||
| msg_callback: Callable[[ReceiveMessage], Coroutine[Any, Any, None] | None], | ||||||||||
| qos: int = DEFAULT_QOS, | ||||||||||
| encoding: str | None = DEFAULT_ENCODING, | ||||||||||
| on_subscribe_status: CALLBACK_TYPE, | ||||||||||
| ) -> CALLBACK_TYPE: | ||||||||||
| """Subscribe to an MQTT topic. | ||||||||||
|
|
||||||||||
| Call the return value to unsubscribe. | ||||||||||
| """ | ||||||||||
| ... | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| Example where we want to publish a status update after the subscription is ready: | ||||||||||
|
|
||||||||||
| ```python | ||||||||||
| from homeassistant.components import mqtt | ||||||||||
|
|
||||||||||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||||||||||
| """Setup integration with awaited MQTT subscription.""" | ||||||||||
|
|
||||||||||
| @callback | ||||||||||
| def async_subscribe_callback(msg: ReceiveMessage) -> None: | ||||||||||
| """Callback example.""" | ||||||||||
| # Do stuff | ||||||||||
|
|
||||||||||
| def _on_subscribe() -> None: | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix function name mismatch in code example. Line 44 defines the callback as - def _on_subscribe() -> None:
+ def _on_subscribe_status() -> None:
"""Publish an online state when we are ready to receive updates."""
hass.async_create_task(
mqtt.async_publish(Also applies to: 58-58 🤖 Prompt for AI Agents |
||||||||||
| """Publish an online state when we are ready to receive updates.""" | ||||||||||
| hass.async_create_task( | ||||||||||
| mqtt.async_publish( | ||||||||||
| hass, "myintegration/status", "online", 0, retain=True | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Subscribe and wait | ||||||||||
| unsubscribe_callback = await mqtt.async_subscribe( | ||||||||||
| hass, | ||||||||||
| "myintegration/updates", | ||||||||||
| async_subscribe_callback, | ||||||||||
| qos=1, | ||||||||||
| on_subscribe_status=_on_subscribe_status | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Do some stuff | ||||||||||
| ... | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Receive status updates on for as specific MQTT subscription | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix heading grammar. "Receive status updates on for as specific MQTT subscription" is grammatically incorrect. - ## Receive status updates on for as specific MQTT subscription
+ ## Monitor subscription status with the helper function📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| In case a subscription is already pending, or when we want to keep monitoring, the `mqtt.async_on_subscribe_done` helper can be used to monitor its subscription, to allow doing additional task. Make sure the same QoS is used. | ||||||||||
|
|
||||||||||
| Example: | ||||||||||
|
|
||||||||||
| ```python | ||||||||||
| from homeassistant.components import mqtt | ||||||||||
|
|
||||||||||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||||||||||
| """Setup integration MQTT subscription monitoring.""" | ||||||||||
|
|
||||||||||
| def _on_subscribe_status() -> None: | ||||||||||
| """Integration .""" | ||||||||||
| # Do stuff | ||||||||||
|
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complete the callback docstring. Line 78 contains an incomplete comment that reads only "Integration ." def _on_subscribe_status() -> None:
- """Integration ."""
+ """Handle subscription ready signal."""
# Do stuff📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| # Await a pending subscription | ||||||||||
| await mqtt.async_on_subscribe_done( | ||||||||||
| hass, | ||||||||||
| "myintegration/status", | ||||||||||
| qos=1, | ||||||||||
| on_subscribe_status=_on_subscribe_status, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Do stuff | ||||||||||
| ``` | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Improve title and heading phrasing.
Both the title (line 5) and heading (line 8) use awkward phrasing with "add to add" and "add a callback on". Use more concise language.
As per coding guidelines, prefer direct, authoritative language and front the goal.
Also applies to: 8-8
🤖 Prompt for AI Agents