Skip to content

Commit 970df94

Browse files
authored
Merge pull request #6 from makermelissa/master
Made some changes so URLs that depend on current time can be more accurate
2 parents a4b7c19 + 545853c commit 970df94

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

adafruit_matrixportal/matrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ def __init__(
6464
)
6565
self.display = framebufferio.FramebufferDisplay(matrix)
6666
except ValueError:
67-
raise RuntimeError("Failed to initialize RGB Matrix")
67+
raise RuntimeError("Failed to initialize RGB Matrix") from ValueError

adafruit_matrixportal/matrixportal.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ def __init__(
8888
debug=debug,
8989
)
9090

91-
self._url = url
91+
self._url = None
92+
self.url = url
9293
self._headers = headers
93-
if json_path:
94-
if isinstance(json_path[0], (list, tuple)):
95-
self._json_path = json_path
96-
else:
97-
self._json_path = (json_path,)
98-
else:
99-
self._json_path = None
94+
self._json_path = None
95+
self.json_path = json_path
10096

10197
self._regexp_path = regexp_path
10298

@@ -111,12 +107,6 @@ def __init__(
111107
self._default_bg = default_bg
112108
self.splash.append(self._bg_group)
113109

114-
if url and not self._network.uselocal:
115-
self._network.connect()
116-
117-
if self._debug:
118-
print("My IP address is", self._network.ip_address)
119-
120110
# set the default background
121111
self.set_background(self._default_bg)
122112
self.display.show(self.splash)
@@ -297,6 +287,10 @@ def set_text(self, val, index=0):
297287
self._text[index].y = self._text_position[index][1]
298288
self.splash.append(self._text[index])
299289

290+
def get_local_time(self, location=None):
291+
"""Accessor function for get_local_time()"""
292+
return self._network.get_local_time(location=location)
293+
300294
def _get_next_scrollable_text_index(self):
301295
index = self._scrolling_index
302296
while True:
@@ -396,3 +390,36 @@ def wrap_nicely(string, max_chars):
396390
# remove first space from first line:
397391
the_lines[0] = the_lines[0][1:]
398392
return the_lines
393+
394+
@property
395+
def url(self):
396+
"""
397+
Get or set the URL of your data source.
398+
"""
399+
return self._json_path
400+
401+
@url.setter
402+
def url(self, value):
403+
self._url = value
404+
if value and not self._network.uselocal:
405+
self._network.connect()
406+
if self._debug:
407+
print("My IP address is", self._network.ip_address)
408+
409+
@property
410+
def json_path(self):
411+
"""
412+
Get or set the list of json traversal to get data out of. Can be list
413+
of lists for multiple data points.
414+
"""
415+
return self._json_path
416+
417+
@json_path.setter
418+
def json_path(self, value):
419+
if value:
420+
if isinstance(value[0], (list, tuple)):
421+
self._json_path = value
422+
else:
423+
self._json_path = (value,)
424+
else:
425+
self._json_path = None

adafruit_matrixportal/network.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
3232
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
3333
import adafruit_requests as requests
34-
from adafruit_matrixportal.wifi import WiFi
35-
from adafruit_matrixportal.fakerequests import Fake_Requests
3634
import supervisor
3735
import rtc
36+
from adafruit_matrixportal.wifi import WiFi
37+
from adafruit_matrixportal.fakerequests import Fake_Requests
3838

3939
try:
4040
from secrets import secrets
@@ -166,7 +166,7 @@ def get_local_time(self, location=None):
166166
except KeyError:
167167
raise KeyError(
168168
"\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'" # pylint: disable=line-too-long
169-
)
169+
) from KeyError
170170

171171
location = secrets.get("timezone", location)
172172
if location:
@@ -196,7 +196,7 @@ def get_local_time(self, location=None):
196196
except KeyError:
197197
raise KeyError(
198198
"Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones" # pylint: disable=line-too-long
199-
)
199+
) from KeyError
200200
year, month, mday = [int(x) for x in the_date.split("-")]
201201
the_time = the_time.split(".")[0]
202202
hours, minutes, seconds = [int(x) for x in the_time.split(":")]
@@ -293,7 +293,7 @@ def push_to_io(self, feed_key, data):
293293
except KeyError:
294294
raise KeyError(
295295
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"
296-
)
296+
) from KeyError
297297

298298
io_client = IO_HTTP(aio_username, aio_key, self._wifi.manager(secrets))
299299

0 commit comments

Comments
 (0)