Skip to main content

Wrapper for Paho MQTT with declarative subscriptions and topic parsing utilities

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

decorated_paho_mqtt-1.0.5.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

decorated_paho_mqtt-1.0.5-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file decorated_paho_mqtt-1.0.5.tar.gz.

File metadata

  • Download URL: decorated_paho_mqtt-1.0.5.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for decorated_paho_mqtt-1.0.5.tar.gz
Algorithm Hash digest
SHA256 e5eb6a995f95d4a601163ed6c4dae403be42db3dd31c9f37f908d88b0b2d9347
MD5 11be1f3dfdd31714dd39cfa3d6685bf7
BLAKE2b-256 0dc973bd79cac14b352bc48384f75e98f85c1a6cc7ecc719a318be1f729a1375

See more details on using hashes here.

File details

Details for the file decorated_paho_mqtt-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: decorated_paho_mqtt-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for decorated_paho_mqtt-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d134f7220dd60281f5f514094ea7ef7582591c771a6f64e1c94325742db9942f
MD5 69af134eb08440d71e545575b2905fbb
BLAKE2b-256 2750697261e3881d383b48cb3db62f7ad280cdb83a00862a8fbfbda4aa7cb3eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page