# DM-aiomqtt
## Urls
* [PyPI](https://pypi.org/project/dm-aiomqtt)
* [GitHub](https://github.com/MykhLibs/dm-aiomqtt)
## Usage
### Example
```python
from dm_aiomqtt import DMAioMqttClient
import asyncio
# create handler for 'test' topic
async def test_topic_handler(publish: DMAioMqttClient.publish, topic: str, payload: str) -> None:
print(f"Received message from {topic}: {payload}")
publish("test/success", payload=True)
async def main():
# create client
mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password")
# add handler for 'test' topic
mqtt_client.add_topic_handler("test", test_topic_handler)
# start connection
await mqtt_client.start()
# publish
mqtt_client.publish("test", payload="Hello World!", sent_logging=True)
# other code (for example, endless waiting)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())
```
You can also start with a block thread
```python
await mqtt_client.start_forever()
```
### TLS connection
* NOT required client certificate
```python
mqtt_client = DMAioMqttClient(
host="localhost",
port=8883,
ca_crt="ssl/ca.crt"
)
```
* REQUIRED client certificate
```python
mqtt_client = DMAioMqttClient(
host="localhost",
port=8883,
ca_crt="ssl/ca.crt",
client_crt="ssl/client.crt",
client_key="ssl/client.key"
)
```
### RESEND_NOT_SUCCESS_MESSAGES
Set this parameter `resend_not_success_messages=True` when create mqtt client
```python
mqtt_client = DMAioMqttClient(
host="localhost",
port=1883,
resend_not_success_messages=True
)
```
Now, in case of loss of connection, all messages that were sent during this period will be re-sent as soon as the connection appears again.
### Set custom logger
_If you want set up custom logger_
```python
from dm_aiomqtt import DMAioMqttClient
# create custom logger
class MyLogger:
def debug(self, message):
pass
def info(self, message):
pass
def warning(self, message):
print(message)
def error(self, message):
print(message)
# set up custom logger for all clients
DMAioMqttClient.set_logger(MyLogger())
```
### Publish method parameters
| Parameter | Type | Default Value | Description |
|-------------------|--------------------|---------------|-------------------------------------------|
| `topic` | `str` | (required) | Topic name |
| `payload` | `str` | `"DEBUG"` | Content to send |
| `qos` | `0` \| `1` \| `2` | `True` | MQTT QoS |
| `payload_to_json` | `bool` \| `"auto"` | `True` | Whether to convert content to JSON |
| `sent_logging` | `bool` | `False` | Whether to print the sending notification |
| `error_logging` | `bool` | `False` | Whether to print a send error warning |
### Run in Windows
_If you run async code in **Windows**, set correct selector_
```python
import asyncio
import sys
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
```
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/dm-aiomqtt",
"name": "dm-aiomqtt",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "dm aiomqtt",
"author": "dimka4621",
"author_email": "mismartconfig@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/f8/23/665956fedbf4256395a9fa91194371965f829091c006c8a43e448943e8a5/dm_aiomqtt-0.4.12.tar.gz",
"platform": null,
"description": "# DM-aiomqtt\n\n## Urls\n\n* [PyPI](https://pypi.org/project/dm-aiomqtt)\n* [GitHub](https://github.com/MykhLibs/dm-aiomqtt)\n\n## Usage\n\n### Example\n\n```python\nfrom dm_aiomqtt import DMAioMqttClient\nimport asyncio\n\n\n# create handler for 'test' topic\nasync def test_topic_handler(publish: DMAioMqttClient.publish, topic: str, payload: str) -> None:\n print(f\"Received message from {topic}: {payload}\")\n publish(\"test/success\", payload=True)\n\n\nasync def main():\n # create client\n mqtt_client = DMAioMqttClient(\"localhost\", 1883, \"username\", \"password\")\n\n # add handler for 'test' topic\n mqtt_client.add_topic_handler(\"test\", test_topic_handler)\n\n # start connection\n await mqtt_client.start()\n\n # publish\n mqtt_client.publish(\"test\", payload=\"Hello World!\", sent_logging=True)\n\n # other code (for example, endless waiting)\n await asyncio.Event().wait()\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\nYou can also start with a block thread\n\n```python\nawait mqtt_client.start_forever()\n```\n\n### TLS connection\n\n* NOT required client certificate\n\n ```python\n mqtt_client = DMAioMqttClient(\n host=\"localhost\",\n port=8883,\n ca_crt=\"ssl/ca.crt\"\n )\n ```\n\n* REQUIRED client certificate\n\n ```python\n mqtt_client = DMAioMqttClient(\n host=\"localhost\",\n port=8883,\n ca_crt=\"ssl/ca.crt\",\n client_crt=\"ssl/client.crt\",\n client_key=\"ssl/client.key\"\n )\n ```\n\n### RESEND_NOT_SUCCESS_MESSAGES\n\nSet this parameter `resend_not_success_messages=True` when create mqtt client\n\n ```python\n mqtt_client = DMAioMqttClient(\n host=\"localhost\",\n port=1883,\n resend_not_success_messages=True\n )\n ```\n\nNow, in case of loss of connection, all messages that were sent during this period will be re-sent as soon as the connection appears again.\n\n### Set custom logger\n\n_If you want set up custom logger_\n\n```python\nfrom dm_aiomqtt import DMAioMqttClient\n\n\n# create custom logger\nclass MyLogger:\n def debug(self, message):\n pass\n\n def info(self, message):\n pass\n\n def warning(self, message):\n print(message)\n\n def error(self, message):\n print(message)\n\n\n# set up custom logger for all clients\nDMAioMqttClient.set_logger(MyLogger())\n```\n\n### Publish method parameters\n\n| Parameter | Type | Default Value | Description |\n|-------------------|--------------------|---------------|-------------------------------------------|\n| `topic` | `str` | (required) | Topic name |\n| `payload` | `str` | `\"DEBUG\"` | Content to send |\n| `qos` | `0` \\| `1` \\| `2` | `True` | MQTT QoS |\n| `payload_to_json` | `bool` \\| `\"auto\"` | `True` | Whether to convert content to JSON |\n| `sent_logging` | `bool` | `False` | Whether to print the sending notification |\n| `error_logging` | `bool` | `False` | Whether to print a send error warning |\n\n### Run in Windows\n\n_If you run async code in **Windows**, set correct selector_\n\n```python\nimport asyncio\nimport sys\n\nif sys.platform == \"win32\":\n asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "This is my custom aiomqtt client",
"version": "0.4.12",
"project_urls": {
"GitHub": "https://github.com/MykhLibs/dm-aiomqtt",
"Homepage": "https://pypi.org/project/dm-aiomqtt"
},
"split_keywords": [
"dm",
"aiomqtt"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5ed0165968d9b4b9ba6caca1335320fa4b95665b442d11237d43fa05027822c1",
"md5": "1bd8e76d4a0bcbd300ca657f06459c6d",
"sha256": "00e0153708ca0315b6912b59e7751caddc67fa8a227eb3d6633ccc7eb92101ee"
},
"downloads": -1,
"filename": "dm_aiomqtt-0.4.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1bd8e76d4a0bcbd300ca657f06459c6d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 6019,
"upload_time": "2024-10-21T10:21:58",
"upload_time_iso_8601": "2024-10-21T10:21:58.027757Z",
"url": "https://files.pythonhosted.org/packages/5e/d0/165968d9b4b9ba6caca1335320fa4b95665b442d11237d43fa05027822c1/dm_aiomqtt-0.4.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f823665956fedbf4256395a9fa91194371965f829091c006c8a43e448943e8a5",
"md5": "020dd3e6dfe3a6b14d9e780f0bfbee7c",
"sha256": "dd00fd4da87507faa6ad7d52410f67dce290dcae588e4cacdf62b106e066077f"
},
"downloads": -1,
"filename": "dm_aiomqtt-0.4.12.tar.gz",
"has_sig": false,
"md5_digest": "020dd3e6dfe3a6b14d9e780f0bfbee7c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 5382,
"upload_time": "2024-10-21T10:21:58",
"upload_time_iso_8601": "2024-10-21T10:21:58.922251Z",
"url": "https://files.pythonhosted.org/packages/f8/23/665956fedbf4256395a9fa91194371965f829091c006c8a43e448943e8a5/dm_aiomqtt-0.4.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-21 10:21:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MykhLibs",
"github_project": "dm-aiomqtt",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "dm-aiomqtt"
}