Skip to content

Change to allow passing in secrets in blinka pyportal #10

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 2 commits into from
Jan 16, 2021
Merged
Changes from 1 commit
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
30 changes: 17 additions & 13 deletions adafruit_portalbase/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,29 @@ class NetworkBase:
:param bool extract_values: If true, single-length fetched values are automatically extracted
from lists and tuples. Defaults to ``True``.
:param debug: Turn on debug print outs. Defaults to False.
:param list secrets_data: An optional list in place of the data contained in the secrets.py file

"""

# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
def __init__(
self,
wifi_module,
*,
extract_values=True,
debug=False,
self, wifi_module, *, extract_values=True, debug=False, secrets_data=None
):
self._wifi = wifi_module
self._debug = debug
self.json_transform = []
self._extract_values = extract_values

self._json_types = [
"application/json",
"application/javascript",
"application/geo+json",
]

if secrets_data is not None:
self._secrets = secrets_data
else:
self._secrets = secrets

# This may be removed. Using for testing
self.requests = None

Expand Down Expand Up @@ -175,15 +176,15 @@ def get_local_time(self, location=None):
self.connect()
api_url = None
try:
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
aio_username = self._secrets["aio_username"]
aio_key = self._secrets["aio_key"]
except KeyError:
raise KeyError(
"\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
) from KeyError

if location is None:
location = secrets.get("timezone", location)
location = self._secrets.get("timezone", location)
if location:
print("Getting time for timezone", location)
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
Expand Down Expand Up @@ -302,7 +303,10 @@ def connect(self):
while not self._wifi.is_connected:
# secrets dictionary must contain 'ssid' and 'password' at a minimum
print("Connecting to AP", secrets["ssid"])
if secrets["ssid"] == "CHANGE ME" or secrets["password"] == "CHANGE ME":
if (
self._secrets["ssid"] == "CHANGE ME"
or self._secrets["password"] == "CHANGE ME"
):
change_me = "\n" + "*" * 45
change_me += "\nPlease update the 'secrets.py' file on your\n"
change_me += "CIRCUITPY drive to include your local WiFi\n"
Expand All @@ -312,7 +316,7 @@ def connect(self):
raise OSError(change_me)
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
try:
self._wifi.connect(secrets["ssid"], secrets["password"])
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
self.requests = self._wifi.requests
except RuntimeError as error:
print("Could not connect to internet", error)
Expand All @@ -323,8 +327,8 @@ def _get_io_client(self):
self.connect()

try:
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
aio_username = self._secrets["aio_username"]
aio_key = self._secrets["aio_key"]
except KeyError:
raise KeyError(
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"
Expand Down