class documentation

This class implements an MQTT client. It is a wrapper around the paho MQTT client. It is implemented as a BackgroundWorker/Actor, so it runs in its own thread.

Method __init__ Constructor. Initialize the MQTT client.
Method __str__ Return a string representation of the MQTTConnection instance.
Method isFullySubscribed Check whether the number managed subscriptions matches the number of currently subscribed-to topics.
Method publish Publish the message data with the topic topic with the MQTT broker.
Method run Initialize and run the MQTT client as a BackgroundWorker/Actor.
Method shutdown Shutting down the MQTT client.
Method subscribeTopic Add one or more MQTT topics to subscribe to. Add the topic(s) afterwards to the list of subscribed-to topics.
Method unsubscribeTopic Unsubscribe from a topic. topic is either an MQTTTopic structure with a previously subscribed to topic, or a topic name, in which case it is searched for in the list of MQTTTopics.
Class Variable __slots__ Slots of the class.
Instance Variable actor The actor for the MQTT client.
Instance Variable address The address of the MQTT broker.
Instance Variable bindIF The interface to bind to.
Instance Variable caFile The CA file for the MQTT broker's certificate.
Instance Variable clientID The client ID for the MQTT client.
Instance Variable enableWebSocket Whether to enable WebSocket support.
Instance Variable isConnected Indicator whether the MQTT client is connected.
Instance Variable isStopped Indicator whether the MQTT client is stopped.
Instance Variable keepalive The keepalive time for the MQTT connection.
Instance Variable lowLevelLogging Indicator whether to log MQTT messages.
Instance Variable messageHandler The message handler.
Instance Variable mqttClient The MQTT client.
Instance Variable mqttsCertfile The certificate file for the MQTT client.
Instance Variable mqttsKeyfile The key file for the MQTT client.
Instance Variable password The password for the MQTT broker.
Instance Variable port The port of the MQTT broker.
Instance Variable subscribedCount The number of subscribed-to topics.
Instance Variable subscribedTopics The list of subscribed-to topics.
Instance Variable username The username for the MQTT broker.
Instance Variable useTLS Whether to use TLS for the MQTT connection.
Instance Variable verifyCertificate Indicator whether to verify the MQTT broker's certificate.
Instance Variable websocketPath The websocket path to use (incase of websockets).
Method _mqttActor BackgroundWorker callback to run the actuall MQTT loop.
Method _onConnect Callback when the MQTT client connected to the broker.
Method _onDisconnect Callback when the MQTT client disconnected from the broker.
Method _onLog Mapping of the paho MQTT client's log to the logging system. Also handles different log-level scheme.
Method _onMessage Handle a received message. Forward it to the apropriate handler callback (in another Thread).
Method _onSubscribe Callback when the client successfulle subscribed to a topic. The topic is also added to the internal topic list.
Method _onUnsubscribe Callback when the client successfulle unsubscribed from a topic. The topic is also removed from the internal topic list.
def __init__(self, address: str, port: int | None = None, keepalive: int = 60, interface: str = '0.0.0.0', clientID: str | None = None, username: str | None = None, password: str | None = None, useTLS: bool = False, caFile: str | None = None, verifyCertificate: bool = False, certfile: str | None = None, keyfile: str | None = None, lowLevelLogging: bool = True, messageHandler: MQTTHandler | None = None, enableWebSocket: bool | None = False, webSocketPort: int | None = 8080, websocketPath: str | None = None):

Constructor. Initialize the MQTT client.

Parameters
address:strThe address of the MQTT broker.
port:int | NoneThe port of the MQTT broker.
keepalive:intThe keepalive time for the MQTT connection.
interface:strThe interface to bind to.
clientID:str | NoneThe client ID for the MQTT client.
username:str | NoneThe username for the MQTT broker.
password:str | NoneThe password for the MQTT broker.
useTLS:boolWhether to use TLS for the MQTT connection.
caFile:str | NoneThe CA file for the MQTT broker's certificate.
verifyCertificate:boolIndicator whether to verify the MQTT broker's certificate.
certfile:str | NoneThe certificate file for the MQTT client.
keyfile:str | NoneThe key file for the MQTT client.
lowLevelLogging:boolIndicator whether to log MQTT messages.
messageHandler:MQTTHandler | NoneThe message handler.
enableWebSocket:bool | NoneWhether to enable WebSocket support.
webSocketPort:int | NoneThe port to use for WebSocket connections.
websocketPath:str | NoneThe websocket path to use (incase of websockets).
def __str__(self) -> str:

Return a string representation of the MQTTConnection instance.

Returns
strA string representation of the MQTTConnection instance.
def isFullySubscribed(self) -> bool:

Check whether the number managed subscriptions matches the number of currently subscribed-to topics.

Returns
boolTrue if fully subscribed, False otherwise.
def publish(self, topic: str, data: bytes):

Publish the message data with the topic topic with the MQTT broker.

