dm-aiomqtt


Namedm-aiomqtt JSON
Version 0.4.7 PyPI version JSON
download
home_pagehttps://pypi.org/project/dm-aiomqtt
SummaryThis is my custom aiomqtt client
upload_time2024-04-23 07:35:56
maintainerNone
docs_urlNone
authordimka4621
requires_python>=3.8
licenseNone
keywords dm aiomqtt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # DM-aiomqtt

## Urls

* [PyPI](https://pypi.org/project/dm-aiomqtt)
* [GitHub](https://github.com/DIMKA4621/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())
```

### 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.
<br>
**BUT** the `payload` will be in a changed format `[your_payload]_dt_[iso_local_datetime]`

**Example**:
<br>
Your payload: `"{"temp": 12}"`
<br>
Edited payload: `"{"temp": 12}_dt_2024-04-19T09:57:40"`

### 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/68/7b/0b4017a9bd3e309d7b18634cf3c89f97de9020862ccff3d3f9e684ce9550/dm_aiomqtt-0.4.7.tar.gz",
    "platform": null,
    "description": "# DM-aiomqtt\n\n## Urls\n\n* [PyPI](https://pypi.org/project/dm-aiomqtt)\n* [GitHub](https://github.com/DIMKA4621/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\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<br>\n**BUT** the `payload` will be in a changed format `[your_payload]_dt_[iso_local_datetime]`\n\n**Example**:\n<br>\nYour payload: `\"{\"temp\": 12}\"`\n<br>\nEdited payload: `\"{\"temp\": 12}_dt_2024-04-19T09:57:40\"`\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.7",
    "project_urls": {
        "GitHub": "https://github.com/DIMKA4621/dm-aiomqtt",
        "Homepage": "https://pypi.org/project/dm-aiomqtt"
    },
    "split_keywords": [
        "dm",
        "aiomqtt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b81a5217c862b6736949da8316510d25d1d09aae3dc6441d4dc3e21f21f29f0e",
                "md5": "5874c05dc9442393788a7e1ccdfed586",
                "sha256": "d1f735e3e971a8e4c2d02c007326b20911927f1ae6561de1ea08ea7bee401e30"
            },
            "downloads": -1,
            "filename": "dm_aiomqtt-0.4.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5874c05dc9442393788a7e1ccdfed586",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5787,
            "upload_time": "2024-04-23T07:35:54",
            "upload_time_iso_8601": "2024-04-23T07:35:54.338826Z",
            "url": "https://files.pythonhosted.org/packages/b8/1a/5217c862b6736949da8316510d25d1d09aae3dc6441d4dc3e21f21f29f0e/dm_aiomqtt-0.4.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "687b0b4017a9bd3e309d7b18634cf3c89f97de9020862ccff3d3f9e684ce9550",
                "md5": "6958f090f9c724c98fecf1425593cac6",
                "sha256": "6698ddd03bd255a8d72da09ce3bd6b9ea1106a367c6ec25143fff5be5325a3cd"
            },
            "downloads": -1,
            "filename": "dm_aiomqtt-0.4.7.tar.gz",
            "has_sig": false,
            "md5_digest": "6958f090f9c724c98fecf1425593cac6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 5138,
            "upload_time": "2024-04-23T07:35:56",
            "upload_time_iso_8601": "2024-04-23T07:35:56.379929Z",
            "url": "https://files.pythonhosted.org/packages/68/7b/0b4017a9bd3e309d7b18634cf3c89f97de9020862ccff3d3f9e684ce9550/dm_aiomqtt-0.4.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-23 07:35:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DIMKA4621",
    "github_project": "dm-aiomqtt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "dm-aiomqtt"
}
        
Elapsed time: 0.29089s