Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max_attempts to get_local_time #103

Merged
merged 4 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions adafruit_portalbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PortalBase:

"""

# pylint: disable=too-many-instance-attributes, too-many-branches, too-many-public-methods
# pylint: disable=too-many-instance-attributes, too-many-branches, too-many-public-methods, too-many-arguments
def __init__(
self,
network,
Expand Down Expand Up @@ -484,12 +484,12 @@ def _fill_text_labels(self, values):
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
Expand Down
2 changes: 1 addition & 1 deletion adafruit_portalbase/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def set_background(self, file_or_color, position=None):

def qrcode(
self, qr_data, *, qr_size=1, x=0, y=0, qr_color=0x000000
): # pylint: disable=invalid-name
): # pylint: disable=invalid-name, too-many-arguments
"""Display a QR code

:param qr_data: The data for the QR code, None to remove.
Expand Down
16 changes: 11 additions & 5 deletions adafruit_portalbase/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,17 @@ def url_encode(url):
"""
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.

"""
# pylint: disable=line-too-long
self.connect()
self.connect(max_attempts=max_attempts)
api_url = None
reply = None
try:
Expand Down Expand Up @@ -259,15 +261,19 @@ def get_strftime(self, time_format, location=None):

return reply

def get_local_time(self, location=None):
def get_local_time(self, location=None, max_attempts=10):
# pylint: disable=line-too-long
"""
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]
Expand Down Expand Up @@ -631,7 +637,7 @@ def fetch_data(
json_path=None,
regexp_path=None,
timeout=10,
):
): # pylint: disable=too-many-arguments
"""Fetch data from the specified url and perfom any parsing

:param str url: The URL to fetch from.
Expand Down
Loading