Merge branch 'refs/heads/main' into ruff-updates

# Conflicts:
#	adafruit_portalbase/__init__.py
#	adafruit_portalbase/graphics.py
#	adafruit_portalbase/network.py
This commit is contained in:
foamyguy 2024-07-29 10:34:19 -05:00
commit ea7fb41be5
2 changed files with 12 additions and 6 deletions

View file

@ -480,12 +480,12 @@ class PortalBase:
self._fetch_set_text(string, index=i)
value_index += 1
def get_local_time(self, location=None):
def get_local_time(self, location=None, max_attempts=10):
"""Accessor function for get_local_time()"""
if self.network is None:
raise RuntimeError("network must not be None to use get_local_time()")
return self.network.get_local_time(location=location)
return self.network.get_local_time(location=location, max_attempts=max_attempts)
def push_to_io(self, feed_key, data, metadata=None, precision=None):
"""Push data to an adafruit.io feed

View file

@ -201,14 +201,16 @@ class NetworkBase:
"""
return url.replace(" ", "+").replace("%", "%25").replace(":", "%3A")
def get_strftime(self, time_format, location=None):
def get_strftime(self, time_format, location=None, max_attempts=10):
"""
Fetch a custom strftime relative to your location.
:param str location: Your city and country, e.g. ``"America/New_York"``.
:param max_attempts: The maximum number of of attempts to connect to WiFi before
failing or use None to disable. Defaults to 10.
"""
self.connect()
self.connect(max_attempts=max_attempts)
api_url = None
reply = None
try:
@ -256,14 +258,18 @@ class NetworkBase:
return reply
def get_local_time(self, location=None):
def get_local_time(self, location=None, max_attempts=10):
"""
Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
:param str location: Your city and country, e.g. ``"America/New_York"``.
:param max_attempts: The maximum number of of attempts to connect to WiFi before
failing or use None to disable. Defaults to 10.
"""
reply = self.get_strftime(TIME_SERVICE_FORMAT, location=location)
reply = self.get_strftime(
TIME_SERVICE_FORMAT, location=location, max_attempts=max_attempts
)
if reply:
times = reply.split(" ")
the_date = times[0]