Pepijn De Winter
Pepijn De Winter

Reputation: 1

paho-mqtt 2.x - on_disconnect() callback - 5 arguments were given

While upgrading to paho-mqqt 2.x (python) the callback functions were adapted to work with version2. However for the on_disconnect() callback I get the error that the number of arguments are not correct. It seems 5 are being provided (instead of 4), however in the code I only have 4 listed? It is unclear to me from were these 5 arguments are coming.

stack trace:

Code:

def connect_mqtt(client_id, username, password) -> mqtt_client:
    def on_connect(client, userdata, flags, reason_code, properties):
        if reason_code == 0:
            writeLog("Connected to MQTT Broker: " + client._client_id.decode('utf-8'),0)   
            if client._client_id.decode('utf-8') == client_id_p:
                globals()["flag_connected_p"] = True                    
            if client._client_id.decode('utf-8') == client_id_s:
                globals()["flag_connected_s"] = True
                writeLog("Subscribing to topic: " + topic_s + " With client: " + client._client_id.decode('utf-8'),0)
                subscribe(client, topic_s)
        else:
            writeLog("Failed to connect to client: " + client._client_id.decode('utf-8') + ", error code:" + str(reason_code),0)
            writeLog("1: Connection refused - incorrect protocol version",0)
            writeLog("2: Connection refused - invalid client identifier",0)
            writeLog("3: Connection refused - server unavailable",0)
            writeLog("4: Connection refused - bad username or password",0)
            writeLog("5: Connection refused - not authorised",0)            

    def on_disconnect(client, userdata, reason_code, properties):
        if reason_code == 0:
            writeLog("Requested disconnect MQTT excuted sucesfully for client: !" + client._client_id.decode('utf-8'),0)                   
        else:
            writeLog("Unexcpected disconnect from MQTT for client: " + client._client_id.decode('utf-8') + ", rc=" + str(reason_code),0)
        if client._client_id.decode('utf-8') == client_id_p:
            globals()["flag_connected_p"] = False
        if client._client_id.decode('utf-8') == client_id_s:
            globals()["flag_connected_s"] = False             

    def on_publish(client, userdata, mid, reason_codes, properties):
        writeLog("published message "+ str(mid) + " From client: " + client._client_id.decode('utf-8') + " sucesfully",1) 

    client = mqtt_client.Client(mqtt.CallbackAPIVersion.VERSION2,client_id, CleanSession)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_publish = on_publish
    client.max_inflight_messages_set(1000)
    client.connect(broker, port)
    return client

Code worked fine on paho-mqtt 1.x, callbacks were upgraded to match documentation for 2.x. I only pass 4 arguments in my callback implementation however trace shows there were 5?

Upvotes: 0

Views: 654

Answers (1)

NPike
NPike

Reputation: 13254

Try

def on_disconnect(client, userdata, flags, reason_code, properties):

Upvotes: 1

Related Questions