Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed is_connected to return bool while maintaining error checking #125

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
BiffoBear marked this conversation as resolved.
Show resolved Hide resolved
"""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