22
22
https://github.com/adafruit/circuitpython/releases
23
23
24
24
"""
25
+ try :
26
+ from typing import Tuple , Callable , Optional , Any
27
+ from adafruit_io .adafruit_io import IO_MQTT
28
+ from digitalio import DigitalInOut
29
+ except ImportError :
30
+ pass
25
31
26
32
import time
27
33
from collections import OrderedDict
28
- from adafruit_display_shapes .rect import Rect
29
- from adafruit_display_text .label import Label
30
34
import displayio
31
35
import terminalio
36
+ from adafruit_display_shapes .rect import Rect
37
+ from adafruit_display_text .label import Label
32
38
33
39
__version__ = "0.0.0+auto.0"
34
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Dash_Display.git"
@@ -38,7 +44,14 @@ class Feed:
38
44
"""Feed object to make getting and setting different feed properties easier"""
39
45
40
46
def __init__ (
41
- self , key , default_text , formatted_text , callback , color , pub , index
47
+ self ,
48
+ key : str ,
49
+ default_text : str ,
50
+ formatted_text : str ,
51
+ callback : Optional [Callable ],
52
+ color : Optional [int ],
53
+ pub : Optional [Callable ],
54
+ index : int ,
42
55
): # pylint: disable=too-many-arguments
43
56
self ._key = key
44
57
self .default_text = default_text
@@ -56,7 +69,7 @@ def key(self):
56
69
return self ._key
57
70
58
71
@key .setter
59
- def key (self , value ):
72
+ def key (self , value : str ):
60
73
"""Setter for feed key. Sets a new value for the feed key property _key"""
61
74
self ._key = value
62
75
@@ -66,7 +79,7 @@ def text(self):
66
79
return self ._text
67
80
68
81
@text .setter
69
- def text (self , value ):
82
+ def text (self , value : str ):
70
83
"""Setter for text ready to be formatted. Allows to change the feed text"""
71
84
self ._text = value
72
85
@@ -76,7 +89,7 @@ def callback(self):
76
89
return self ._callback
77
90
78
91
@callback .setter
79
- def callback (self , value ):
92
+ def callback (self , value : Callable ):
80
93
"""Setter for callback function. Changes the feed callback"""
81
94
self ._callback = value
82
95
@@ -86,7 +99,7 @@ def color(self):
86
99
return self ._color
87
100
88
101
@color .setter
89
- def color (self , value ):
102
+ def color (self , value : int ):
90
103
"""Setter for text color callback function"""
91
104
self ._color = value
92
105
@@ -96,7 +109,7 @@ def pub(self):
96
109
return self ._pub
97
110
98
111
@pub .setter
99
- def pub (self , value ):
112
+ def pub (self , value : Callable ):
100
113
"""Setter for publish function"""
101
114
self ._pub = value
102
115
@@ -106,7 +119,7 @@ def last_val(self):
106
119
return self ._last_val
107
120
108
121
@last_val .setter
109
- def last_val (self , value ):
122
+ def last_val (self , value : str ):
110
123
"""Setter for last received value"""
111
124
self ._last_val = value
112
125
@@ -115,7 +128,12 @@ class Hub: # pylint: disable=too-many-instance-attributes
115
128
"""Object that lets you make an IOT dashboard"""
116
129
117
130
# pylint: disable=invalid-name
118
- def __init__ (self , display , io , nav ):
131
+ def __init__ (
132
+ self ,
133
+ display : displayio .Display ,
134
+ io : IO_MQTT ,
135
+ nav : Tuple [DigitalInOut , ...],
136
+ ):
119
137
self .display = display
120
138
121
139
self .io = io # pylint: disable=invalid-name
@@ -145,7 +163,7 @@ def __init__(self, display, io, nav):
145
163
self .display .show (self .splash )
146
164
147
165
def simple_text_callback (
148
- self , client , feed_id , message
166
+ self , client : IO_MQTT , feed_id : str , message : str
149
167
): # pylint: disable=unused-argument
150
168
"""Default callback function that uses the text in the Feed object and the color callback
151
169
to set the text"""
@@ -157,25 +175,25 @@ def simple_text_callback(
157
175
text = feed .text .format (float (message ))
158
176
return text
159
177
160
- def update_text (self , client , feed_id , message ):
178
+ def update_text (self , client : IO_MQTT , feed_id : str , message : str ):
161
179
"""Updates the text on the display"""
162
180
feed = self .feeds [feed_id ]
163
181
feed .callback (client , feed_id , message )
164
182
self .splash [feed .index + 1 ].text = feed .callback (client , feed_id , str (message ))
165
183
if feed .color :
166
184
self .splash [feed .index + 1 ].color = feed .color (message )
167
185
168
- def base_pub (self , var ):
186
+ def base_pub (self , var : Any ):
169
187
"""Default function called when a feed is published to"""
170
188
171
189
def add_device (
172
190
self ,
173
- feed_key ,
174
- default_text = None ,
175
- formatted_text = None ,
176
- color_callback = None ,
177
- callback = None ,
178
- pub_method = None ,
191
+ feed_key : str ,
192
+ default_text : Optional [ str ] = None ,
193
+ formatted_text : Optional [ str ] = None ,
194
+ color_callback : Optional [ int ] = None ,
195
+ callback : Optional [ Callable ] = None ,
196
+ pub_method : Optional [ Callable ] = None ,
179
197
): # pylint: disable=too-many-arguments
180
198
"""Adds a feed/device to the UI"""
181
199
if not callback :
@@ -234,29 +252,29 @@ def get(self):
234
252
235
253
# pylint: disable=unused-argument
236
254
@staticmethod
237
- def connected (client ):
255
+ def connected (client : IO_MQTT ):
238
256
"""Callback for when the device is connected to Adafruit IO"""
239
257
print ("Connected to Adafruit IO!" )
240
258
241
259
@staticmethod
242
- def subscribe (client , userdata , topic , granted_qos ):
260
+ def subscribe (client : IO_MQTT , userdata : Any , topic : str , granted_qos : str ):
243
261
"""Callback for when a new feed is subscribed to"""
244
262
print ("Subscribed to {0} with QOS level {1}" .format (topic , granted_qos ))
245
263
246
264
@staticmethod
247
- def disconnected (client ):
265
+ def disconnected (client : IO_MQTT ):
248
266
"""Callback for when the device disconnects from Adafruit IO"""
249
267
print ("Disconnected from Adafruit IO!" )
250
268
251
- def message (self , client , feed_id , message ):
269
+ def message (self , client : IO_MQTT , feed_id : str , message : str ):
252
270
"""Callback for whenever a new message is received"""
253
271
print ("Feed {0} received new value: {1}" .format (feed_id , message ))
254
272
feed_id = feed_id .split ("/" )[- 1 ]
255
273
feed = self .feeds [feed_id ]
256
274
feed .last_val = message
257
275
self .update_text (client , feed_id , str (message ))
258
276
259
- def publish (self , feed , message ):
277
+ def publish (self , feed : Feed , message : str ):
260
278
"""Callback for publishing a message"""
261
279
print (f"Publishing { message } to { feed } " )
262
280
self .io .publish (feed , message )
0 commit comments