Skip to content

Commit 91bc610

Browse files
authored
Merge pull request #20 from geudrik/fix_ntp_requirement
Remove forced dep on ntp
2 parents 2d669e5 + 4fe3c2c commit 91bc610

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

adafruit_gc_iot_core.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import adafruit_logging as logging
3232
from adafruit_jwt import JWT
33-
import adafruit_ntp as NTP
3433

3534
__version__ = "0.0.0-auto.0"
3635
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core.git"
@@ -61,14 +60,15 @@ def __init__(self, mqtt_client):
6160
)
6261
# Verify that the MiniMQTT client was setup correctly.
6362
try:
64-
self.user = self._client.user
63+
# I have no idea where _client.user comes from, but ._username exists when .user doesn't
64+
self.user = self._client._username
6565
except Exception as err:
6666
raise TypeError(
6767
"Google Cloud Core IoT MQTT API requires a username."
6868
) from err
6969
# Validate provided JWT before connecting
7070
try:
71-
JWT.validate(self._client.password)
71+
JWT.validate(self._client._password) # Again, .password isn't valid here..
7272
except Exception as err:
7373
raise TypeError("Invalid JWT provided.") from err
7474
# If client has KeepAlive =0 or if KeepAlive > 20min,
@@ -296,10 +296,10 @@ class Cloud_Core:
296296
297297
"""
298298

299-
def __init__(self, esp, secrets, log=False):
299+
def __init__(self, esp=None, secrets=None, log=False):
300300
self._esp = esp
301301
# Validate Secrets
302-
if hasattr(secrets, "keys"):
302+
if secrets and hasattr(secrets, "keys"):
303303
self._secrets = secrets
304304
else:
305305
raise AttributeError(
@@ -343,15 +343,24 @@ def generate_jwt(self, ttl=43200, algo="RS256"):
343343
"""
344344
if self.logger:
345345
self.logger.debug("Generating JWT...")
346-
ntp = NTP.NTP(self._esp)
347-
ntp.set_time()
346+
347+
if self._esp is not None:
348+
# Not all boards have ESP access easily (eg: featherS2). If we pass in a False or None in init, lets
349+
# assume that we've handled setting the RTC outside of here
350+
import adafruit_ntp as NTP
351+
ntp = NTP.NTP(self._esp)
352+
ntp.set_time()
353+
else:
354+
if self.logger:
355+
self.logger.info(f"No self._esp instance found, assuming RTC has been previously set")
356+
348357
claims = {
349358
# The time that the token was issued at
350359
"iat": time.time(),
351360
# The time the token expires.
352361
"exp": time.time() + ttl,
353362
# The audience field should always be set to the GCP project id.
354-
"aud": self._proj_id,
363+
"aud": self._proj_id
355364
}
356365
jwt = JWT.generate(claims, self._private_key, algo)
357366
return jwt

0 commit comments

Comments
 (0)