@@ -92,9 +92,11 @@ def __init__(self, mqtt_client):
92
92
self ._client .on_connect = self ._on_connect_mqtt
93
93
self ._client .on_disconnect = self ._on_disconnect_mqtt
94
94
self ._client .on_message = self ._on_message_mqtt
95
+ self ._client .on_subscribe = self ._on_subscribe_mqtt
96
+ self ._client .on_unsubscribe = self ._on_unsubscribe_mqtt
95
97
self ._logger = False
96
98
if self ._client ._logger is not None :
97
- # Allow IOTCore to share MiniMQTT Client's logger
99
+ # Allow MQTT_API to utilize MiniMQTT Client's logger
98
100
self ._logger = True
99
101
self ._client .set_logger_level ("DEBUG" )
100
102
self ._connected = False
@@ -163,13 +165,31 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
163
165
164
166
# pylint: disable=not-callable
165
167
def _on_message_mqtt (self , client , topic , payload ):
166
- """Runs when the client calls on_message
168
+ """Runs when the client calls on_message.
167
169
"""
168
170
if self ._logger :
169
171
self ._client ._logger .debug ("Client called on_message" )
170
172
if self .on_message is not None :
171
173
self .on_message (self , topic , payload )
172
174
175
+ # pylint: disable=not-callable
176
+ def _on_subscribe_mqtt (self , client , user_data , topic , qos ):
177
+ """Runs when the client calls on_subscribe.
178
+ """
179
+ if self ._logger :
180
+ self ._client ._logger .debug ("Client called on_subscribe" )
181
+ if self .on_subscribe is not None :
182
+ self .on_subscribe (self , user_data , topic , qos )
183
+
184
+ # pylint: disable=not-callable
185
+ def _on_unsubscribe_mqtt (self , client , user_data , topic , pid ):
186
+ """Runs when the client calls on_unsubscribe.
187
+ """
188
+ if self ._logger :
189
+ self ._client ._logger .debug ("Client called on_unsubscribe" )
190
+ if self .on_unsubscribe is not None :
191
+ self .on_unsubscribe (self , user_data , topic , pid )
192
+
173
193
def loop (self ):
174
194
"""Maintains a connection with Google Cloud IoT Core's MQTT broker. You will
175
195
need to manually call this method within a loop to retain connection.
@@ -191,6 +211,25 @@ def loop_blocking(self):
191
211
"""
192
212
self ._client .loop_forever ()
193
213
214
+ def unsubscribe (self , topic , subfolder = None ):
215
+ """Unsubscribes from a Google Cloud IoT device topic.
216
+ :param str topic: Required MQTT topic. Defaults to events.
217
+ :param str subfolder: Optional MQTT topic subfolder. Defaults to None.
218
+
219
+ """
220
+ if subfolder is not None :
221
+ mqtt_topic = "/devices/{}/{}/{}" .format (self .device_id , topic , subfolder )
222
+ else :
223
+ mqtt_topic = "/devices/{}/{}" .format (self .device_id , topic )
224
+ self ._client .unsubscribe (mqtt_topic )
225
+
226
+ def unsubscribe_from_all_commands (self ):
227
+ """Unsubscribes from a device's "commands/#" topic.
228
+ :param int qos: Quality of Service level for the message.
229
+
230
+ """
231
+ self .unsubscribe ("commands/#" )
232
+
194
233
def subscribe (self , topic , subfolder = None , qos = 1 ):
195
234
"""Subscribes to a Google Cloud IoT device topic.
196
235
:param str topic: Required MQTT topic. Defaults to events.
0 commit comments