Skip to content

Commit 3b5bcc0

Browse files
authored
Merge pull request #9 from julianaklulo/main
Add type annotations to adafruit_dash_display
2 parents 9289f44 + 3d804a8 commit 3b5bcc0

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

adafruit_dash_display.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
https://github.com/adafruit/circuitpython/releases
2323
2424
"""
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
2531

2632
import time
2733
from collections import OrderedDict
28-
from adafruit_display_shapes.rect import Rect
29-
from adafruit_display_text.label import Label
3034
import displayio
3135
import terminalio
36+
from adafruit_display_shapes.rect import Rect
37+
from adafruit_display_text.label import Label
3238

3339
__version__ = "0.0.0+auto.0"
3440
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Dash_Display.git"
@@ -38,7 +44,14 @@ class Feed:
3844
"""Feed object to make getting and setting different feed properties easier"""
3945

4046
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,
4255
): # pylint: disable=too-many-arguments
4356
self._key = key
4457
self.default_text = default_text
@@ -56,7 +69,7 @@ def key(self):
5669
return self._key
5770

5871
@key.setter
59-
def key(self, value):
72+
def key(self, value: str):
6073
"""Setter for feed key. Sets a new value for the feed key property _key"""
6174
self._key = value
6275

@@ -66,7 +79,7 @@ def text(self):
6679
return self._text
6780

6881
@text.setter
69-
def text(self, value):
82+
def text(self, value: str):
7083
"""Setter for text ready to be formatted. Allows to change the feed text"""
7184
self._text = value
7285

@@ -76,7 +89,7 @@ def callback(self):
7689
return self._callback
7790

7891
@callback.setter
79-
def callback(self, value):
92+
def callback(self, value: Callable):
8093
"""Setter for callback function. Changes the feed callback"""
8194
self._callback = value
8295

@@ -86,7 +99,7 @@ def color(self):
8699
return self._color
87100

88101
@color.setter
89-
def color(self, value):
102+
def color(self, value: int):
90103
"""Setter for text color callback function"""
91104
self._color = value
92105

@@ -96,7 +109,7 @@ def pub(self):
96109
return self._pub
97110

98111
@pub.setter
99-
def pub(self, value):
112+
def pub(self, value: Callable):
100113
"""Setter for publish function"""
101114
self._pub = value
102115

@@ -106,7 +119,7 @@ def last_val(self):
106119
return self._last_val
107120

108121
@last_val.setter
109-
def last_val(self, value):
122+
def last_val(self, value: str):
110123
"""Setter for last received value"""
111124
self._last_val = value
112125

@@ -115,7 +128,12 @@ class Hub: # pylint: disable=too-many-instance-attributes
115128
"""Object that lets you make an IOT dashboard"""
116129

117130
# 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+
):
119137
self.display = display
120138

121139
self.io = io # pylint: disable=invalid-name
@@ -145,7 +163,7 @@ def __init__(self, display, io, nav):
145163
self.display.show(self.splash)
146164

147165
def simple_text_callback(
148-
self, client, feed_id, message
166+
self, client: IO_MQTT, feed_id: str, message: str
149167
): # pylint: disable=unused-argument
150168
"""Default callback function that uses the text in the Feed object and the color callback
151169
to set the text"""
@@ -157,25 +175,25 @@ def simple_text_callback(
157175
text = feed.text.format(float(message))
158176
return text
159177

160-
def update_text(self, client, feed_id, message):
178+
def update_text(self, client: IO_MQTT, feed_id: str, message: str):
161179
"""Updates the text on the display"""
162180
feed = self.feeds[feed_id]
163181
feed.callback(client, feed_id, message)
164182
self.splash[feed.index + 1].text = feed.callback(client, feed_id, str(message))
165183
if feed.color:
166184
self.splash[feed.index + 1].color = feed.color(message)
167185

168-
def base_pub(self, var):
186+
def base_pub(self, var: Any):
169187
"""Default function called when a feed is published to"""
170188

171189
def add_device(
172190
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,
179197
): # pylint: disable=too-many-arguments
180198
"""Adds a feed/device to the UI"""
181199
if not callback:
@@ -234,29 +252,29 @@ def get(self):
234252

235253
# pylint: disable=unused-argument
236254
@staticmethod
237-
def connected(client):
255+
def connected(client: IO_MQTT):
238256
"""Callback for when the device is connected to Adafruit IO"""
239257
print("Connected to Adafruit IO!")
240258

241259
@staticmethod
242-
def subscribe(client, userdata, topic, granted_qos):
260+
def subscribe(client: IO_MQTT, userdata: Any, topic: str, granted_qos: str):
243261
"""Callback for when a new feed is subscribed to"""
244262
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
245263

246264
@staticmethod
247-
def disconnected(client):
265+
def disconnected(client: IO_MQTT):
248266
"""Callback for when the device disconnects from Adafruit IO"""
249267
print("Disconnected from Adafruit IO!")
250268

251-
def message(self, client, feed_id, message):
269+
def message(self, client: IO_MQTT, feed_id: str, message: str):
252270
"""Callback for whenever a new message is received"""
253271
print("Feed {0} received new value: {1}".format(feed_id, message))
254272
feed_id = feed_id.split("/")[-1]
255273
feed = self.feeds[feed_id]
256274
feed.last_val = message
257275
self.update_text(client, feed_id, str(message))
258276

259-
def publish(self, feed, message):
277+
def publish(self, feed: Feed, message: str):
260278
"""Callback for publishing a message"""
261279
print(f"Publishing {message} to {feed}")
262280
self.io.publish(feed, message)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-display-text
77
adafruit-circuitpython-display-shapes
8+
adafruit-circuitpython-adafruitio

0 commit comments

Comments
 (0)