@@ -19,15 +19,6 @@ class Communicator:
1919 mqtt = None
2020 enocean = None
2121
22- CONNECTION_RETURN_CODE = [
23- "connection successful" ,
24- "incorrect protocol version" ,
25- "invalid client identifier" ,
26- "server unavailable" ,
27- "bad username or password" ,
28- "not authorised" ,
29- ]
30-
3122 def __init__ (self , config , sensors ):
3223 self .conf = config
3324 self .sensors = sensors
@@ -40,7 +31,7 @@ def __init__(self, config, sensors):
4031
4132 # setup mqtt connection
4233 client_id = self .conf ['mqtt_client_id' ] if 'mqtt_client_id' in self .conf else ''
43- self .mqtt = mqtt .Client (client_id = client_id )
34+ self .mqtt = mqtt .Client (mqtt . CallbackAPIVersion . VERSION2 , client_id = client_id )
4435 self .mqtt .on_connect = self ._on_connect
4536 self .mqtt .on_disconnect = self ._on_disconnect
4637 self .mqtt .on_message = self ._on_mqtt_message
@@ -78,27 +69,23 @@ def __del__(self):
7869 #=============================================================================================
7970 # MQTT CLIENT
8071 #=============================================================================================
81- def _on_connect (self , mqtt_client , _userdata , _flags , return_code ):
72+ def _on_connect (self , mqtt_client , _userdata , _flags , reason_code , properties ):
8273 '''callback for when the client receives a CONNACK response from the MQTT server.'''
83- if return_code == 0 :
74+ if reason_code == 0 :
8475 logging .info ("Succesfully connected to MQTT broker." )
8576 # listen to enocean send requests
8677 for cur_sensor in self .sensors :
8778 # logging.debug("MQTT subscribing: %s", cur_sensor['name']+'/req/#')
8879 mqtt_client .subscribe (cur_sensor ['name' ]+ '/req/#' )
8980 else :
90- logging .error ("Error connecting to MQTT broker: %s" ,
91- self .CONNECTION_RETURN_CODE [return_code ]
92- if return_code < len (self .CONNECTION_RETURN_CODE ) else return_code )
81+ logging .error ("Error connecting to MQTT broker: %s" , reason_code )
9382
94- def _on_disconnect (self , _mqtt_client , _userdata , return_code ):
83+ def _on_disconnect (self , _mqtt_client , _userdata , reason_code , properties ):
9584 '''callback for when the client disconnects from the MQTT server.'''
96- if return_code == 0 :
85+ if reason_code == 0 :
9786 logging .warning ("Successfully disconnected from MQTT broker" )
9887 else :
99- logging .warning ("Unexpectedly disconnected from MQTT broker: %s" ,
100- self .CONNECTION_RETURN_CODE [return_code ]
101- if return_code < len (self .CONNECTION_RETURN_CODE ) else return_code )
88+ logging .warning ("Unexpectedly disconnected from MQTT broker: %s" , reason_code )
10289
10390 def _on_mqtt_message (self , _mqtt_client , _userdata , msg ):
10491 '''the callback for when a PUBLISH message is received from the MQTT server.'''
@@ -120,7 +107,7 @@ def _on_mqtt_message(self, _mqtt_client, _userdata, msg):
120107 if not found_topic :
121108 logging .warning ("Unexpected or erroneous MQTT message: %s: %s" , msg .topic , msg .payload )
122109
123- def _on_mqtt_publish (self , _mqtt_client , _userdata , _mid ):
110+ def _on_mqtt_publish (self , _mqtt_client , _userdata , _mid , _reason_codes , _properties ):
124111 '''the callback for when a PUBLISH message is successfully sent to the MQTT server.'''
125112 #logging.debug("Published MQTT message "+str(mid))
126113
0 commit comments