@@ -99,21 +99,24 @@ def _mqtt_disconnect(self, client, userdata, rc):
9999
100100 def _mqtt_message (self , client , userdata , msg ):
101101 logger .debug ('Client on_message called.' )
102- # Parse out the feed id and call on_message callback.
103- # Assumes topic looks like "username/feeds/id"
102+ """Parse out the topic and call on_message callback
103+ assume topic looks like `username/topic/id`
104+ """
104105 parsed_topic = msg .topic .split ('/' )
105106 if self .on_message is not None :
106- feed = parsed_topic [2 ]
107+ topic = parsed_topic [2 ]
107108 payload = '' if msg .payload is None else msg .payload .decode ('utf-8' )
108109 elif self .on_message is not None and parsed_topic [0 ] == 'time' :
109- feed = parsed_topic [0 ]
110+ topic = parsed_topic [0 ]
111+ payload = msg .payload .decode ('utf-8' )
112+ elif self .on_message is not None and parsed_topic [1 ] == 'groups' :
113+ topic = parsed_topic [3 ]
110114 payload = msg .payload .decode ('utf-8' )
111- self .on_message (self , feed , payload )
115+ self .on_message (self , topic , payload )
112116
113117 def _mqtt_subscribe (client , userdata , mid , granted_qos ):
114118 """Called when broker responds to a subscribe request."""
115119
116-
117120 def connect (self , ** kwargs ):
118121 """Connect to the Adafruit.IO service. Must be called before any loop
119122 or publish operations are called. Will raise an exception if a
@@ -179,15 +182,21 @@ def subscribe(self, feed_id, feed_user=None):
179182 the on_message function will be called with the feed_id and new value.
180183
181184 Params:
182- - feed_id: The id of the feed to update .
183- - feed_user (optional): The user id of the feed. Used for feed sharing.
185+ - feed_id: The id of the feed to subscribe to .
186+ - feed_user (optional): The user id of the feed. Used for feed sharing functionality .
184187 """
185188 if feed_user is not None :
186189 (res , mid ) = self ._client .subscribe ('{0}/feeds/{1}' .format (feed_user , feed_id ))
187190 else :
188191 (res , mid ) = self ._client .subscribe ('{0}/feeds/{1}' .format (self ._username , feed_id ))
189192 return res , mid
190193
194+ def subscribe_group (self , group_id ):
195+ """Subscribe to changes on the specified group. When the group is updated
196+ the on_message function will be called with the group_id and the new value.
197+ """
198+ self ._client .subscribe ('{0}/groups/{1}' .format (self ._username , group_id ))
199+
191200 def subscribe_time (self , time ):
192201 """Subscribe to changes on the Adafruit IO time feeds. When the feed is
193202 updated, the on_message function will be called and publish a new value:
@@ -204,24 +213,34 @@ def subscribe_time(self, time):
204213 raise TypeError ('Invalid Time Feed Specified.' )
205214 return
206215
207- def unsubscribe (self , feed_id ):
208- """Unsubscribes from a specified MQTT feed.
209- Note: this does not prevent publishing to a feed, it will unsubscribe
210- from receiving messages via on_message.
211- """
212- (res , mid ) = self ._client .unsubscribe ('{0}/feeds/{1}' .format (self ._username , feed_id ))
213-
214- def publish (self , feed_id , value = None , feed_user = None ):
216+ def unsubscribe (self , feed_id = None , group_id = None ):
217+ """Unsubscribes from a specified MQTT topic.
218+ Note: this does not prevent publishing to a topic, it will unsubscribe
219+ from receiving messages via on_message.
220+ """
221+ if feed_id is not None :
222+ self ._client .unsubscribe ('{0}/feeds/{1}' .format (self ._username , feed_id ))
223+ elif group_id is not None :
224+ self ._client .unsubscribe ('{0}/groups/{1}' .format (self ._username , group_id ))
225+ else :
226+ raise TypeError ('Invalid topic type specified.' )
227+ return
228+
229+ def publish (self , feed_id , value = None , group_id = None , feed_user = None ):
215230 """Publish a value to a specified feed.
216231
217232 Params:
218233 - feed_id: The id of the feed to update.
219- - feed_user (optional): The user id of the feed. Used for feed sharing.
220234 - value: The new value to publish to the feed.
235+ - (optional) group_id: The id of the group to update.
236+ - (optional) feed_user: The feed owner's username. Used for Sharing Feeds.
221237 """
222- if feed_user is not None :
223- (res , self ._pub_mid ) = self ._client .publish ('{0}/feeds/{1}' .format (feed_user , feed_id ),
224- payload = value )
225- else :
226- (res , self ._pub_mid ) = self ._client .publish ('{0}/feeds/{1}' .format (self ._username , feed_id ),
227- payload = value )
238+ if feed_user is not None : # shared feed
239+ (res , self ._pub_mid ) = self ._client .publish ('{0}/feeds/{1}' .format (feed_user , feed_id ),
240+ payload = value )
241+ elif group_id is not None : # group-specified feed
242+ self ._client .publish ('{0}/feeds/{1}.{2}' .format (self ._username , group_id , feed_id ),
243+ payload = value )
244+ else : # regular feed
245+ (res , self ._pub_mid ) = self ._client .publish ('{0}/feeds/{1}' .format (self ._username , feed_id ),
246+ payload = value )
0 commit comments