22
22
import json
23
23
import re
24
24
25
+ try :
26
+ from typing import List , Any , Callable , Optional
27
+ except ImportError :
28
+ pass
29
+
25
30
from adafruit_minimqtt .adafruit_minimqtt import MMQTTException
26
31
from adafruit_io .adafruit_io_errors import (
27
32
AdafruitIO_RequestError ,
35
40
CLIENT_HEADERS = {"User-Agent" : "AIO-CircuitPython/{0}" .format (__version__ )}
36
41
37
42
38
- def validate_feed_key (feed_key ):
43
+ def validate_feed_key (feed_key : str ):
39
44
"""Validates a provided feed key against Adafruit IO's system rules.
40
45
https://learn.adafruit.com/naming-things-in-adafruit-io/the-two-feed-identifiers
41
46
"""
@@ -143,7 +148,7 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
143
148
self .on_disconnect (self )
144
149
145
150
# pylint: disable=not-callable
146
- def _on_message_mqtt (self , client , topic , payload ):
151
+ def _on_message_mqtt (self , client , topic : str , payload : str ):
147
152
"""Runs when the client calls on_message. Parses and returns
148
153
incoming data from Adafruit IO feeds.
149
154
@@ -195,7 +200,7 @@ def _on_unsubscribe_mqtt(self, client, user_data, topic, pid):
195
200
if self .on_unsubscribe is not None :
196
201
self .on_unsubscribe (self , user_data , topic , pid )
197
202
198
- def add_feed_callback (self , feed_key , callback_method ):
203
+ def add_feed_callback (self , feed_key : str , callback_method : Callable ):
199
204
"""Attaches a callback_method to an Adafruit IO feed.
200
205
The callback_method function is called when a
201
206
new value is written to the feed.
@@ -211,7 +216,7 @@ def add_feed_callback(self, feed_key, callback_method):
211
216
"{0}/f/{1}" .format (self ._user , feed_key ), callback_method
212
217
)
213
218
214
- def remove_feed_callback (self , feed_key ):
219
+ def remove_feed_callback (self , feed_key : str ):
215
220
"""Removes a previously registered callback method
216
221
from executing whenever feed_key receives new data.
217
222
@@ -239,7 +244,12 @@ def loop(self, timeout=1):
239
244
self ._client .loop (timeout )
240
245
241
246
# 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
+ ):
243
253
"""Subscribes to your Adafruit IO feed or group.
244
254
Can also subscribe to someone else's feed.
245
255
@@ -277,7 +287,7 @@ def subscribe_to_errors(self):
277
287
"""
278
288
self ._client .subscribe ("%s/errors" % self ._user )
279
289
280
- def subscribe_to_randomizer (self , randomizer_id ):
290
+ def subscribe_to_randomizer (self , randomizer_id : int ):
281
291
"""Subscribes to a random data stream created by the Adafruit IO Words service.
282
292
283
293
:param int randomizer_id: Random word record you want data for.
@@ -286,7 +296,7 @@ def subscribe_to_randomizer(self, randomizer_id):
286
296
"{0}/integration/words/{1}" .format (self ._user , randomizer_id )
287
297
)
288
298
289
- def subscribe_to_weather (self , weather_record , forecast ):
299
+ def subscribe_to_weather (self , weather_record : int , forecast : str ):
290
300
"""Subscribes to a weather forecast using the Adafruit IO PLUS weather
291
301
service. This feature is only avaliable to Adafruit IO PLUS subscribers.
292
302
@@ -299,7 +309,7 @@ def subscribe_to_weather(self, weather_record, forecast):
299
309
)
300
310
)
301
311
302
- def subscribe_to_time (self , time_type ):
312
+ def subscribe_to_time (self , time_type : str ):
303
313
"""Adafruit IO provides some built-in MQTT topics for getting the current server time.
304
314
305
315
: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):
312
322
else :
313
323
self ._client .subscribe ("time/" + time_type )
314
324
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
+ ):
316
331
"""Unsubscribes from an Adafruit IO feed or group.
317
332
Can also subscribe to someone else's feed.
318
333
@@ -345,11 +360,13 @@ def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
345
360
raise AdafruitIO_MQTTError ("Must provide a feed_key or group_key." )
346
361
347
362
# 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
+ ):
349
366
"""Publishes multiple data points to multiple feeds or groups with a variable
350
367
timeout.
351
368
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.
353
370
:param int timeout: Delay between publishing data points to Adafruit IO, in seconds.
354
371
:param bool is_group: Set to True if you're publishing to a group.
355
372
@@ -373,8 +390,15 @@ def publish_multiple(self, feeds_and_data, timeout=3, is_group=False):
373
390
time .sleep (timeout )
374
391
375
392
# 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.
378
402
379
403
:param str feed_key: Adafruit IO Feed key.
380
404
: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
439
463
else :
440
464
self ._client .publish ("{0}/f/{1}" .format (self ._user , feed_key ), data )
441
465
442
- def get (self , feed_key ):
466
+ def get (self , feed_key : str ):
443
467
"""Calling this method will make Adafruit IO publish the most recent
444
468
value on feed_key.
445
469
https://io.adafruit.com/api/docs/mqtt.html#retained-values
@@ -484,7 +508,7 @@ def _create_headers(io_headers):
484
508
return headers
485
509
486
510
@staticmethod
487
- def _create_data (data , metadata ):
511
+ def _create_data (data , metadata : dict ):
488
512
"""Returns a data payload as expected by the Adafruit IO HTTP API
489
513
490
514
:param data: Payload value.
@@ -508,15 +532,15 @@ def _handle_error(response):
508
532
if response .status_code >= 400 :
509
533
raise AdafruitIO_RequestError (response )
510
534
511
- def _compose_path (self , path ):
535
+ def _compose_path (self , path : str ):
512
536
"""Composes a valid API request path.
513
537
514
538
:param str path: Adafruit IO API URL path.
515
539
"""
516
540
return "https://io.adafruit.com/api/v2/{0}/{1}" .format (self .username , path )
517
541
518
542
# HTTP Requests
519
- def _post (self , path , payload ):
543
+ def _post (self , path : str , payload : Any ):
520
544
"""
521
545
POST data to Adafruit IO
522
546
@@ -531,7 +555,7 @@ def _post(self, path, payload):
531
555
532
556
return json_data
533
557
534
- def _get (self , path ):
558
+ def _get (self , path : str ):
535
559
"""
536
560
GET data from Adafruit IO
537
561
@@ -544,7 +568,7 @@ def _get(self, path):
544
568
json_data = response .json ()
545
569
return json_data
546
570
547
- def _delete (self , path ):
571
+ def _delete (self , path : str ):
548
572
"""
549
573
DELETE data from Adafruit IO.
550
574
@@ -559,7 +583,13 @@ def _delete(self, path):
559
583
return json_data
560
584
561
585
# 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
+ ):
563
593
"""
564
594
Sends value data to a specified Adafruit IO feed.
565
595
@@ -580,7 +610,7 @@ def send_data(self, feed_key, data, metadata=None, precision=None):
580
610
payload = self ._create_data (data , metadata )
581
611
self ._post (path , payload )
582
612
583
- def send_batch_data (self , feed_key , data_list ):
613
+ def send_batch_data (self , feed_key : str , data_list : list ):
584
614
"""
585
615
Sends a batch array of data to a specified Adafruit IO feed
586
616
@@ -592,7 +622,7 @@ def send_batch_data(self, feed_key, data_list):
592
622
data_dict = type (data_list )((data ._asdict () for data in data_list ))
593
623
self ._post (path , {"data" : data_dict })
594
624
595
- def receive_all_data (self , feed_key ):
625
+ def receive_all_data (self , feed_key : str ):
596
626
"""
597
627
Get all data values from a specified Adafruit IO feed. Data is
598
628
returned in reverse order.
@@ -603,7 +633,7 @@ def receive_all_data(self, feed_key):
603
633
path = self ._compose_path ("feeds/{0}/data" .format (feed_key ))
604
634
return self ._get (path )
605
635
606
- def receive_data (self , feed_key ):
636
+ def receive_data (self , feed_key : str ):
607
637
"""
608
638
Return the most recent value for the specified feed.
609
639
@@ -613,7 +643,7 @@ def receive_data(self, feed_key):
613
643
path = self ._compose_path ("feeds/{0}/data/last" .format (feed_key ))
614
644
return self ._get (path )
615
645
616
- def delete_data (self , feed_key , data_id ):
646
+ def delete_data (self , feed_key : str , data_id : str ):
617
647
"""
618
648
Deletes an existing Data point from a feed.
619
649
@@ -625,7 +655,7 @@ def delete_data(self, feed_key, data_id):
625
655
return self ._delete (path )
626
656
627
657
# Groups
628
- def create_new_group (self , group_key , group_description ):
658
+ def create_new_group (self , group_key : str , group_description : str ):
629
659
"""
630
660
Creates a new Adafruit IO Group.
631
661
@@ -636,7 +666,7 @@ def create_new_group(self, group_key, group_description):
636
666
payload = {"name" : group_key , "description" : group_description }
637
667
return self ._post (path , payload )
638
668
639
- def delete_group (self , group_key ):
669
+ def delete_group (self , group_key : str ):
640
670
"""
641
671
Deletes an existing group.
642
672
@@ -645,7 +675,7 @@ def delete_group(self, group_key):
645
675
path = self ._compose_path ("groups/{0}" .format (group_key ))
646
676
return self ._delete (path )
647
677
648
- def get_group (self , group_key ):
678
+ def get_group (self , group_key : str ):
649
679
"""
650
680
Returns Group based on Group Key
651
681
@@ -654,7 +684,7 @@ def get_group(self, group_key):
654
684
path = self ._compose_path ("groups/{0}" .format (group_key ))
655
685
return self ._get (path )
656
686
657
- def create_feed_in_group (self , group_key , feed_name ):
687
+ def create_feed_in_group (self , group_key : str , feed_name : str ):
658
688
"""Creates a new feed in an existing group.
659
689
660
690
:param str group_key: Group name.
@@ -664,7 +694,7 @@ def create_feed_in_group(self, group_key, feed_name):
664
694
payload = {"feed" : {"name" : feed_name }}
665
695
return self ._post (path , payload )
666
696
667
- def add_feed_to_group (self , group_key , feed_key ):
697
+ def add_feed_to_group (self , group_key : str , feed_key : str ):
668
698
"""
669
699
Adds an existing feed to a group
670
700
@@ -677,7 +707,7 @@ def add_feed_to_group(self, group_key, feed_key):
677
707
return self ._post (path , payload )
678
708
679
709
# Feeds
680
- def get_feed (self , feed_key , detailed = False ):
710
+ def get_feed (self , feed_key : str , detailed : bool = False ):
681
711
"""
682
712
Returns an Adafruit IO feed based on the feed key
683
713
@@ -691,7 +721,12 @@ def get_feed(self, feed_key, detailed=False):
691
721
path = self ._compose_path ("feeds/{0}" .format (feed_key ))
692
722
return self ._get (path )
693
723
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
+ ):
695
730
"""
696
731
Creates a new Adafruit IO feed.
697
732
@@ -705,7 +740,11 @@ def create_new_feed(self, feed_key, feed_desc=None, feed_license=None):
705
740
return self ._post (path , payload )
706
741
707
742
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 ,
709
748
):
710
749
"""
711
750
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(
723
762
)
724
763
return self .get_feed (feed_key , detailed = detailed )
725
764
726
- def delete_feed (self , feed_key ):
765
+ def delete_feed (self , feed_key : str ):
727
766
"""
728
767
Deletes an existing feed.
729
768
@@ -734,7 +773,7 @@ def delete_feed(self, feed_key):
734
773
return self ._delete (path )
735
774
736
775
# Adafruit IO Connected Services
737
- def receive_weather (self , weather_id ):
776
+ def receive_weather (self , weather_id : int ):
738
777
"""
739
778
Get data from the Adafruit IO Weather Forecast Service
740
779
NOTE: This service is avaliable to Adafruit IO Plus subscribers only.
@@ -744,7 +783,7 @@ def receive_weather(self, weather_id):
744
783
path = self ._compose_path ("integrations/weather/{0}" .format (weather_id ))
745
784
return self ._get (path )
746
785
747
- def receive_random_data (self , generator_id ):
786
+ def receive_random_data (self , generator_id : int ):
748
787
"""
749
788
Get data from the Adafruit IO Random Data Stream Service
750
789
0 commit comments