You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I recently met a bug when I tried to reinitialize the mqtt client to reuse the client.
importconfigparserimportos.pathimportpaho.mqtt.clientasmqttclassMQTTClientWrapper:
"""Wrapper class for the MQTT client connected to predefined host and port"""def__init__(self, user_data=None, on_message=None, topic=None):
"""Initialize the MQTT client from the configuration file"""config_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), '../conf', 'audio_base.ini')
self.subscribed_topic=Noneself.config=configparser.ConfigParser()
self.config.read(config_path)
self.client=mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self.initialize(user_data, on_message, topic)
defreinit(self, user_data=None, on_message=None, topic=None):
"""Reinitialize the client with new user data, on_message callback, and topic"""self.subscribed_topic=Noneself.client.reinitialise()
self.initialize(user_data, on_message, topic)
definitialize(self, user_data=None, on_message=None, topic=None):
self.client.connect(host=self.config['MQTT']['mqtt_host'], port=int(self.config['MQTT']['mqtt_port']),
keepalive=60)
ifuser_data:
self.user_data_set(user_data)
ifon_message:
self.on_message(on_message)
iftopic:
self.subscribe(topic)
self.subscribed_topic=topicdefuser_data_set(self, user_data):
"""Set the user data for the client"""self.client.user_data_set(user_data)
defsubscribe(self, topic):
"""Unsubscribe from the current topic and subscribe to a new topic"""ifself.subscribed_topic:
self.client.unsubscribe(self.subscribed_topic)
self.client.subscribe(topic)
self.subscribed_topic=topicdefpublish(self, topic, message, retain=False, qos=0):
"""Publish a message to a topic"""self.client.publish(topic, message, retain=retain, qos=qos)
defon_message(self, on_message):
"""Set the on_message callback function"""self.client.on_message=on_messagedefstart(self):
"""Start the network loop in a separate thread"""self.client.loop_start()
defstop(self):
"""Stop the network loop"""self.client.loop_stop()
The errors shown
self.mqtt_client.reinit()
File "/Users/ericli/mbox-audio/utils/mqtt_client.py", line 22, in reinit
self.client.reinitialise()
File "/Users/ericli/miniforge3/envs/audio-base/lib/python3.10/site-packages/paho/mqtt/client.py", line 1150, in reinitialise
self.__init__(client_id, clean_session, userdata) # type: ignore[misc]
File "/Users/ericli/miniforge3/envs/audio-base/lib/python3.10/site-packages/paho/mqtt/client.py", line 766, in __init__
raise ValueError(
ValueError: Unsupported callback API version: version 2.0 added a callback_api_version, see migrations.md for details
Hi, I recently met a bug when I tried to reinitialize the mqtt client to reuse the client.
The errors shown
I check the code of mqtt/client.py
Here, the function call of reinitialise missed the callback_api_version parameter needed in the init function
The text was updated successfully, but these errors were encountered: