Skip to content

Commit 52f5a3a

Browse files
authored
Merge pull request #19 from adafruit/docs_and_types
Docs and types
2 parents c00908b + 88e588f commit 52f5a3a

File tree

1 file changed

+108
-37
lines changed

1 file changed

+108
-37
lines changed

adafruit_dash_display.py

Lines changed: 108 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@
4141

4242

4343
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+
"""
4555

4656
def __init__(
4757
self,
@@ -64,63 +74,78 @@ def __init__(
6474
self._last_val = None
6575

6676
@property
67-
def key(self):
77+
def key(self) -> str:
6878
"""Getter for feed key. Will give the value of the feed key"""
6979
return self._key
7080

7181
@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+
"""
7487
self._key = value
7588

7689
@property
77-
def text(self):
90+
def text(self) -> str:
7891
"""Getter for text ready to be formatted. Will give the feed text"""
7992
return self._text
8093

8194
@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+
"""
84100
self._text = value
85101

86102
@property
87-
def callback(self):
103+
def callback(self) -> Optional[Callable]:
88104
"""Getter for callback function. Returns the feed callback"""
89105
return self._callback
90106

91107
@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+
"""
94113
self._callback = value
95114

96115
@property
97-
def color(self):
116+
def color(self) -> int:
98117
"""Getter for text color callback function. Will return the color for the feed"""
99118
return self._color
100119

101120
@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+
"""
104126
self._color = value
105127

106128
@property
107-
def pub(self):
129+
def pub(self) -> Optional[Callable]:
108130
"""Getter for publish function, called when a new value is published by this library."""
109131
return self._pub
110132

111133
@pub.setter
112-
def pub(self, value: Callable):
134+
def pub(self, value: Callable) -> None:
113135
"""Setter for publish function"""
114136
self._pub = value
115137

116138
@property
117-
def last_val(self):
139+
def last_val(self) -> Optional[str]:
118140
"""Getter for the last value received"""
119141
return self._last_val
120142

121143
@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+
"""
124149
self._last_val = value
125150

126151

@@ -168,10 +193,20 @@ def __init__(
168193
self.display.root_group = self.splash
169194

170195
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:
173202
"""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+
"""
175210
feed_id = feed_id.split("/")[-1]
176211
feed = self.feeds[feed_id]
177212
try:
@@ -180,15 +215,20 @@ def simple_text_callback(
180215
text = feed.text.format(float(message))
181216
return text
182217

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+
"""
185225
feed = self.feeds[feed_id]
186226
feed.callback(client, feed_id, message)
187227
self.splash[feed.index + 1].text = feed.callback(client, feed_id, str(message))
188228
if feed.color:
189229
self.splash[feed.index + 1].color = feed.color(message)
190230

191-
def base_pub(self, var: Any):
231+
def base_pub(self, var: Any) -> None:
192232
"""Default function called when a feed is published to"""
193233

194234
def add_device(
@@ -200,7 +240,17 @@ def add_device(
200240
callback: Optional[Callable] = None,
201241
pub_method: Optional[Callable] = None,
202242
): # 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+
"""
204254
if not callback:
205255
callback = self.simple_text_callback
206256
if not pub_method:
@@ -247,7 +297,7 @@ def add_device(
247297
index=len(self.feeds),
248298
)
249299

250-
def get(self):
300+
def get(self) -> None:
251301
"""Gets all the subscribed feeds"""
252302
for feed in self.feeds.keys():
253303
print(f"getting {feed}")
@@ -257,34 +307,55 @@ def get(self):
257307

258308
# pylint: disable=unused-argument
259309
@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+
"""
262315
print("Connected to Adafruit IO!")
263316

264317
@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+
"""
267326
print(f"Subscribed to {topic} with QOS level {granted_qos}")
268327

269328
@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+
"""
272334
print("Disconnected from Adafruit IO!")
273335

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+
"""
276343
print(f"Feed {feed_id} received new value: {message}")
277344
feed_id = feed_id.split("/")[-1]
278345
feed = self.feeds[feed_id]
279346
feed.last_val = message
280347
self.update_text(client, feed_id, str(message))
281348

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+
"""
284355
print(f"Publishing {message} to {feed}")
285356
self.io_mqtt.publish(feed, message)
286357

287-
def loop(self):
358+
def loop(self) -> None:
288359
"""Loops Adafruit IO and also checks to see if any buttons have been pressed"""
289360
self.io_mqtt.loop()
290361
if self.select.value:

0 commit comments

Comments
 (0)