-
-
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
Open
jbouwh
wants to merge
6
commits into
master
Choose a base branch
from
mqtt-subscribe-wait
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2db8b0f
Blog post for new MQTT subscribe wait option
jbouwh 3e7dc22
Update blog/2025-09-26-mqtt-subscribe-wait.md
jbouwh 5a240c3
Add `await` and document additional helper
jbouwh 6319146
Update blogpost after rework
jbouwh b9bb856
Small improvements
jbouwh b44731e
fix missed text change
jbouwh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --- | ||
| author: Jan Bouwhuis | ||
| authorURL: https://github.com/jbouwh | ||
| authorImageURL: https://avatars.githubusercontent.com/u/7188918?s=96&v=4 | ||
| title: Option added to wait for an MQTT subscription | ||
| --- | ||
|
|
||
| ## Option added to wait for an MQTT subscription | ||
|
|
||
| Integrations that use MQTT, might want to wait for an MQTT subscription to be completed. The default behavior is that an MQTT subscription is queued and debounced. In most cases the caller does not need to wait for the subscription to be completed. In some cases though, the integration needs to ensure that the subscription was actually done at the MQTT broker. To support the the `wait` options was added to the MQTT subscribe API. | ||
|
|
||
| The new async signature for MQTT subscribe that supports the new wait 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, | ||
| wait: bool = False, | ||
| ) -> CALLBACK_TYPE: | ||
| """Subscribe to an MQTT topic. | ||
|
|
||
| Call the return value to unsubscribe. | ||
| """ | ||
| ... | ||
| ``` | ||
|
|
||
| Example: | ||
|
|
||
| ```python | ||
| from homeassistant.components import mqtt | ||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Setup integration with awaited MQTT subscribe.""" | ||
|
|
||
| @callback | ||
| def async_subscribe_callback(msg: ReceiveMessage) -> None: | ||
| """Callback example.""" | ||
| # Do stuff | ||
|
|
||
| # Subscribe and wait | ||
| unsubscribe_callback = mqtt.async_subscribe( | ||
| hass, "myintegration/status", async_subscribe_callback, wait=True | ||
| ) | ||
|
|
||
| # Do some stuff | ||
| ... | ||
|
|
||
| # Unsubscribe | ||
| unsubscribe_callback() | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.