Resolved issue with Paho V2 API

Chnaged client creation logic to V2 API as we are already using Paho2.0. Chnaged version selection from Paho version (which should not have been a user choice) to MQTT Protocol selection, which can be v3 or v5. Most modern MQQTT brokers like Mosquitta or EMQX support v5.
This commit is contained in:
Ingo Ratsdorf
2024-08-17 14:00:39 +12:00
parent 7fd8b039ed
commit b7fa32f70a
2 changed files with 27 additions and 16 deletions

View File

@@ -767,8 +767,8 @@
{ "elementType": "select", "elementOptions": [], "transformers": [] } { "elementType": "select", "elementOptions": [], "transformers": [] }
] ]
}, },
"default_value": 1, "default_value": 5,
"options": [1, 2], "options": [3, 5],
"localized": ["name", "description"], "localized": ["name", "description"],
"name": [ "name": [
{ {
@@ -779,7 +779,7 @@
"description": [ "description": [
{ {
"language_code": "en_us", "language_code": "en_us",
"string": "Paho MQTT API version. Depends on the MQTT <a href=\"https://eclipse.dev/paho/files/paho.mqtt.python/html/index.html#callbacks\" target=\"_blank\">version supported by the MQTT broker</a>. Usually set to <code>1</code>." "string": "MQTT Protocol version. Depends on the MQTT broker</a>. Usually set to <code>5</code>, or <code>3</code> for backwards compatibility."
} }
] ]
}, },

View File

@@ -272,19 +272,22 @@ def create_sensor(mqtt_client, deviceId, deviceName, sensorType, sensorName, ico
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def mqtt_create_client(): def mqtt_create_client():
mytransport = 'tcp' # or 'websockets'
def on_disconnect(mqtt_client, userdata, reason_code): def on_disconnect(mqtt_client, userdata, reason_code):
global mqtt_connected_to_broker global mqtt_connected_to_broker
# REF: If we wanted a auto reconnect, a good source is here: https://www.emqx.com/en/blog/how-to-use-mqtt-in-python
mqtt_connected_to_broker = False mqtt_connected_to_broker = False
mylog('debug', [f"[{pluginName}] Connection terminated, reason_code: {reason_code}"])
# not sure is below line is correct / necessary def on_connect(mqtt_client, userdata, flags, reason_code, properties):
# client = mqtt_create_client()
def on_connect(mqtt_client, userdata, flags, reason_code):
global mqtt_connected_to_broker global mqtt_connected_to_broker
# REF: Good docu on reason codes: https://www.emqx.com/en/blog/mqtt5-new-features-reason-code-and-ack
if reason_code == 0: if reason_code == 0:
mylog('verbose', [f"[{pluginName}] Connected to broker"]) mylog('verbose', [f"[{pluginName}] Connected to broker"])
mqtt_connected_to_broker = True # Signal connection mqtt_connected_to_broker = True # Signal connection
@@ -292,21 +295,29 @@ def mqtt_create_client():
mylog('verbose', [f"[{pluginName}] Connection failed, reason_code: {reason_code}"]) mylog('verbose', [f"[{pluginName}] Connection failed, reason_code: {reason_code}"])
mqtt_connected_to_broker = False mqtt_connected_to_broker = False
global mqtt_client global mqtt_client
if get_setting_value('MQTT_VERSION') == 1: # Paho will be soon not supporting V1 anymore, so this really should not be a user choice to start with
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) # This code now uses V2 by default
# Ref: https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html
if get_setting_value('MQTT_VERSION') == 3:
version = mqtt.MQTTv311
else: else:
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) version = mqtt.MQTTv5
mqtt_client = mqtt.Client(
callback_api_version = mqtt.CallbackAPIVersion.VERSION2,
transport=mytransport,
protocol=mqtt.MQTTv5)
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
if get_setting_value('MQTT_TLS'): if get_setting_value('MQTT_TLS'):
mqtt_client.tls_set() mqtt_client.tls_set()
mqtt_client.username_pw_set(get_setting_value('MQTT_USER'), get_setting_value('MQTT_PASSWORD')) mqtt_client.username_pw_set(username = get_setting_value('MQTT_USER'), password = get_setting_value('MQTT_PASSWORD'))
mqtt_client.on_connect = on_connect mqtt_client.connect(host = get_setting_value('MQTT_BROKER'), port = get_setting_value('MQTT_PORT'))
mqtt_client.on_disconnect = on_disconnect
mqtt_client.connect(get_setting_value('MQTT_BROKER'), get_setting_value('MQTT_PORT'))
mqtt_client.loop_start() mqtt_client.loop_start()
return mqtt_client return mqtt_client