Metadata-Version: 2.1
Name: dm-aiomqtt
Version: 0.2.2
Summary: This is my custom aiomqtt client
Home-page: https://pypi.org/project/dm-aiomqtt
Author: dimka4621
Author-email: mismartconfig@gmail.com
Project-URL: GitHub, https://github.com/DIMKA4621/dm-aiomqtt
Keywords: dm aiomqtt
Classifier: Programming Language :: Python :: 3.8
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: dm-logger ==0.5.2
Requires-Dist: aiomqtt ==2.0.0

# DM-aiomqtt

## Urls

* [PyPI](https://pypi.org/project/dm-aiomqtt)
* [GitHub](https://github.com/DIMKA4621/dm-aiomqtt)

## Usage

### 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())
```

### One connection for all actions

```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}")
    await publish("test/success", payload=True)


async def main():
    # create client
    mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password")

    # add handler for 'test' topic
    await mqtt_client.add_topic_handler("test", test_topic_handler)

    # start connection
    await mqtt_client.connect()

    # publish
    await mqtt_client.publish("test", payload="Hello World!", logging=True)

    # other code (for example, endless waiting)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())
```

### Temporary connection for actions

```python
from dm_aiomqtt import DMAioMqttClient
import asyncio


async def main():
    # create client
    mqtt_client = DMAioMqttClient("localhost", 1883, "username", "password")

    # create callback
    async def callback(publish: DMAioMqttClient.publish):
        await publish("test/1", payload="Hello World!1", qos=2, logging=True)
        # other code
        await publish("test/2", payload="Hello World!2", qos=2, logging=True)

    # execute callback in temporary connection
    await mqtt_client.temp_connect(callback)

    # other code (for example, endless waiting)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())
```

### Set custom logger

_If you want set up custom logger_

```python
from dm_aiomqtt import DMAioMqttClient


# create custom logger
class MyLogger:
    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())
```
