I'm trying to wrap an existing MQTT client with a helper class.
The functions defined by paho.mqtt.client
defined as follows:
def on_connect(client, userdata, flags, rc):
def on_message( client, userdata, msg):
The wrapper class looks as follows:
import paho.mqtt.client as mqtt
HOST = ''
PORT = 1883
class MqttHandler:
def __init__(self):
self.client = mqtt.Client()
self.client.connect(HOST, PORT)
# How can I direct those callbacks into the class functions?
self.client.on_connect = on_connect
self.client.on_message = on_message
self.client.loop_forever()
def terminate(self):
self.client.disconnect()
def on_connect(self, client, userdata, flags, rc):
pass
def on_message(self, client, userdata, msg):
pass
The paho.mqtt.client
on_connect
property is expecting a function with a signature of a non class function (without the leading self
variable), how can I redirect those callbacks into my class functions?