@@ -165,6 +165,12 @@ def __init__(
165
165
self ._msg_size_lim = MQTT_MSG_SZ_LIM
166
166
self ._pid = 0
167
167
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
168
174
# List of subscribed topics, used for tracking
169
175
self ._subscribed_topics = []
170
176
# Server callbacks
@@ -174,7 +180,6 @@ def __init__(
174
180
self .on_publish = None
175
181
self .on_subscribe = None
176
182
self .on_unsubscribe = None
177
- self .last_will ()
178
183
179
184
def __enter__ (self ):
180
185
return self
@@ -189,25 +194,28 @@ def deinit(self):
189
194
"""
190
195
self .disconnect ()
191
196
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 ):
193
198
"""Sets the last will and testament properties. MUST be called before connect().
194
199
:param str topic: MQTT Broker topic.
195
- :param str message : Last will disconnection message .
200
+ :param str payload : Last will disconnection payload .
196
201
: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.
198
203
199
204
"""
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." )
206
205
if self .logger is not None :
207
206
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." )
208
216
self ._lw_qos = qos
209
217
self ._lw_topic = topic
210
- self ._lw_msg = message
218
+ self ._lw_msg = payload
211
219
self ._lw_retain = retain
212
220
213
221
# pylint: disable=too-many-branches, too-many-statements, too-many-locals
0 commit comments