Skip to content

Commit 490c83a

Browse files
authored
Merge pull request #10 from makermelissa/main
Change to allow passing in secrets in blinka pyportal
2 parents 5c5b54d + 9a47a9b commit 490c83a

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

adafruit_portalbase/network.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"""WiFi settings are kept in secrets.py, please add them there!
4646
the secrets dictionary must contain 'ssid' and 'password' at a minimum"""
4747
)
48-
raise
4948

5049
__version__ = "0.0.0-auto.0"
5150
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
@@ -87,28 +86,29 @@ class NetworkBase:
8786
:param bool extract_values: If true, single-length fetched values are automatically extracted
8887
from lists and tuples. Defaults to ``True``.
8988
:param debug: Turn on debug print outs. Defaults to False.
89+
:param list secrets_data: An optional list in place of the data contained in the secrets.py file
9090
9191
"""
9292

9393
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
9494
def __init__(
95-
self,
96-
wifi_module,
97-
*,
98-
extract_values=True,
99-
debug=False,
95+
self, wifi_module, *, extract_values=True, debug=False, secrets_data=None
10096
):
10197
self._wifi = wifi_module
10298
self._debug = debug
10399
self.json_transform = []
104100
self._extract_values = extract_values
105-
106101
self._json_types = [
107102
"application/json",
108103
"application/javascript",
109104
"application/geo+json",
110105
]
111106

107+
if secrets_data is not None:
108+
self._secrets = secrets_data
109+
else:
110+
self._secrets = secrets
111+
112112
# This may be removed. Using for testing
113113
self.requests = None
114114

@@ -175,15 +175,15 @@ def get_local_time(self, location=None):
175175
self.connect()
176176
api_url = None
177177
try:
178-
aio_username = secrets["aio_username"]
179-
aio_key = secrets["aio_key"]
178+
aio_username = self._secrets["aio_username"]
179+
aio_key = self._secrets["aio_key"]
180180
except KeyError:
181181
raise KeyError(
182182
"\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
183183
) from KeyError
184184

185185
if location is None:
186-
location = secrets.get("timezone", location)
186+
location = self._secrets.get("timezone", location)
187187
if location:
188188
print("Getting time for timezone", location)
189189
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
@@ -301,8 +301,11 @@ def connect(self):
301301
self._wifi.neo_status(STATUS_CONNECTING)
302302
while not self._wifi.is_connected:
303303
# secrets dictionary must contain 'ssid' and 'password' at a minimum
304-
print("Connecting to AP", secrets["ssid"])
305-
if secrets["ssid"] == "CHANGE ME" or secrets["password"] == "CHANGE ME":
304+
print("Connecting to AP", self._secrets["ssid"])
305+
if (
306+
self._secrets["ssid"] == "CHANGE ME"
307+
or self._secrets["password"] == "CHANGE ME"
308+
):
306309
change_me = "\n" + "*" * 45
307310
change_me += "\nPlease update the 'secrets.py' file on your\n"
308311
change_me += "CIRCUITPY drive to include your local WiFi\n"
@@ -312,7 +315,7 @@ def connect(self):
312315
raise OSError(change_me)
313316
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
314317
try:
315-
self._wifi.connect(secrets["ssid"], secrets["password"])
318+
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
316319
self.requests = self._wifi.requests
317320
except RuntimeError as error:
318321
print("Could not connect to internet", error)
@@ -323,8 +326,8 @@ def _get_io_client(self):
323326
self.connect()
324327

325328
try:
326-
aio_username = secrets["aio_username"]
327-
aio_key = secrets["aio_key"]
329+
aio_username = self._secrets["aio_username"]
330+
aio_key = self._secrets["aio_key"]
328331
except KeyError:
329332
raise KeyError(
330333
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"

0 commit comments

Comments
 (0)