Skip to content

Commit 2156b0f

Browse files
author
brentru
committed
fix lwt issues
1 parent ec9b57a commit 2156b0f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

adafruit_minimqtt.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def __init__(
165165
self._msg_size_lim = MQTT_MSG_SZ_LIM
166166
self._pid = 0
167167
self._timestamp = 0
168+
# LWT
169+
self._lw_topic = None
170+
self._lw_qos = 0
171+
self._lw_topic = None
172+
self._lw_msg = None
173+
self._lw_retain = False
168174
# List of subscribed topics, used for tracking
169175
self._subscribed_topics = []
170176
# Server callbacks
@@ -174,7 +180,6 @@ def __init__(
174180
self.on_publish = None
175181
self.on_subscribe = None
176182
self.on_unsubscribe = None
177-
self.last_will()
178183

179184
def __enter__(self):
180185
return self
@@ -189,25 +194,28 @@ def deinit(self):
189194
"""
190195
self.disconnect()
191196

192-
def last_will(self, topic=None, message=None, qos=0, retain=False):
197+
def will_set(self, topic=None, payload=None, qos=0, retain=False):
193198
"""Sets the last will and testament properties. MUST be called before connect().
194199
:param str topic: MQTT Broker topic.
195-
:param str message: Last will disconnection message.
200+
:param str payload: Last will disconnection payload.
196201
:param int qos: Quality of Service level.
197-
:param bool retain: Specifies if the message is to be retained when it is published.
202+
:param bool retain: Specifies if the payload is to be retained when it is published.
198203
199204
"""
200-
if self._is_connected:
201-
raise MMQTTException(
202-
"Last Will should be defined before connect() is called."
203-
)
204-
if qos < 0 or qos > 2:
205-
raise MMQTTException("Invalid QoS level, must be between 0 and 2.")
206205
if self.logger is not None:
207206
self.logger.debug("Setting last will properties")
207+
self._check_qos(qos)
208+
if self._is_connected:
209+
raise MMQTTException("Last Will should only be called before connect().")
210+
if payload is None:
211+
raise MMQTTException("Message can not be None.")
212+
if isinstance(payload, (int, float, str)):
213+
payload = str(payload).encode()
214+
else:
215+
raise MMQTTException("Invalid message data type.")
208216
self._lw_qos = qos
209217
self._lw_topic = topic
210-
self._lw_msg = message
218+
self._lw_msg = payload
211219
self._lw_retain = retain
212220

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

0 commit comments

Comments
 (0)