41
41
42
42
43
43
class Feed :
44
- """Feed object to make getting and setting different feed properties easier"""
44
+ """Feed object to make getting and setting different feed properties easier
45
+
46
+ :param str key: The Adafruit IO key.
47
+ :param str default_text: Text to display before data has been pulled from Adafruit IO.
48
+ :param str formatted_text: String with formatting placeholders within it ready to have
49
+ data formatted into it.
50
+ :param Callable callback: A function to call when the feed is fetched.
51
+ :param int color: Hex color code for the feed text.
52
+ :param Callable pub: a function to call when data is published to the feed.
53
+
54
+ """
45
55
46
56
def __init__ (
47
57
self ,
@@ -64,63 +74,78 @@ def __init__(
64
74
self ._last_val = None
65
75
66
76
@property
67
- def key (self ):
77
+ def key (self ) -> str :
68
78
"""Getter for feed key. Will give the value of the feed key"""
69
79
return self ._key
70
80
71
81
@key .setter
72
- def key (self , value : str ):
73
- """Setter for feed key. Sets a new value for the feed key property _key"""
82
+ def key (self , value : str ) -> None :
83
+ """Setter for feed key. Sets a new value for the feed key property _key
84
+
85
+ :param str value: The new value of the feed key.
86
+ """
74
87
self ._key = value
75
88
76
89
@property
77
- def text (self ):
90
+ def text (self ) -> str :
78
91
"""Getter for text ready to be formatted. Will give the feed text"""
79
92
return self ._text
80
93
81
94
@text .setter
82
- def text (self , value : str ):
83
- """Setter for text ready to be formatted. Allows to change the feed text"""
95
+ def text (self , value : str ) -> None :
96
+ """Setter for text ready to be formatted. Allows to change the feed text
97
+
98
+ :param str value: The new value of the feed text.
99
+ """
84
100
self ._text = value
85
101
86
102
@property
87
- def callback (self ):
103
+ def callback (self ) -> Optional [ Callable ] :
88
104
"""Getter for callback function. Returns the feed callback"""
89
105
return self ._callback
90
106
91
107
@callback .setter
92
- def callback (self , value : Callable ):
93
- """Setter for callback function. Changes the feed callback"""
108
+ def callback (self , value : Callable ) -> None :
109
+ """Setter for callback function. Changes the feed callback
110
+
111
+ :param Callable value: A function to call when the feed is fetched.
112
+ """
94
113
self ._callback = value
95
114
96
115
@property
97
- def color (self ):
116
+ def color (self ) -> int :
98
117
"""Getter for text color callback function. Will return the color for the feed"""
99
118
return self ._color
100
119
101
120
@color .setter
102
- def color (self , value : int ):
103
- """Setter for text color callback function"""
121
+ def color (self , value : int ) -> None :
122
+ """Setter for text color callback function
123
+
124
+ :param int value: The new value of the feed color.
125
+ """
104
126
self ._color = value
105
127
106
128
@property
107
- def pub (self ):
129
+ def pub (self ) -> Optional [ Callable ] :
108
130
"""Getter for publish function, called when a new value is published by this library."""
109
131
return self ._pub
110
132
111
133
@pub .setter
112
- def pub (self , value : Callable ):
134
+ def pub (self , value : Callable ) -> None :
113
135
"""Setter for publish function"""
114
136
self ._pub = value
115
137
116
138
@property
117
- def last_val (self ):
139
+ def last_val (self ) -> Optional [ str ] :
118
140
"""Getter for the last value received"""
119
141
return self ._last_val
120
142
121
143
@last_val .setter
122
- def last_val (self , value : str ):
123
- """Setter for last received value"""
144
+ def last_val (self , value : str ) -> None :
145
+ """Setter for last received value
146
+
147
+ :param str value: The newly received value.
148
+ """
124
149
self ._last_val = value
125
150
126
151
@@ -168,10 +193,20 @@ def __init__(
168
193
self .display .root_group = self .splash
169
194
170
195
def simple_text_callback (
171
- self , client : IO_MQTT , feed_id : str , message : str
172
- ): # pylint: disable=unused-argument
196
+ # pylint: disable=unused-argument
197
+ self ,
198
+ client : IO_MQTT ,
199
+ feed_id : str ,
200
+ message : str ,
201
+ ) -> str :
173
202
"""Default callback function that uses the text in the Feed object and the color callback
174
- to set the text"""
203
+ to set the text
204
+
205
+ :param IO_MQTT client: The MQTT client to use.
206
+ :param str feed_id: The Adafruit IO feed ID.
207
+ :param str message: The text to display.
208
+ :return: A string with data formatted into it.
209
+ """
175
210
feed_id = feed_id .split ("/" )[- 1 ]
176
211
feed = self .feeds [feed_id ]
177
212
try :
@@ -180,15 +215,20 @@ def simple_text_callback(
180
215
text = feed .text .format (float (message ))
181
216
return text
182
217
183
- def update_text (self , client : IO_MQTT , feed_id : str , message : str ):
184
- """Updates the text on the display"""
218
+ def update_text (self , client : IO_MQTT , feed_id : str , message : str ) -> None :
219
+ """Updates the text on the display
220
+
221
+ :param IO_MQTT client: The MQTT client to use.
222
+ :param str feed_id: The Adafruit IO feed ID.
223
+ :param str message: The text to display.
224
+ """
185
225
feed = self .feeds [feed_id ]
186
226
feed .callback (client , feed_id , message )
187
227
self .splash [feed .index + 1 ].text = feed .callback (client , feed_id , str (message ))
188
228
if feed .color :
189
229
self .splash [feed .index + 1 ].color = feed .color (message )
190
230
191
- def base_pub (self , var : Any ):
231
+ def base_pub (self , var : Any ) -> None :
192
232
"""Default function called when a feed is published to"""
193
233
194
234
def add_device (
@@ -200,7 +240,17 @@ def add_device(
200
240
callback : Optional [Callable ] = None ,
201
241
pub_method : Optional [Callable ] = None ,
202
242
): # pylint: disable=too-many-arguments
203
- """Adds a feed/device to the UI"""
243
+ """Adds a feed/device to the UI
244
+
245
+ :param feed_key: The Adafruit IO feed key.
246
+ :param str default_text: The default text for the device.
247
+ :param str formatted_text: The formatted text for the device.
248
+ :param int color_callback: The color to use for the device
249
+ :param Callable callback: The callback function to be called
250
+ when data is fetched.
251
+ :param Callable pub_method: The pub_method to be called
252
+ when data is published.
253
+ """
204
254
if not callback :
205
255
callback = self .simple_text_callback
206
256
if not pub_method :
@@ -247,7 +297,7 @@ def add_device(
247
297
index = len (self .feeds ),
248
298
)
249
299
250
- def get (self ):
300
+ def get (self ) -> None :
251
301
"""Gets all the subscribed feeds"""
252
302
for feed in self .feeds .keys ():
253
303
print (f"getting { feed } " )
@@ -257,34 +307,55 @@ def get(self):
257
307
258
308
# pylint: disable=unused-argument
259
309
@staticmethod
260
- def connected (client : IO_MQTT ):
261
- """Callback for when the device is connected to Adafruit IO"""
310
+ def connected (client : IO_MQTT ) -> None :
311
+ """Callback for when the device is connected to Adafruit IO
312
+
313
+ :param IO_MQTT client: The MQTT client to use.
314
+ """
262
315
print ("Connected to Adafruit IO!" )
263
316
264
317
@staticmethod
265
- def subscribe (client : IO_MQTT , userdata : Any , topic : str , granted_qos : str ):
266
- """Callback for when a new feed is subscribed to"""
318
+ def subscribe (client : IO_MQTT , userdata : Any , topic : str , granted_qos : str ) -> None :
319
+ """Callback for when a new feed is subscribed to
320
+
321
+ :param IO_MQTT client: The MQTT client to use.
322
+ :param str userdata: The userdata to subscribe to.
323
+ :param str topic: The topic to subscribe to.
324
+ :param str granted_qos: The QoS level.
325
+ """
267
326
print (f"Subscribed to { topic } with QOS level { granted_qos } " )
268
327
269
328
@staticmethod
270
- def disconnected (client : IO_MQTT ):
271
- """Callback for when the device disconnects from Adafruit IO"""
329
+ def disconnected (client : IO_MQTT ) -> None :
330
+ """Callback for when the device disconnects from Adafruit IO
331
+
332
+ :param IO_MQTT client: The MQTT client to use.
333
+ """
272
334
print ("Disconnected from Adafruit IO!" )
273
335
274
- def message (self , client : IO_MQTT , feed_id : str , message : str ):
275
- """Callback for whenever a new message is received"""
336
+ def message (self , client : IO_MQTT , feed_id : str , message : str ) -> None :
337
+ """Callback for whenever a new message is received
338
+
339
+ :param IO_MQTT client: The MQTT client to use.
340
+ :param str feed_id: The Adafruit IO feed ID.
341
+ :param str message: The message received.
342
+ """
276
343
print (f"Feed { feed_id } received new value: { message } " )
277
344
feed_id = feed_id .split ("/" )[- 1 ]
278
345
feed = self .feeds [feed_id ]
279
346
feed .last_val = message
280
347
self .update_text (client , feed_id , str (message ))
281
348
282
- def publish (self , feed : Feed , message : str ):
283
- """Callback for publishing a message"""
349
+ def publish (self , feed : Feed , message : str ) -> None :
350
+ """Callback for publishing a message
351
+
352
+ :param Feed feed: The feed to publish to.
353
+ :param str message: The message to publish.
354
+ """
284
355
print (f"Publishing { message } to { feed } " )
285
356
self .io_mqtt .publish (feed , message )
286
357
287
- def loop (self ):
358
+ def loop (self ) -> None :
288
359
"""Loops Adafruit IO and also checks to see if any buttons have been pressed"""
289
360
self .io_mqtt .loop ()
290
361
if self .select .value :
0 commit comments