-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add 'aiomqtt' recipe. * [pre-commit.ci] auto fixes from pre-commit.com hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
df62a45
commit 104816f
Showing
16 changed files
with
305 additions
and
254 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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,58 @@ | ||
# `aiomqtt` - Python Asyncio APIs for MQTT | ||
|
||
## Set Up | ||
|
||
```toml | ||
# pyproject.toml | ||
|
||
dependencies = [ | ||
"aiomqtt", | ||
] | ||
``` | ||
|
||
```bash | ||
pipenv install aiomqtt | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
"""MQTT `aiomqtt` Usage. | ||
""" | ||
|
||
import asyncio | ||
import os | ||
import sys | ||
|
||
import aiomqtt | ||
|
||
MQTT_HOST = 'localhost' | ||
MQTT_TOPIC_PREFIX = 'python-cookbook' | ||
|
||
|
||
async def main(): | ||
async with aiomqtt.Client(MQTT_HOST, timeout=3.5) as client: | ||
|
||
# Subscribe | ||
await client.subscribe(f'{MQTT_TOPIC_PREFIX}/#') | ||
async for message in client.messages: | ||
if isinstance(message.payload, bytes): | ||
print(message.payload.decode('utf-8')) | ||
|
||
# Publish | ||
# await client.publish(f'{MQTT_TOPIC_PREFIX}/example', payload={'msg': 'hello'}) | ||
|
||
|
||
# Change to the "Selector" event loop if platform is Windows | ||
if sys.platform.lower() == "win32" or os.name.lower() == "nt": | ||
from asyncio import WindowsSelectorEventLoopPolicy # type: ignore | ||
from asyncio import set_event_loop_policy | ||
|
||
set_event_loop_policy(WindowsSelectorEventLoopPolicy()) | ||
|
||
asyncio.run(main()) | ||
``` | ||
|
||
## References | ||
|
||
- [`aiomqtt` Documentation](https://sbtinstruments.github.io/aiomqtt/index.html) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,34 @@ | ||
"""MQTT `aiomqtt` Usage. | ||
""" | ||
|
||
import asyncio | ||
import os | ||
import sys | ||
|
||
import aiomqtt | ||
|
||
MQTT_HOST = 'localhost' | ||
MQTT_TOPIC_PREFIX = 'python-cookbook' | ||
|
||
|
||
async def main() -> None: | ||
async with aiomqtt.Client(MQTT_HOST, timeout=3.5) as client: | ||
|
||
# Subscribe | ||
await client.subscribe(f'{MQTT_TOPIC_PREFIX}/#') | ||
async for message in client.messages: | ||
if isinstance(message.payload, bytes): | ||
print(message.payload.decode('utf-8')) | ||
|
||
# Publish | ||
# await client.publish(f'{MQTT_TOPIC_PREFIX}/example', payload={'msg': 'hello'}) | ||
|
||
|
||
# Change to the "Selector" event loop if platform is Windows | ||
if sys.platform.lower() == 'win32' or os.name.lower() == 'nt': | ||
from asyncio import WindowsSelectorEventLoopPolicy # type: ignore | ||
from asyncio import set_event_loop_policy | ||
|
||
set_event_loop_policy(WindowsSelectorEventLoopPolicy()) | ||
|
||
asyncio.run(main()) |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.