33
33
except ImportError :
34
34
rtc = None
35
35
36
- try :
37
- from secrets import secrets
38
- except ImportError :
39
- print (
40
- """WiFi settings are kept in secrets.py, please add them there!
41
- the secrets dictionary must contain 'ssid' and 'password' at a minimum
42
- in order to use network related features"""
43
- )
44
-
45
36
__version__ = "0.0.0+auto.0"
46
37
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
47
38
69
60
CONTENT_JSON = const (2 )
70
61
CONTENT_IMAGE = const (3 )
71
62
63
+ OLD_SETTINGS = {
64
+ "CIRCUITPY_WIFI_SSID" : "ssid" ,
65
+ "CIRCUITPY_WIFI_PASSWORD" : "password" ,
66
+ "AIO_USERNAME" : "aio_username" ,
67
+ "AIO_KEY" : "aio_key" ,
68
+ }
69
+
72
70
73
71
class HttpError (Exception ):
74
72
"""HTTP Specific Error"""
@@ -106,11 +104,13 @@ def __init__(
106
104
"application/geo+json" ,
107
105
]
108
106
107
+ self ._settings = {}
109
108
if secrets_data is not None :
110
- self ._secrets = secrets_data
111
- else :
112
- self ._secrets = secrets
113
- self ._secrets_network = None
109
+ for key , value in secrets_data .items ():
110
+ if key in OLD_SETTINGS :
111
+ key = OLD_SETTINGS .get (key )
112
+ self ._settings [key ] = value
113
+ self ._wifi_credentials = None
114
114
115
115
self .requests = None
116
116
@@ -124,6 +124,35 @@ def __init__(
124
124
125
125
gc .collect ()
126
126
127
+ def _get_setting (self , setting_name , show_error = True ):
128
+ if setting_name in self ._settings :
129
+ return self ._settings [setting_name ]
130
+
131
+ old_setting_name = setting_name
132
+ if setting_name in OLD_SETTINGS :
133
+ old_setting_name = OLD_SETTINGS .get (setting_name )
134
+ if os .getenv (setting_name ) is not None :
135
+ return os .getenv (setting_name )
136
+ try :
137
+ from secrets import secrets # pylint: disable=import-outside-toplevel
138
+ except ImportError :
139
+ secrets = {}
140
+ if old_setting_name in secrets .keys ():
141
+ self ._settings [setting_name ] = secrets [old_setting_name ]
142
+ return self ._settings [setting_name ]
143
+ if show_error :
144
+ if setting_name in ("CIRCUITPY_WIFI_SSID" , "CIRCUITPY_WIFI_PASSWORD" ):
145
+ print (
146
+ """WiFi settings are kept in settings.toml, please add them there!
147
+ the secrets dictionary must contain 'CIRCUITPY_WIFI_SSID' and 'CIRCUITPY_WIFI_PASSWORD'
148
+ at a minimum in order to use network related features"""
149
+ )
150
+ else :
151
+ print (
152
+ f"{ setting_name } not found. Please add this setting to settings.toml."
153
+ )
154
+ return None
155
+
127
156
def neo_status (self , value ):
128
157
"""The status NeoPixel.
129
158
@@ -186,15 +215,15 @@ def get_strftime(self, time_format, location=None):
186
215
api_url = None
187
216
reply = None
188
217
try :
189
- aio_username = self ._secrets [ "aio_username" ]
190
- aio_key = self ._secrets [ "aio_key" ]
218
+ aio_username = self ._get_setting ( "AIO_USERNAME" )
219
+ aio_key = self ._get_setting ( "AIO_KEY" )
191
220
except KeyError :
192
221
raise KeyError (
193
- "\n \n Our 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
222
+ "\n \n Our 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
194
223
) from KeyError
195
224
196
225
if location is None :
197
- location = self ._secrets . get ("timezone" , location )
226
+ location = self ._get_setting ("timezone" , False )
198
227
if location :
199
228
print ("Getting time for timezone" , location )
200
229
api_url = (TIME_SERVICE + "&tz=%s" ) % (aio_username , aio_key , location )
@@ -337,23 +366,24 @@ def connect(self, max_attempts=10):
337
366
338
367
"""
339
368
340
- if not self ._secrets_network :
341
- if "networks" in self ._secrets :
342
- if isinstance (self ._secrets ["networks" ], (list , tuple )):
343
- self ._secrets_network = self ._secrets ["networks" ]
369
+ if not self ._wifi_credentials :
370
+ self ._wifi_credentials = [
371
+ {
372
+ "ssid" : self ._get_setting ("CIRCUITPY_WIFI_SSID" ),
373
+ "password" : self ._get_setting ("CIRCUITPY_WIFI_PASSWORD" ),
374
+ }
375
+ ]
376
+
377
+ networks = self ._get_setting ("networks" , False )
378
+ if networks is not None :
379
+ if isinstance (networks , (list , tuple )):
380
+ self ._wifi_credentials = networks
344
381
else :
345
382
raise TypeError (
346
383
"'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
347
384
)
348
- else :
349
- self ._secrets_network = [
350
- {
351
- "ssid" : self ._secrets ["ssid" ],
352
- "password" : self ._secrets ["password" ],
353
- }
354
- ]
355
-
356
- for secret_entry in self ._secrets_network :
385
+
386
+ for secret_entry in self ._wifi_credentials :
357
387
self ._wifi .neo_status (STATUS_CONNECTING )
358
388
attempt = 1
359
389
@@ -365,10 +395,14 @@ def connect(self, max_attempts=10):
365
395
or secret_entry ["password" ] == "CHANGE ME"
366
396
):
367
397
change_me = "\n " + "*" * 45
368
- change_me += "\n Please update the 'secrets.py ' file on your\n "
398
+ change_me += "\n Please update the 'settings.toml ' file on your\n "
369
399
change_me += "CIRCUITPY drive to include your local WiFi\n "
370
- change_me += "access point SSID name in 'ssid' and SSID\n "
371
- change_me += "password in 'password'. Then save to reload!\n "
400
+ change_me += (
401
+ "access point SSID name in 'CIRCUITPY_WIFI_SSID' and SSID\n "
402
+ )
403
+ change_me += (
404
+ "password in 'CIRCUITPY_WIFI_PASSWORD'. Then save to reload!\n "
405
+ )
372
406
change_me += "*" * 45
373
407
raise OSError (change_me )
374
408
self ._wifi .neo_status (STATUS_NO_CONNECTION ) # red = not connected
@@ -400,8 +434,8 @@ def _get_io_client(self):
400
434
self .connect ()
401
435
402
436
try :
403
- aio_username = self ._secrets [ "aio_username" ]
404
- aio_key = self ._secrets [ "aio_key" ]
437
+ aio_username = self ._get_setting ( "AIO_USERNAME" )
438
+ aio_key = self ._get_setting ( "AIO_KEY" )
405
439
except KeyError :
406
440
raise KeyError (
407
441
"Adafruit IO secrets are kept in secrets.py, please add them there!\n \n "
0 commit comments