Skip to content

Converted strings to f-strings, added Hub class parameter descriptions #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions adafruit_dash_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,23 @@ def last_val(self, value: str):


class Hub: # pylint: disable=too-many-instance-attributes
"""Object that lets you make an IOT dashboard"""
"""
Object that lets you make an IOT dashboard

:param displayio.Display display: The display for the dashboard.
:param IO_MQTT io_mqtt: MQTT communications object.
:param Tuple[DigitalInOut, ...] nav: The navigation pushbuttons.
"""

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

self.io = io # pylint: disable=invalid-name
self.io_mqtt = io_mqtt

self.up_btn, self.select, self.down, self.back, self.submit = nav

Expand All @@ -145,13 +150,13 @@ def __init__(

self.feeds = OrderedDict()

self.io.on_mqtt_connect = self.connected
self.io.on_mqtt_disconnect = self.disconnected
self.io.on_mqtt_subscribe = self.subscribe
self.io.on_message = self.message
self.io_mqtt.on_mqtt_connect = self.connected
self.io_mqtt.on_mqtt_disconnect = self.disconnected
self.io_mqtt.on_mqtt_subscribe = self.subscribe
self.io_mqtt.on_message = self.message

print("Connecting to Adafruit IO...")
io.connect()
io_mqtt.connect()

self.display.show(None)

Expand Down Expand Up @@ -206,7 +211,7 @@ def add_device(
if not default_text:
default_text = feed_key

self.io.subscribe(feed_key)
self.io_mqtt.subscribe(feed_key)
if len(self.splash) == 1:
self.splash.append(
Label(
Expand Down Expand Up @@ -246,9 +251,9 @@ def get(self):
"""Gets all the subscribed feeds"""
for feed in self.feeds.keys():
print(f"getting {feed}")
self.io.get(feed)
self.io_mqtt.get(feed)
time.sleep(0.1)
self.io.loop()
self.io_mqtt.loop()

# pylint: disable=unused-argument
@staticmethod
Expand All @@ -259,7 +264,7 @@ def connected(client: IO_MQTT):
@staticmethod
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))
print(f"Subscribed to {topic} with QOS level {granted_qos}")

@staticmethod
def disconnected(client: IO_MQTT):
Expand All @@ -268,7 +273,7 @@ def disconnected(client: IO_MQTT):

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))
print(f"Feed {feed_id} received new value: {message}")
feed_id = feed_id.split("/")[-1]
feed = self.feeds[feed_id]
feed.last_val = message
Expand All @@ -277,11 +282,11 @@ def message(self, client: IO_MQTT, feed_id: str, message: str):
def publish(self, feed: Feed, message: str):
"""Callback for publishing a message"""
print(f"Publishing {message} to {feed}")
self.io.publish(feed, message)
self.io_mqtt.publish(feed, message)

def loop(self):
"""Loops Adafruit IO and also checks to see if any buttons have been pressed"""
self.io.loop()
self.io_mqtt.loop()
if self.select.value:
feed = self.feeds[list(self.feeds.keys())[self.selected - 1]]
if feed.pub:
Expand Down
2 changes: 1 addition & 1 deletion examples/dash_display_advancedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def pub_lamp(lamp):
# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)

iot = Hub(display=display, io=io, nav=(up, select, down, back, submit))
iot = Hub(display=display, io_mqtt=io, nav=(up, select, down, back, submit))

iot.add_device(
feed_key="lamp",
Expand Down
2 changes: 1 addition & 1 deletion examples/dash_display_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def pub_lamp(lamp):
time.sleep(0.3)


iot = Hub(display=display, io=io, nav=(up, select, down, back, submit))
iot = Hub(display=display, io_mqtt=io, nav=(up, select, down, back, submit))

iot.add_device(
feed_key="lamp",
Expand Down