Skip to content

Commit 6d25933

Browse files
author
brentru
committed
tie together unsuback and subacks
1 parent e5247c6 commit 6d25933

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

adafruit_gc_iot_core.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ def __init__(self, mqtt_client):
9292
self._client.on_connect = self._on_connect_mqtt
9393
self._client.on_disconnect = self._on_disconnect_mqtt
9494
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
9597
self._logger = False
9698
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
98100
self._logger = True
99101
self._client.set_logger_level("DEBUG")
100102
self._connected = False
@@ -163,13 +165,31 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
163165

164166
# pylint: disable=not-callable
165167
def _on_message_mqtt(self, client, topic, payload):
166-
"""Runs when the client calls on_message
168+
"""Runs when the client calls on_message.
167169
"""
168170
if self._logger:
169171
self._client._logger.debug("Client called on_message")
170172
if self.on_message is not None:
171173
self.on_message(self, topic, payload)
172174

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+
173193
def loop(self):
174194
"""Maintains a connection with Google Cloud IoT Core's MQTT broker. You will
175195
need to manually call this method within a loop to retain connection.
@@ -191,6 +211,25 @@ def loop_blocking(self):
191211
"""
192212
self._client.loop_forever()
193213

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+
194233
def subscribe(self, topic, subfolder=None, qos=1):
195234
"""Subscribes to a Google Cloud IoT device topic.
196235
:param str topic: Required MQTT topic. Defaults to events.

0 commit comments

Comments
 (0)