Skip to main content

An opinionated framework to handle MQTT communication in Python.

Project description

Python MQTT framework

PyPI - Version PyPI - Python Version codecov


Overview

MQTT framework is a library that provides an opinionated structure for setting up message handlers and publishers for MQTT brokers. It is built on top of the paho-mqtt library.


Requirements

  • Python 3.8+

We highly recommend and only officially support the latest patch release of each Python series.

Installation

Install using pip.

pip install python-mqtt-framework

Defining topic handlers

This is how you write the topic handlers:

from mqtt_framework import TopicHandler
from rest_framework import serializers
from pydantic import BaseModel


class SimpleTestModel(BaseModel):
    testing: str


class SimpleTestSerializer(serializers.Serializer):
    testing = serializers.CharField()

    def create(self, validated_data):
        print('test_message', validated_data)
        return validated_data


class SerializerTopicHandler(TopicHandler):
    topic = 'test/topic'
    serializer_class = SimpleTestSerializer
    qos = 1

    # Calls the serializer's "save" method


class PydanticTopicHandler(TopicHandler):
    topic = 'another/topic'
    pydantic_model = SimpleTestModel
    qos = 0

    def handle(self):
        pydantic_instance = self.get_validated_payload()
        # Do something with the pydantic_instance

There's no need to register the topic handlers, the framework will automatically discover them as long as they are imported.

Django Integration

Add 'mqtt_framework' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    # ...
    'mqtt_framework',
]

Then you add the MQTT_FRAMEWORK setting:

MQTT_FRAMEWORK = {
    'BROKER_URL': 'mqtt://<user>:<password>@<host>:<port>',
    'TOPIC_HANDLERS': 'your_app.topic_handlers',
    'KEEPALIVE': 60,
}

Running the MQTT listener

To run the MQTT listener, you can use the runmqtt management command:

python manage.py runmqtt

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

python_mqtt_framework-0.0.5.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

python_mqtt_framework-0.0.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file python_mqtt_framework-0.0.5.tar.gz.

File metadata

  • Download URL: python_mqtt_framework-0.0.5.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for python_mqtt_framework-0.0.5.tar.gz
Algorithm Hash digest
SHA256 81e78a453636c6b9b53434bf9efe1a84c880cb7669d4a1e93f785a159da71977
MD5 51f21acfe2896cfb24808d6189aa2bf2
BLAKE2b-256 84166c9810870247142a26d2413e69db13d6589bae418effb15863008700c07a

See more details on using hashes here.

File details

Details for the file python_mqtt_framework-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for python_mqtt_framework-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c69a567ec0a4f2eebd2d33e3e06f0d421e78d384c0011b1e7236a9fed73a3adb
MD5 dc4d05c487906b471a368ae8383af017
BLAKE2b-256 562c9881abb34a409dfb7f71d3407a04e43a1d778426cde247339d7cca6f851a

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page