Skip to content

Commit d15a11f

Browse files
committed
Change to allow passing in secrets in blinkapyportal
1 parent 5c5b54d commit d15a11f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

adafruit_portalbase/network.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,29 @@ class NetworkBase:
8787
:param bool extract_values: If true, single-length fetched values are automatically extracted
8888
from lists and tuples. Defaults to ``True``.
8989
:param debug: Turn on debug print outs. Defaults to False.
90+
:param list secrets_data: An optional list in place of the data contained in the secrets.py file
9091
9192
"""
9293

9394
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
9495
def __init__(
95-
self,
96-
wifi_module,
97-
*,
98-
extract_values=True,
99-
debug=False,
96+
self, wifi_module, *, extract_values=True, debug=False, secrets_data=None
10097
):
10198
self._wifi = wifi_module
10299
self._debug = debug
103100
self.json_transform = []
104101
self._extract_values = extract_values
105-
106102
self._json_types = [
107103
"application/json",
108104
"application/javascript",
109105
"application/geo+json",
110106
]
111107

108+
if secrets_data is not None:
109+
self._secrets = secrets_data
110+
else:
111+
self._secrets = secrets
112+
112113
# This may be removed. Using for testing
113114
self.requests = None
114115

@@ -175,15 +176,15 @@ def get_local_time(self, location=None):
175176
self.connect()
176177
api_url = None
177178
try:
178-
aio_username = secrets["aio_username"]
179-
aio_key = secrets["aio_key"]
179+
aio_username = self._secrets["aio_username"]
180+
aio_key = self._secrets["aio_key"]
180181
except KeyError:
181182
raise KeyError(
182183
"\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
183184
) from KeyError
184185

185186
if location is None:
186-
location = secrets.get("timezone", location)
187+
location = self._secrets.get("timezone", location)
187188
if location:
188189
print("Getting time for timezone", location)
189190
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
@@ -302,7 +303,10 @@ def connect(self):
302303
while not self._wifi.is_connected:
303304
# secrets dictionary must contain 'ssid' and 'password' at a minimum
304305
print("Connecting to AP", secrets["ssid"])
305-
if secrets["ssid"] == "CHANGE ME" or secrets["password"] == "CHANGE ME":
306+
if (
307+
self._secrets["ssid"] == "CHANGE ME"
308+
or self._secrets["password"] == "CHANGE ME"
309+
):
306310
change_me = "\n" + "*" * 45
307311
change_me += "\nPlease update the 'secrets.py' file on your\n"
308312
change_me += "CIRCUITPY drive to include your local WiFi\n"
@@ -312,7 +316,7 @@ def connect(self):
312316
raise OSError(change_me)
313317
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
314318
try:
315-
self._wifi.connect(secrets["ssid"], secrets["password"])
319+
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
316320
self.requests = self._wifi.requests
317321
except RuntimeError as error:
318322
print("Could not connect to internet", error)
@@ -323,8 +327,8 @@ def _get_io_client(self):
323327
self.connect()
324328

325329
try:
326-
aio_username = secrets["aio_username"]
327-
aio_key = secrets["aio_key"]
330+
aio_username = self._secrets["aio_username"]
331+
aio_key = self._secrets["aio_key"]
328332
except KeyError:
329333
raise KeyError(
330334
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"

0 commit comments

Comments
 (0)