From 972142096e20f64f0232311b098b1c10d4f4f581 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Sun, 16 Oct 2022 07:41:50 +0300 Subject: [PATCH 1/3] Refactored old is_connected to _connected to maintain error checking purpose. Created new is_connected that returns a bool. --- adafruit_minimqtt/adafruit_minimqtt.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 743fdbc..f98d3a6 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -561,7 +561,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None): def disconnect(self): """Disconnects the MiniMQTT client from the MQTT broker.""" - self.is_connected() + self._connected() if self.logger is not None: self.logger.debug("Sending DISCONNECT packet to broker") try: @@ -582,7 +582,7 @@ def ping(self): there is an active network connection. Returns response codes of any messages received while waiting for PINGRESP. """ - self.is_connected() + self._connected() if self.logger is not None: self.logger.debug("Sending PINGREQ") self._sock.send(MQTT_PINGREQ) @@ -607,7 +607,7 @@ def publish(self, topic, msg, retain=False, qos=0): :param int qos: Quality of Service level for the message, defaults to zero. """ - self.is_connected() + self._connected() self._valid_topic(topic) if "+" in topic or "#" in topic: raise MMQTTException("Publish topic can not contain wildcards.") @@ -703,7 +703,7 @@ def subscribe(self, topic, qos=0): (send at least once), or ``2`` (send exactly once). """ - self.is_connected() + self._connected() topics = None if isinstance(topic, tuple): topic, qos = topic @@ -1046,7 +1046,7 @@ def _valid_qos(qos_level): else: raise MMQTTException("QoS must be an integer.") - def is_connected(self): + def _connected(self): """Returns MQTT client session status as True if connected, raises a `MMQTTException` if `False`. """ @@ -1054,6 +1054,15 @@ def is_connected(self): raise MMQTTException("MiniMQTT is not connected.") return self._is_connected + def is_connected(self): + """Returns MQTT client session status as True if connected, False + if not. + """ + try: + return self._connected() + except MMQTTException: + return False + # Logging def enable_logger(self, logger, log_level=20): """Enables library logging provided a logger object. From 8097b80d4a93f0380d1755fb932a2250d80a84c1 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 1 Nov 2022 18:11:46 +0700 Subject: [PATCH 2/3] Made requested changes. --- adafruit_minimqtt/adafruit_minimqtt.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index f98d3a6..7901205 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -1050,18 +1050,14 @@ def _connected(self): """Returns MQTT client session status as True if connected, raises a `MMQTTException` if `False`. """ - if self._sock is None or self._is_connected is False: - raise MMQTTException("MiniMQTT is not connected.") - return self._is_connected + if not self.is_connected(): + raise MQTTException("MiniMQTT is not connected") def is_connected(self): """Returns MQTT client session status as True if connected, False if not. """ - try: - return self._connected() - except MMQTTException: - return False + return self._is_connected and self._sock is not None # Logging def enable_logger(self, logger, log_level=20): From 5c4d43b91668fcfa8cfcaa17d0494c581a9c2eb2 Mon Sep 17 00:00:00 2001 From: BiffoBear Date: Tue, 1 Nov 2022 18:29:13 +0700 Subject: [PATCH 3/3] Fixed a typo. --- adafruit_minimqtt/adafruit_minimqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 7901205..d654fd7 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -1051,7 +1051,7 @@ def _connected(self): a `MMQTTException` if `False`. """ if not self.is_connected(): - raise MQTTException("MiniMQTT is not connected") + raise MMQTTException("MiniMQTT is not connected") def is_connected(self): """Returns MQTT client session status as True if connected, False