Skip to main content

decorated_paho_mqtt

decorated_paho_mqtt is a wrapper to the Eclipse paho mqtt library ( https://pypi.org/project/paho-mqtt/ )

It is intended to be used with MQTTv5

pack_topic

pack_topic takes an mqtt topic pattern, such as a/+/c/#, and additional parameters to fill in for the placeholders.

For each + placeholder, a string has to be passed; for a trailing #, a list/tuple has to be passed.

pack_topic('a/+/c/#', "b", ("d","e")) will return the topic a/b/c/d/e

Packing a topic with obviously invalid characters as parameters will raise an exception.

Known Bug: Packing a topic with an empty tuple as parameter for a # placeholder will not remove the trailing slash. pack_topic and unpack_topic are not yet completely symmetrical.

unpack_topic

unpack_topic takes an mqtt topic pattern, such as a/+/c/#, and an actual topic, such as a/b/c/d/e.

It will match the topic against the pattern, and for each placeholder in the pattern, yield the actual values.

list(unpack_topic('a/+/c/#','a/b/c/d/e')) will return ["b", ["d","e"]]

GenericMqttEndpoint

GenericMqttEndpoint allows to specify topic subscriptions in a declarative way using decorators. It wraps an paho mqtt client.

You can use the subscribe_decorator on methods of derived classes to receive messages.

You can use the publish method to publish messages.

Example:

import json
from signal import pause
from icecream import ic
from decorated_paho_mqtt import GenericMqttEndpoint


class MyMqtt(GenericMqttEndpoint):

    # Using this decorator will automatically subscribe to the topic and provide the values as positional parameters
    # The paho parameters will be passed as named parameters.
    @GenericMqttEndpoint.subscribe_decorator("a/+/c/#", qos=2)
    def receive_something(self, b, d_e, *, client, userdata, message):
        assert b == "b"
        assert len(d_e) == 2
        assert d_e[0] == "d"
        assert d_e[1] == "e"

    def send_something(self, b, d, e):
        self.publish("a/+/c/#", b, (d, e), qos=2, retain=False, payload=json.dumps(None))

    def _on_log(self, client, userdata, level, buf):
        super(MyMqtt, self)._on_log(client, userdata, level, buf)
        ic(client, userdata, level, buf)


mqtt = MyMqtt(
    # Same parameters as paho's MqttClient()
    dict(transport="tcp"),
    # None or same parameters as paho's MqttClient.username_pw_set()
    dict(username="username", password="password"),
    # Same parameters as paho's MqttClient.connect_async()
    dict(host="127.0.0.1", port=1883, keepalive=10),
    # Whether to activate TLS
    False
)

# Non-Blocking, callbacks will return in another thread:
mqtt.connect()
pause()

Metadata

Release files for decorated-paho-mqtt 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for decorated-paho-mqtt 1.1.1
File Size Uploaded
decorated_paho_mqtt-1.1.1.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for decorated-paho-mqtt 1.1.1
File Interpreter ABI Platform
decorated_paho_mqtt-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / decorated_paho_mqtt-1.1.1.tar.gz

Download URL decorated_paho_mqtt-1.1.1.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ef847d8040aeaf9190f2ad8031b0f91d3ab80806613d830109108f939056e60f
BLAKE2b-256 checksum
How to use checksums
fc3a8cfde47377ab9443563ace7d52e4f2e82aa1de8fbf94caed4d48280ece1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / decorated_paho_mqtt-1.1.1-py3-none-any.whl

Download URL decorated_paho_mqtt-1.1.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
909df4ed0e5e14935b1aacd6bce7edc1d84485502b3b9a38e7ecc390020a9f14
BLAKE2b-256 checksum
How to use checksums
3d5e136295ad6eb37eec187495c2cbbb97f68e3d6108963a4e8d8495720c0472
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page