Parameters
topic:strThe topic to publish to.
data:bytesThe data to publish.
def run(self):

Initialize and run the MQTT client as a BackgroundWorker/Actor.

def shutdown(self) -> bool:

Shutting down the MQTT client.

Returns
boolTrue if successful, False otherwise.
def subscribeTopic(self, topic: str | list[str], callback: MQTTCallback | None = None, **kwargs: Any):

Add one or more MQTT topics to subscribe to. Add the topic(s) afterwards to the list of subscribed-to topics.

Parameters
topic:str | list[str]The topic(s) to subscribe to. Either a single topic or a list of topics.
callback:MQTTCallback | NoneThe callback function to call when a message is received for the topic.
**kwargs:AnyAdditional arguments for the callback function.
def unsubscribeTopic(self, topic: str | MQTTTopic):

Unsubscribe from a topic. topic is either an MQTTTopic structure with a previously subscribed to topic, or a topic name, in which case it is searched for in the list of MQTTTopics.

Parameters
topic:str | MQTTTopicThe topic to unsubscribe from.
__slots__: tuple[str, ...] =

Slots of the class.

actor: BackgroundWorker | None =

The actor for the MQTT client.

address =

The address of the MQTT broker.

bindIF =

The interface to bind to.

caFile =

The CA file for the MQTT broker's certificate.

clientID =

The client ID for the MQTT client.

enableWebSocket =

Whether to enable WebSocket support.

isConnected: bool =

Indicator whether the MQTT client is connected.

isStopped: bool =

Indicator whether the MQTT client is stopped.

keepalive =

The keepalive time for the MQTT connection.

lowLevelLogging =

Indicator whether to log MQTT messages.

messageHandler: MQTTHandler | None =

The message handler.

mqttClient: MQTTClient | None =

The MQTT client.

mqttsCertfile =

The certificate file for the MQTT client.

mqttsKeyfile =

The key file for the MQTT client.

password: str | None =

The password for the MQTT broker.

port =

The port of the MQTT broker.

subscribedCount: int =

The number of subscribed-to topics.

subscribedTopics: dict[str, MQTTTopic] =

The list of subscribed-to topics.

username: str | None =

The username for the MQTT broker.

useTLS: bool =

Whether to use TLS for the MQTT connection.

verifyCertificate =

Indicator whether to verify the MQTT broker's certificate.

websocketPath =

The websocket path to use (incase of websockets).

def _mqttActor(self) -> bool:

BackgroundWorker callback to run the actuall MQTT loop.

Returns
boolAlways True.
def _onConnect(self, client: MQTTClient, userdata: Any, flags: dict, reason_code: mqtt_rc.ReasonCode, properties: mqtt_pr.Properties):

Callback when the MQTT client connected to the broker.

Parameters
client:MQTTClientThe MQTT client.
userdata:AnyUser data.
flags:dictFlags.
reason_code:mqtt_rc.ReasonCodeReason code
properties:mqtt_pr.PropertiesProperties (MQTTv5 Only)
def _onDisconnect(self, client: MQTTClient, userdata: Any, disconnect_flags: mqtt.DisconnectFlags, reason_code: mqtt_rc.ReasonCode, properties: mqtt_pr.Properties):

Callback when the MQTT client disconnected from the broker.

Parameters
client:MQTTClientThe MQTT client.
userdata:AnyUser data.
disconnect_flags:mqtt.DisconnectFlagsUndocumented
reason_code:mqtt_rc.ReasonCodeReason code
properties:mqtt_pr.PropertiesProperties (MQTTv5 Only)
def _onLog(self, client: MQTTClient, userdata: Any, level: int, buf: str):

Mapping of the paho MQTT client's log to the logging system. Also handles different log-level scheme.

Parameters
client:MQTTClientThe MQTT client.
userdata:AnyUser data.
level:intLog level.
buf:strLog message.
def _onMessage(self, client: MQTTClient, userdata: Any, message: mqtt.MQTTMessage):

Handle a received message. Forward it to the apropriate handler callback (in another Thread).

Parameters
client:MQTTClientThe MQTT client.
userdata:AnyUser data.
message:mqtt.MQTTMessageThe received message.
def _onSubscribe(self, client: MQTTClient, userdata: Any, mid: int, reason_codes: list[mqtt_rc.ReasonCode], properties: mqtt_pr.Properties):

Callback when the client successfulle subscribed to a topic. The topic is also added to the internal topic list.

Parameters
client:MQTTClientThe MQTT client.
userdata:AnyUser data.
mid:intThe message ID.
reason_codes:list[mqtt_rc.ReasonCode]Reason codes received from the broker for each subscription
properties:mqtt_pr.PropertiesProperties (MQTTv5 Only)
def _onUnsubscribe(self, client: MQTTClient, userdata: Any, mid: int, reason_codes: list[mqtt_rc.ReasonCode], properties: mqtt_pr.Properties):

Callback when the client successfulle unsubscribed from a topic. The topic is also removed from the internal topic list.