Skip to content

Fix LWT Issues #36

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

Merged
merged 1 commit into from
Jun 4, 2020
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
30 changes: 19 additions & 11 deletions adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ def __init__(
self._msg_size_lim = MQTT_MSG_SZ_LIM
self._pid = 0
self._timestamp = 0
# LWT
self._lw_topic = None
self._lw_qos = 0
self._lw_topic = None
self._lw_msg = None
self._lw_retain = False
# List of subscribed topics, used for tracking
self._subscribed_topics = []
# Server callbacks
Expand All @@ -174,7 +180,6 @@ def __init__(
self.on_publish = None
self.on_subscribe = None
self.on_unsubscribe = None
self.last_will()

def __enter__(self):
return self
Expand All @@ -189,25 +194,28 @@ def deinit(self):
"""
self.disconnect()

def last_will(self, topic=None, message=None, qos=0, retain=False):
def will_set(self, topic=None, payload=None, qos=0, retain=False):
"""Sets the last will and testament properties. MUST be called before connect().
:param str topic: MQTT Broker topic.
:param str message: Last will disconnection message.
:param str payload: Last will disconnection payload.
:param int qos: Quality of Service level.
:param bool retain: Specifies if the message is to be retained when it is published.
:param bool retain: Specifies if the payload is to be retained when it is published.

"""
if self._is_connected:
raise MMQTTException(
"Last Will should be defined before connect() is called."
)
if qos < 0 or qos > 2:
raise MMQTTException("Invalid QoS level, must be between 0 and 2.")
if self.logger is not None:
self.logger.debug("Setting last will properties")
self._check_qos(qos)
if self._is_connected:
raise MMQTTException("Last Will should only be called before connect().")
if payload is None:
raise MMQTTException("Message can not be None.")
if isinstance(payload, (int, float, str)):
payload = str(payload).encode()
else:
raise MMQTTException("Invalid message data type.")
self._lw_qos = qos
self._lw_topic = topic
self._lw_msg = message
self._lw_msg = payload
self._lw_retain = retain

# pylint: disable=too-many-branches, too-many-statements, too-many-locals
Expand Down