38
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
39
39
40
40
# you'll need to pass in an io username and key
41
- TIME_SERVICE = (
42
- "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s"
43
- )
41
+ TIME_SERVICE = "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s"
44
42
# our strftime is %Y-%m-%d %H:%M:%S.%L %j %u %z %Z see http://strftime.net/ for decoding details
45
43
# See https://apidock.com/ruby/DateTime/strftime for full options
46
44
TIME_SERVICE_FORMAT = "%Y-%m-%d %H:%M:%S.%L %j %u %z %Z"
@@ -146,9 +144,7 @@ def _get_setting(self, setting_name, show_error=True):
146
144
at a minimum in order to use network related features"""
147
145
)
148
146
else :
149
- print (
150
- f"{ setting_name } not found. Please add this setting to settings.toml."
151
- )
147
+ print (f"{ setting_name } not found. Please add this setting to settings.toml." )
152
148
return None
153
149
154
150
def neo_status (self , value ):
@@ -170,16 +166,12 @@ def json_traverse(json, path):
170
166
"""
171
167
value = json
172
168
if not isinstance (path , (list , tuple )):
173
- raise ValueError (
174
- "The json_path parameter should be enclosed in a list or tuple."
175
- )
169
+ raise ValueError ("The json_path parameter should be enclosed in a list or tuple." )
176
170
for x in path :
177
171
try :
178
172
value = value [x ]
179
173
except (TypeError , KeyError , IndexError ) as error :
180
- raise ValueError (
181
- "The specified json_path was not found in the results."
182
- ) from error
174
+ raise ValueError ("The specified json_path was not found in the results." ) from error
183
175
gc .collect ()
184
176
return value
185
177
@@ -206,7 +198,7 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
206
198
Fetch a custom strftime relative to your location.
207
199
208
200
:param str location: Your city and country, e.g. ``"America/New_York"``.
209
- :param max_attempts: The maximum number of of attempts to connect to WiFi before
201
+ :param max_attempts: The maximum number of attempts to connect to WiFi before
210
202
failing or use None to disable. Defaults to 10.
211
203
212
204
"""
@@ -218,7 +210,9 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
218
210
aio_key = self ._get_setting ("AIO_KEY" )
219
211
except KeyError :
220
212
raise KeyError (
221
- "\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'"
213
+ "\n \n Our time service requires a login/password to rate-limit. "
214
+ "Please register for a free adafruit.io account and place the user/key "
215
+ "in your secrets file under 'AIO_USERNAME' and 'AIO_KEY'"
222
216
) from KeyError
223
217
224
218
if location is None :
@@ -238,8 +232,7 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
238
232
if response .status_code != 200 :
239
233
print (response )
240
234
error_message = (
241
- "Error connecting to Adafruit IO. The response was: "
242
- + response .text
235
+ "Error connecting to Adafruit IO. The response was: " + response .text
243
236
)
244
237
self .neo_status (STATUS_HTTP_ERROR )
245
238
raise RuntimeError (error_message )
@@ -260,16 +253,15 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
260
253
261
254
def get_local_time (self , location = None , max_attempts = 10 ):
262
255
"""
263
- Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
256
+ Fetch and "set" the local time of this microcontroller to the local time at the location,
257
+ using an internet time API.
264
258
265
259
:param str location: Your city and country, e.g. ``"America/New_York"``.
266
- :param max_attempts: The maximum number of of attempts to connect to WiFi before
260
+ :param max_attempts: The maximum number of attempts to connect to WiFi before
267
261
failing or use None to disable. Defaults to 10.
268
262
269
263
"""
270
- reply = self .get_strftime (
271
- TIME_SERVICE_FORMAT , location = location , max_attempts = max_attempts
272
- )
264
+ reply = self .get_strftime (TIME_SERVICE_FORMAT , location = location , max_attempts = max_attempts )
273
265
if reply :
274
266
times = reply .split (" " )
275
267
the_date = times [0 ]
@@ -319,9 +311,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
319
311
print ("Date: {}" .format (headers ["date" ]))
320
312
self .neo_status (STATUS_HTTP_ERROR ) # red = http error
321
313
raise HttpError (
322
- "Code {}: {}" .format (
323
- response .status_code , response .reason .decode ("utf-8" )
324
- ),
314
+ "Code {}: {}" .format (response .status_code , response .reason .decode ("utf-8" )),
325
315
response ,
326
316
)
327
317
@@ -340,10 +330,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
340
330
remaining -= len (i )
341
331
file .write (i )
342
332
if self ._debug :
343
- print (
344
- "Read %d bytes, %d remaining"
345
- % (content_length - remaining , remaining )
346
- )
333
+ print ("Read %d bytes, %d remaining" % (content_length - remaining , remaining ))
347
334
else :
348
335
print ("." , end = "" )
349
336
if not remaining :
@@ -352,9 +339,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
352
339
353
340
response .close ()
354
341
stamp = time .monotonic () - stamp
355
- print (
356
- "Created file of %d bytes in %0.1f seconds" % (os .stat (filename )[6 ], stamp )
357
- )
342
+ print ("Created file of %d bytes in %0.1f seconds" % (os .stat (filename )[6 ], stamp ))
358
343
self .neo_status (STATUS_OFF )
359
344
if not content_length == os .stat (filename )[6 ]:
360
345
raise RuntimeError
@@ -363,7 +348,7 @@ def connect(self, max_attempts=10):
363
348
"""
364
349
Connect to WiFi using the settings found in secrets.py
365
350
366
- :param max_attempts: The maximum number of of attempts to connect to WiFi before
351
+ :param max_attempts: The maximum number of attempts to connect to WiFi before
367
352
failing or use None to disable. Defaults to 10.
368
353
369
354
"""
@@ -392,19 +377,12 @@ def connect(self, max_attempts=10):
392
377
while not self ._wifi .is_connected :
393
378
# secrets dictionary must contain 'ssid' and 'password' at a minimum
394
379
print ("Connecting to AP" , secret_entry ["ssid" ])
395
- if (
396
- secret_entry ["ssid" ] == "CHANGE ME"
397
- or secret_entry ["password" ] == "CHANGE ME"
398
- ):
380
+ if secret_entry ["ssid" ] == "CHANGE ME" or secret_entry ["password" ] == "CHANGE ME" :
399
381
change_me = "\n " + "*" * 45
400
382
change_me += "\n Please update the 'settings.toml' file on your\n "
401
383
change_me += "CIRCUITPY drive to include your local WiFi\n "
402
- change_me += (
403
- "access point SSID name in 'CIRCUITPY_WIFI_SSID' and SSID\n "
404
- )
405
- change_me += (
406
- "password in 'CIRCUITPY_WIFI_PASSWORD'. Then save to reload!\n "
407
- )
384
+ change_me += "access point SSID name in 'CIRCUITPY_WIFI_SSID' and SSID\n "
385
+ change_me += "password in 'CIRCUITPY_WIFI_PASSWORD'. Then save to reload!\n "
408
386
change_me += "*" * 45
409
387
raise OSError (change_me )
410
388
self ._wifi .neo_status (STATUS_NO_CONNECTION ) # red = not connected
@@ -425,9 +403,7 @@ def connect(self, max_attempts=10):
425
403
if self ._wifi .is_connected :
426
404
return
427
405
428
- raise OSError (
429
- "Maximum number of attempts reached when trying to connect to WiFi"
430
- )
406
+ raise OSError ("Maximum number of attempts reached when trying to connect to WiFi" )
431
407
432
408
def _get_io_client (self ):
433
409
if self ._io_client is not None :
@@ -609,9 +585,7 @@ def check_response(self, response):
609
585
print ("Date: {}" .format (headers ["date" ]))
610
586
self .neo_status ((100 , 0 , 0 )) # red = http error
611
587
raise HttpError (
612
- "Code {}: {}" .format (
613
- response .status_code , response .reason .decode ("utf-8" )
614
- ),
588
+ "Code {}: {}" .format (response .status_code , response .reason .decode ("utf-8" )),
615
589
response ,
616
590
)
617
591
0 commit comments