Skip to content

Commit

Permalink
Merge pull request #125 from BiffoBear/Make_is_connected_return_bool
Browse files Browse the repository at this point in the history
Changed is_connected to return bool while maintaining error checking
  • Loading branch information
tekktrik authored Nov 10, 2022
2 parents d68d43a + 5c4d43b commit 199a0b8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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.")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1046,13 +1046,18 @@ 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`.
"""
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 MMQTTException("MiniMQTT is not connected")

def is_connected(self):
"""Returns MQTT client session status as True if connected, False
if not.
"""
return self._is_connected and self._sock is not None

# Logging
def enable_logger(self, logger, log_level=20):
Expand Down

0 comments on commit 199a0b8

Please sign in to comment.