2222import json
2323import re
2424
25+ try :
26+ from typing import List , Any , Callable , Optional
27+ except ImportError :
28+ pass
29+
2530from adafruit_minimqtt .adafruit_minimqtt import MMQTTException
2631from adafruit_io .adafruit_io_errors import (
2732 AdafruitIO_RequestError ,
3540CLIENT_HEADERS = {"User-Agent" : "AIO-CircuitPython/{0}" .format (__version__ )}
3641
3742
38- def validate_feed_key (feed_key ):
43+ def validate_feed_key (feed_key : str ):
3944 """Validates a provided feed key against Adafruit IO's system rules.
4045 https://learn.adafruit.com/naming-things-in-adafruit-io/the-two-feed-identifiers
4146 """
@@ -143,7 +148,7 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
143148 self .on_disconnect (self )
144149
145150 # pylint: disable=not-callable
146- def _on_message_mqtt (self , client , topic , payload ):
151+ def _on_message_mqtt (self , client , topic : str , payload : str ):
147152 """Runs when the client calls on_message. Parses and returns
148153 incoming data from Adafruit IO feeds.
149154
@@ -195,7 +200,7 @@ def _on_unsubscribe_mqtt(self, client, user_data, topic, pid):
195200 if self .on_unsubscribe is not None :
196201 self .on_unsubscribe (self , user_data , topic , pid )
197202
198- def add_feed_callback (self , feed_key , callback_method ):
203+ def add_feed_callback (self , feed_key : str , callback_method : Callable ):
199204 """Attaches a callback_method to an Adafruit IO feed.
200205 The callback_method function is called when a
201206 new value is written to the feed.
@@ -211,7 +216,7 @@ def add_feed_callback(self, feed_key, callback_method):
211216 "{0}/f/{1}" .format (self ._user , feed_key ), callback_method
212217 )
213218
214- def remove_feed_callback (self , feed_key ):
219+ def remove_feed_callback (self , feed_key : str ):
215220 """Removes a previously registered callback method
216221 from executing whenever feed_key receives new data.
217222
@@ -239,7 +244,12 @@ def loop(self, timeout=1):
239244 self ._client .loop (timeout )
240245
241246 # Subscriptions
242- def subscribe (self , feed_key = None , group_key = None , shared_user = None ):
247+ def subscribe (
248+ self ,
249+ feed_key : str = None ,
250+ group_key : str = None ,
251+ shared_user : Optional [str ] = None ,
252+ ):
243253 """Subscribes to your Adafruit IO feed or group.
244254 Can also subscribe to someone else's feed.
245255
@@ -277,7 +287,7 @@ def subscribe_to_errors(self):
277287 """
278288 self ._client .subscribe ("%s/errors" % self ._user )
279289
280- def subscribe_to_randomizer (self , randomizer_id ):
290+ def subscribe_to_randomizer (self , randomizer_id : int ):
281291 """Subscribes to a random data stream created by the Adafruit IO Words service.
282292
283293 :param int randomizer_id: Random word record you want data for.
@@ -286,7 +296,7 @@ def subscribe_to_randomizer(self, randomizer_id):
286296 "{0}/integration/words/{1}" .format (self ._user , randomizer_id )
287297 )
288298
289- def subscribe_to_weather (self , weather_record , forecast ):
299+ def subscribe_to_weather (self , weather_record : int , forecast : str ):
290300 """Subscribes to a weather forecast using the Adafruit IO PLUS weather
291301 service. This feature is only avaliable to Adafruit IO PLUS subscribers.
292302
@@ -299,7 +309,7 @@ def subscribe_to_weather(self, weather_record, forecast):
299309 )
300310 )
301311
302- def subscribe_to_time (self , time_type ):
312+ def subscribe_to_time (self , time_type : str ):
303313 """Adafruit IO provides some built-in MQTT topics for getting the current server time.
304314
305315 :param str time_type: Current Adafruit IO server time. Can be 'seconds', 'millis', or 'iso'.
@@ -312,7 +322,12 @@ def subscribe_to_time(self, time_type):
312322 else :
313323 self ._client .subscribe ("time/" + time_type )
314324
315- def unsubscribe (self , feed_key = None , group_key = None , shared_user = None ):
325+ def unsubscribe (
326+ self ,
327+ feed_key : str = None ,
328+ group_key : str = None ,
329+ shared_user : Optional [str ] = None ,
330+ ):
316331 """Unsubscribes from an Adafruit IO feed or group.
317332 Can also subscribe to someone else's feed.
318333
@@ -345,11 +360,13 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
345360 raise AdafruitIO_MQTTError ("Must provide a feed_key or group_key." )
346361
347362 # Publishing
348- def publish_multiple (self , feeds_and_data , timeout = 3 , is_group = False ):
363+ def publish_multiple (
364+ self , feeds_and_data : List , timeout : int = 3 , is_group : bool = False
365+ ):
349366 """Publishes multiple data points to multiple feeds or groups with a variable
350367 timeout.
351368
352- :param str feeds_and_data: List of tuples containing topic strings and data values.
369+ :param list feeds_and_data: List of tuples containing topic strings and data values.
353370 :param int timeout: Delay between publishing data points to Adafruit IO, in seconds.
354371 :param bool is_group: Set to True if you're publishing to a group.
355372
@@ -373,8 +390,15 @@ def publish_multiple(self, feeds_and_data, timeout=3, is_group=False):
373390 time .sleep (timeout )
374391
375392 # pylint: disable=too-many-arguments
376- def publish (self , feed_key , data , metadata = None , shared_user = None , is_group = False ):
377- """Publishes to an An Adafruit IO Feed.
393+ def publish (
394+ self ,
395+ feed_key : str ,
396+ data : str ,
397+ metadata : str = None ,
398+ shared_user : str = None ,
399+ is_group : bool = False ,
400+ ):
401+ """Publishes to an Adafruit IO Feed.
378402
379403 :param str feed_key: Adafruit IO Feed key.
380404 :param str data: Data to publish to the feed or group.
@@ -439,7 +463,7 @@ def publish(self, feed_key, data, metadata=None, shared_user=None, is_group=Fals
439463 else :
440464 self ._client .publish ("{0}/f/{1}" .format (self ._user , feed_key ), data )
441465
442- def get (self , feed_key ):
466+ def get (self , feed_key : str ):
443467 """Calling this method will make Adafruit IO publish the most recent
444468 value on feed_key.
445469 https://io.adafruit.com/api/docs/mqtt.html#retained-values
@@ -484,7 +508,7 @@ def _create_headers(io_headers):
484508 return headers
485509
486510 @staticmethod
487- def _create_data (data , metadata ):
511+ def _create_data (data , metadata : dict ):
488512 """Returns a data payload as expected by the Adafruit IO HTTP API
489513
490514 :param data: Payload value.
@@ -508,15 +532,15 @@ def _handle_error(response):
508532 if response .status_code >= 400 :
509533 raise AdafruitIO_RequestError (response )
510534
511- def _compose_path (self , path ):
535+ def _compose_path (self , path : str ):
512536 """Composes a valid API request path.
513537
514538 :param str path: Adafruit IO API URL path.
515539 """
516540 return "https://io.adafruit.com/api/v2/{0}/{1}" .format (self .username , path )
517541
518542 # HTTP Requests
519- def _post (self , path , payload ):
543+ def _post (self , path : str , payload : Any ):
520544 """
521545 POST data to Adafruit IO
522546
@@ -531,7 +555,7 @@ def _post(self, path, payload):
531555
532556 return json_data
533557
534- def _get (self , path ):
558+ def _get (self , path : str ):
535559 """
536560 GET data from Adafruit IO
537561
@@ -544,7 +568,7 @@ def _get(self, path):
544568 json_data = response .json ()
545569 return json_data
546570
547- def _delete (self , path ):
571+ def _delete (self , path : str ):
548572 """
549573 DELETE data from Adafruit IO.
550574
@@ -559,7 +583,13 @@ def _delete(self, path):
559583 return json_data
560584
561585 # Data
562- def send_data (self , feed_key , data , metadata = None , precision = None ):
586+ def send_data (
587+ self ,
588+ feed_key : str ,
589+ data : str ,
590+ metadata : Optional [dict ] = None ,
591+ precision : Optional [int ] = None ,
592+ ):
563593 """
564594 Sends value data to a specified Adafruit IO feed.
565595
@@ -580,7 +610,7 @@ def send_data(self, feed_key, data, metadata=None, precision=None):
580610 payload = self ._create_data (data , metadata )
581611 self ._post (path , payload )
582612
583- def send_batch_data (self , feed_key , data_list ):
613+ def send_batch_data (self , feed_key : str , data_list : list ):
584614 """
585615 Sends a batch array of data to a specified Adafruit IO feed
586616
@@ -592,7 +622,7 @@ def send_batch_data(self, feed_key, data_list):
592622 data_dict = type (data_list )((data ._asdict () for data in data_list ))
593623 self ._post (path , {"data" : data_dict })
594624
595- def receive_all_data (self , feed_key ):
625+ def receive_all_data (self , feed_key : str ):
596626 """
597627 Get all data values from a specified Adafruit IO feed. Data is
598628 returned in reverse order.
@@ -603,7 +633,7 @@ def receive_all_data(self, feed_key):
603633 path = self ._compose_path ("feeds/{0}/data" .format (feed_key ))
604634 return self ._get (path )
605635
606- def receive_data (self , feed_key ):
636+ def receive_data (self , feed_key : str ):
607637 """
608638 Return the most recent value for the specified feed.
609639
@@ -613,7 +643,7 @@ def receive_data(self, feed_key):
613643 path = self ._compose_path ("feeds/{0}/data/last" .format (feed_key ))
614644 return self ._get (path )
615645
616- def delete_data (self , feed_key , data_id ):
646+ def delete_data (self , feed_key : str , data_id : str ):
617647 """
618648 Deletes an existing Data point from a feed.
619649
@@ -625,7 +655,7 @@ def delete_data(self, feed_key, data_id):
625655 return self ._delete (path )
626656
627657 # Groups
628- def create_new_group (self , group_key , group_description ):
658+ def create_new_group (self , group_key : str , group_description : str ):
629659 """
630660 Creates a new Adafruit IO Group.
631661
@@ -636,7 +666,7 @@ def create_new_group(self, group_key, group_description):
636666 payload = {"name" : group_key , "description" : group_description }
637667 return self ._post (path , payload )
638668
639- def delete_group (self , group_key ):
669+ def delete_group (self , group_key : str ):
640670 """
641671 Deletes an existing group.
642672
@@ -645,7 +675,7 @@ def delete_group(self, group_key):
645675 path = self ._compose_path ("groups/{0}" .format (group_key ))
646676 return self ._delete (path )
647677
648- def get_group (self , group_key ):
678+ def get_group (self , group_key : str ):
649679 """
650680 Returns Group based on Group Key
651681
@@ -654,7 +684,7 @@ def get_group(self, group_key):
654684 path = self ._compose_path ("groups/{0}" .format (group_key ))
655685 return self ._get (path )
656686
657- def create_feed_in_group (self , group_key , feed_name ):
687+ def create_feed_in_group (self , group_key : str , feed_name : str ):
658688 """Creates a new feed in an existing group.
659689
660690 :param str group_key: Group name.
@@ -664,7 +694,7 @@ def create_feed_in_group(self, group_key, feed_name):
664694 payload = {"feed" : {"name" : feed_name }}
665695 return self ._post (path , payload )
666696
667- def add_feed_to_group (self , group_key , feed_key ):
697+ def add_feed_to_group (self , group_key : str , feed_key : str ):
668698 """
669699 Adds an existing feed to a group
670700
@@ -677,7 +707,7 @@ def add_feed_to_group(self, group_key, feed_key):
677707 return self ._post (path , payload )
678708
679709 # Feeds
680- def get_feed (self , feed_key , detailed = False ):
710+ def get_feed (self , feed_key : str , detailed : bool = False ):
681711 """
682712 Returns an Adafruit IO feed based on the feed key
683713
@@ -691,7 +721,12 @@ def get_feed(self, feed_key, detailed=False):
691721 path = self ._compose_path ("feeds/{0}" .format (feed_key ))
692722 return self ._get (path )
693723
694- def create_new_feed (self , feed_key , feed_desc = None , feed_license = None ):
724+ def create_new_feed (
725+ self ,
726+ feed_key : str ,
727+ feed_desc : Optional [str ] = None ,
728+ feed_license : Optional [str ] = None ,
729+ ):
695730 """
696731 Creates a new Adafruit IO feed.
697732
@@ -705,7 +740,11 @@ def create_new_feed(self, feed_key, feed_desc=None, feed_license=None):
705740 return self ._post (path , payload )
706741
707742 def create_and_get_feed (
708- self , feed_key , detailed = False , feed_desc = None , feed_license = None
743+ self ,
744+ feed_key : str ,
745+ detailed : bool = False ,
746+ feed_desc : Optional [str ] = None ,
747+ feed_license : Optional [str ] = None ,
709748 ):
710749 """
711750 Attempts to return a feed; if the feed does not exist, it is created, and then returned.
@@ -723,7 +762,7 @@ def create_and_get_feed(
723762 )
724763 return self .get_feed (feed_key , detailed = detailed )
725764
726- def delete_feed (self , feed_key ):
765+ def delete_feed (self , feed_key : str ):
727766 """
728767 Deletes an existing feed.
729768
@@ -734,7 +773,7 @@ def delete_feed(self, feed_key):
734773 return self ._delete (path )
735774
736775 # Adafruit IO Connected Services
737- def receive_weather (self , weather_id ):
776+ def receive_weather (self , weather_id : int ):
738777 """
739778 Get data from the Adafruit IO Weather Forecast Service
740779 NOTE: This service is avaliable to Adafruit IO Plus subscribers only.
@@ -744,7 +783,7 @@ def receive_weather(self, weather_id):
744783 path = self ._compose_path ("integrations/weather/{0}" .format (weather_id ))
745784 return self ._get (path )
746785
747- def receive_random_data (self , generator_id ):
786+ def receive_random_data (self , generator_id : int ):
748787 """
749788 Get data from the Adafruit IO Random Data Stream Service
750789
0 commit comments