Change to allow passing in secrets in blinkapyportal
This commit is contained in:
parent
5c5b54d8df
commit
d15a11fe4d
1 changed files with 17 additions and 13 deletions
|
|
@ -87,28 +87,29 @@ class NetworkBase:
|
||||||
:param bool extract_values: If true, single-length fetched values are automatically extracted
|
:param bool extract_values: If true, single-length fetched values are automatically extracted
|
||||||
from lists and tuples. Defaults to ``True``.
|
from lists and tuples. Defaults to ``True``.
|
||||||
:param debug: Turn on debug print outs. Defaults to False.
|
: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
|
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self, wifi_module, *, extract_values=True, debug=False, secrets_data=None
|
||||||
wifi_module,
|
|
||||||
*,
|
|
||||||
extract_values=True,
|
|
||||||
debug=False,
|
|
||||||
):
|
):
|
||||||
self._wifi = wifi_module
|
self._wifi = wifi_module
|
||||||
self._debug = debug
|
self._debug = debug
|
||||||
self.json_transform = []
|
self.json_transform = []
|
||||||
self._extract_values = extract_values
|
self._extract_values = extract_values
|
||||||
|
|
||||||
self._json_types = [
|
self._json_types = [
|
||||||
"application/json",
|
"application/json",
|
||||||
"application/javascript",
|
"application/javascript",
|
||||||
"application/geo+json",
|
"application/geo+json",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if secrets_data is not None:
|
||||||
|
self._secrets = secrets_data
|
||||||
|
else:
|
||||||
|
self._secrets = secrets
|
||||||
|
|
||||||
# This may be removed. Using for testing
|
# This may be removed. Using for testing
|
||||||
self.requests = None
|
self.requests = None
|
||||||
|
|
||||||
|
|
@ -175,15 +176,15 @@ class NetworkBase:
|
||||||
self.connect()
|
self.connect()
|
||||||
api_url = None
|
api_url = None
|
||||||
try:
|
try:
|
||||||
aio_username = secrets["aio_username"]
|
aio_username = self._secrets["aio_username"]
|
||||||
aio_key = secrets["aio_key"]
|
aio_key = self._secrets["aio_key"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise 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
|
"\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
|
) from KeyError
|
||||||
|
|
||||||
if location is None:
|
if location is None:
|
||||||
location = secrets.get("timezone", location)
|
location = self._secrets.get("timezone", location)
|
||||||
if location:
|
if location:
|
||||||
print("Getting time for timezone", location)
|
print("Getting time for timezone", location)
|
||||||
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
|
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
|
||||||
|
|
@ -302,7 +303,10 @@ class NetworkBase:
|
||||||
while not self._wifi.is_connected:
|
while not self._wifi.is_connected:
|
||||||
# secrets dictionary must contain 'ssid' and 'password' at a minimum
|
# secrets dictionary must contain 'ssid' and 'password' at a minimum
|
||||||
print("Connecting to AP", secrets["ssid"])
|
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 = "\n" + "*" * 45
|
||||||
change_me += "\nPlease update the 'secrets.py' file on your\n"
|
change_me += "\nPlease update the 'secrets.py' file on your\n"
|
||||||
change_me += "CIRCUITPY drive to include your local WiFi\n"
|
change_me += "CIRCUITPY drive to include your local WiFi\n"
|
||||||
|
|
@ -312,7 +316,7 @@ class NetworkBase:
|
||||||
raise OSError(change_me)
|
raise OSError(change_me)
|
||||||
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
|
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
|
||||||
try:
|
try:
|
||||||
self._wifi.connect(secrets["ssid"], secrets["password"])
|
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
|
||||||
self.requests = self._wifi.requests
|
self.requests = self._wifi.requests
|
||||||
except RuntimeError as error:
|
except RuntimeError as error:
|
||||||
print("Could not connect to internet", error)
|
print("Could not connect to internet", error)
|
||||||
|
|
@ -323,8 +327,8 @@ class NetworkBase:
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
aio_username = secrets["aio_username"]
|
aio_username = self._secrets["aio_username"]
|
||||||
aio_key = secrets["aio_key"]
|
aio_key = self._secrets["aio_key"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"
|
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue