7979_the_interface = None # pylint: disable=invalid-name
8080_the_sock = None # pylint: disable=invalid-name
8181
82+
8283class MMQTTException (Exception ):
8384 """MiniMQTT Exception class."""
8485
@@ -95,10 +96,11 @@ def set_socket(sock, iface=None):
9596 global _the_sock # pylint: disable=invalid-name, global-statement
9697 _the_sock = sock
9798 if iface :
98- global _the_interface # pylint: disable=invalid-name, global-statement
99+ global _the_interface # pylint: disable=invalid-name, global-statement
99100 _the_interface = iface
100101 _the_sock .set_interface (iface )
101102
103+
102104class MQTT :
103105 """MQTT Client for CircuitPython
104106 :param str broker: MQTT Broker URL or IP Address.
@@ -114,14 +116,22 @@ class MQTT:
114116 """
115117
116118 # pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
117- def __init__ (self , broker , port = None , username = None ,
118- password = None , client_id = None ,
119- is_ssl = True , log = False , keep_alive = 60 ):
119+ def __init__ (
120+ self ,
121+ broker ,
122+ port = None ,
123+ username = None ,
124+ password = None ,
125+ client_id = None ,
126+ is_ssl = True ,
127+ log = False ,
128+ keep_alive = 60 ,
129+ ):
120130 self ._sock = None
121131 # broker
122- try : # set broker IP
132+ try : # set broker IP
123133 self .broker = _the_interface .unpretty_ip (broker )
124- except ValueError : # set broker URL
134+ except ValueError : # set broker URL
125135 self .broker = broker
126136 # port/ssl
127137 self .port = MQTT_TCP_PORT
@@ -230,20 +240,26 @@ def connect(self, clean_session=True):
230240 self .broker , port = self .broker .split (":" , 1 )
231241 port = int (port )
232242
233- addr = _the_sock .getaddrinfo (self .broker , self .port , 0 , _the_sock .SOCK_STREAM )[0 ]
243+ addr = _the_sock .getaddrinfo (self .broker , self .port , 0 , _the_sock .SOCK_STREAM )[
244+ 0
245+ ]
234246 self ._sock = _the_sock .socket (addr [0 ], addr [1 ], addr [2 ])
235247 self ._sock .settimeout (15 )
236248 if self .port == 8883 :
237249 try :
238250 if self .logger is not None :
239- self .logger .debug ('Attempting to establish secure MQTT connection...' )
251+ self .logger .debug (
252+ "Attempting to establish secure MQTT connection..."
253+ )
240254 self ._sock .connect ((self .broker , self .port ), _the_interface .TLS_MODE )
241255 except RuntimeError as e :
242256 raise MMQTTException ("Invalid broker address defined." , e )
243257 else :
244258 try :
245259 if self .logger is not None :
246- self .logger .debug ('Attempting to establish insecure MQTT connection...' )
260+ self .logger .debug (
261+ "Attempting to establish insecure MQTT connection..."
262+ )
247263 self ._sock .connect (addr [- 1 ], TCP_MODE )
248264 except RuntimeError as e :
249265 raise MMQTTException ("Invalid broker address defined." , e )
@@ -386,9 +402,9 @@ def publish(self, topic, msg, retain=False, qos=0):
386402 raise MMQTTException ("Publish topic can not contain wildcards." )
387403 # check msg/qos kwargs
388404 if msg is None :
389- raise MMQTTException (' Message can not be None.' )
405+ raise MMQTTException (" Message can not be None." )
390406 if isinstance (msg , (int , float )):
391- msg = str (msg ).encode (' ascii' )
407+ msg = str (msg ).encode (" ascii" )
392408 elif isinstance (msg , str ):
393409 msg = str (msg ).encode ("utf-8" )
394410 else :
@@ -628,8 +644,10 @@ def loop(self):
628644 if current_time - self ._timestamp >= self .keep_alive :
629645 # Handle KeepAlive by expecting a PINGREQ/PINGRESP from the server
630646 if self .logger is not None :
631- self .logger .debug ('KeepAlive period elapsed - \
632- requesting a PINGRESP from the server...' )
647+ self .logger .debug (
648+ "KeepAlive period elapsed - \
649+ requesting a PINGRESP from the server..."
650+ )
633651 self .ping ()
634652 self ._timestamp = 0
635653 self ._sock .settimeout (0.1 )
@@ -699,10 +717,10 @@ def _check_topic(topic):
699717 raise MMQTTException ("Topic may not be NoneType" )
700718 # [MQTT-4.7.3-1]
701719 if not topic :
702- raise MMQTTException (' Topic may not be empty.' )
720+ raise MMQTTException (" Topic may not be empty." )
703721 # [MQTT-4.7.3-3]
704- if len (topic .encode (' utf-8' )) > MQTT_TOPIC_LENGTH_LIMIT :
705- raise MMQTTException (' Topic length is too large.' )
722+ if len (topic .encode (" utf-8" )) > MQTT_TOPIC_LENGTH_LIMIT :
723+ raise MMQTTException (" Topic length is too large." )
706724
707725 @staticmethod
708726 def _check_qos (qos_level ):
0 commit comments