Skip to content

Commit 5431e6c

Browse files
authored
Merge pull request #27 from brentru/callbacks-for-sub-unsub
Implement network callbacks for Feed Subscribe/Unsubscribe
2 parents 110aa24 + 2095ccb commit 5431e6c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

adafruit_io/adafruit_io.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def __init__(self, mqtt_client):
8484
self._client.on_connect = self._on_connect_mqtt
8585
self._client.on_disconnect = self._on_disconnect_mqtt
8686
self._client.on_message = self._on_message_mqtt
87+
self._client.on_subscribe = self._on_subscribe_mqtt
88+
self._client.on_unsubscribe = self._on_unsubscribe_mqtt
8789
self._logger = False
8890
# Write to the MiniMQTT logger, if avaliable.
8991
if self._client.logger is not None:
@@ -182,6 +184,24 @@ def _on_message_mqtt(self, client, topic, payload):
182184
)
183185
self.on_message(self, topic_name, message)
184186

187+
# pylint: disable=not-callable
188+
def _on_subscribe_mqtt(self, client, user_data, topic, qos):
189+
"""Runs when the client calls on_subscribe.
190+
"""
191+
if self._logger:
192+
self._client._logger.debug("Client called on_subscribe")
193+
if self.on_subscribe is not None:
194+
self.on_subscribe(self, user_data, topic, qos)
195+
196+
# pylint: disable=not-callable
197+
def _on_unsubscribe_mqtt(self, client, user_data, topic, pid):
198+
"""Runs when the client calls on_unsubscribe.
199+
"""
200+
if self._logger:
201+
self._client._logger.debug("Client called on_unsubscribe")
202+
if self.on_unsubscribe is not None:
203+
self.on_unsubscribe(self, user_data, topic, pid)
204+
185205
def loop(self):
186206
"""Manually process messages from Adafruit IO.
187207
Call this method to check incoming subscription messages.

examples/mqtt/adafruit_io_simpletest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def connected(client):
6565
# Subscribe to changes on a feed named DemoFeed.
6666
client.subscribe("DemoFeed")
6767

68+
def subscribe(client, userdata, topic, granted_qos):
69+
# This method is called when the client subscribes to a new feed.
70+
print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos))
71+
72+
def unsubscribe(client, userdata, topic, pid):
73+
# This method is called when the client unsubscribes from a feed.
74+
print('Unsubscribed from {0} with PID {1}'.format(topic, pid))
75+
6876
# pylint: disable=unused-argument
6977
def disconnected(client):
7078
# Disconnected function will be called when the client disconnects.
@@ -96,6 +104,8 @@ def message(client, feed_id, payload):
96104
# Connect the callback methods defined above to Adafruit IO
97105
io.on_connect = connected
98106
io.on_disconnect = disconnected
107+
io.on_subscribe = subscribe
108+
io.on_unsubscribe = unsubscribe
99109
io.on_message = message
100110

101111
# Connect to Adafruit IO

0 commit comments

Comments
 (0)