Skip to content

Commit

Permalink
Merge pull request #9 from julianaklulo/main
Browse files Browse the repository at this point in the history
Add type annotations to adafruit_dash_display
  • Loading branch information
FoamyGuy committed May 30, 2023
2 parents 9289f44 + 3d804a8 commit 3b5bcc0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
66 changes: 42 additions & 24 deletions adafruit_dash_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
https://github.com/adafruit/circuitpython/releases
"""
try:
from typing import Tuple, Callable, Optional, Any
from adafruit_io.adafruit_io import IO_MQTT
from digitalio import DigitalInOut
except ImportError:
pass

import time
from collections import OrderedDict
from adafruit_display_shapes.rect import Rect
from adafruit_display_text.label import Label
import displayio
import terminalio
from adafruit_display_shapes.rect import Rect
from adafruit_display_text.label import Label

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

def __init__(
self, key, default_text, formatted_text, callback, color, pub, index
self,
key: str,
default_text: str,
formatted_text: str,
callback: Optional[Callable],
color: Optional[int],
pub: Optional[Callable],
index: int,
): # pylint: disable=too-many-arguments
self._key = key
self.default_text = default_text
Expand All @@ -56,7 +69,7 @@ def key(self):
return self._key

@key.setter
def key(self, value):
def key(self, value: str):
"""Setter for feed key. Sets a new value for the feed key property _key"""
self._key = value

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

@text.setter
def text(self, value):
def text(self, value: str):
"""Setter for text ready to be formatted. Allows to change the feed text"""
self._text = value

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

@callback.setter
def callback(self, value):
def callback(self, value: Callable):
"""Setter for callback function. Changes the feed callback"""
self._callback = value

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

@color.setter
def color(self, value):
def color(self, value: int):
"""Setter for text color callback function"""
self._color = value

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

@pub.setter
def pub(self, value):
def pub(self, value: Callable):
"""Setter for publish function"""
self._pub = value

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

@last_val.setter
def last_val(self, value):
def last_val(self, value: str):
"""Setter for last received value"""
self._last_val = value

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

# pylint: disable=invalid-name
def __init__(self, display, io, nav):
def __init__(
self,
display: displayio.Display,
io: IO_MQTT,
nav: Tuple[DigitalInOut, ...],
):
self.display = display

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

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

def update_text(self, client, feed_id, message):
def update_text(self, client: IO_MQTT, feed_id: str, message: str):
"""Updates the text on the display"""
feed = self.feeds[feed_id]
feed.callback(client, feed_id, message)
self.splash[feed.index + 1].text = feed.callback(client, feed_id, str(message))
if feed.color:
self.splash[feed.index + 1].color = feed.color(message)

def base_pub(self, var):
def base_pub(self, var: Any):
"""Default function called when a feed is published to"""

def add_device(
self,
feed_key,
default_text=None,
formatted_text=None,
color_callback=None,
callback=None,
pub_method=None,
feed_key: str,
default_text: Optional[str] = None,
formatted_text: Optional[str] = None,
color_callback: Optional[int] = None,
callback: Optional[Callable] = None,
pub_method: Optional[Callable] = None,
): # pylint: disable=too-many-arguments
"""Adds a feed/device to the UI"""
if not callback:
Expand Down Expand Up @@ -234,29 +252,29 @@ def get(self):

# pylint: disable=unused-argument
@staticmethod
def connected(client):
def connected(client: IO_MQTT):
"""Callback for when the device is connected to Adafruit IO"""
print("Connected to Adafruit IO!")

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

@staticmethod
def disconnected(client):
def disconnected(client: IO_MQTT):
"""Callback for when the device disconnects from Adafruit IO"""
print("Disconnected from Adafruit IO!")

def message(self, client, feed_id, message):
def message(self, client: IO_MQTT, feed_id: str, message: str):
"""Callback for whenever a new message is received"""
print("Feed {0} received new value: {1}".format(feed_id, message))
feed_id = feed_id.split("/")[-1]
feed = self.feeds[feed_id]
feed.last_val = message
self.update_text(client, feed_id, str(message))

def publish(self, feed, message):
def publish(self, feed: Feed, message: str):
"""Callback for publishing a message"""
print(f"Publishing {message} to {feed}")
self.io.publish(feed, message)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
Adafruit-Blinka
adafruit-circuitpython-display-text
adafruit-circuitpython-display-shapes
adafruit-circuitpython-adafruitio

0 comments on commit 3b5bcc0

Please sign in to comment.