Skip to content

pyportal.get_local_time() failure, NoneType #24

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

Closed
brentru opened this issue Mar 26, 2021 · 2 comments · Fixed by adafruit/Adafruit_CircuitPython_PyPortal#108
Closed
Assignees
Labels
bug Something isn't working

Comments

@brentru
Copy link
Member

brentru commented Mar 26, 2021

Calls to pyportal.get_local_time(secrets["timezone"]) fail with error:

  File "/lib/adafruit_portalbase/__init__.py", line 403, in get_local_time
  File "/lib/adafruit_portalbase/network.py", line 233, in get_local_time
  File "/lib/adafruit_portalbase/network.py", line 216, in get_strftime
  File "/lib/adafruit_portalbase/network.py", line 202, in get_strftime
AttributeError: 'NoneType' object has no attribute 'get'

Printing on L198 (https://github.com/adafruit/Adafruit_CircuitPython_PortalBase/blob/main/adafruit_portalbase/network.py#L198):

            print(self._wifi)
            print(dir(self._wifi))
            print(self._wifi.requests)

Yields a None type for self._wifi.requests.

Getting time for timezone Etc/UTC
<WiFi object at 20003fe0>
['__class__', '__dict__', '__init__', '__module__', '__qualname__', 'connect', 'enabled', 'requests', 'esp', 'is_connected', 'neo_status', 'manager', 'neopix', '_manager']
None

Example code for where this occurs: https://learn.adafruit.com/pyportal-google-calendar-event-display/code-usage

@brentru brentru added the bug Something isn't working label Mar 26, 2021
@Neradoc
Copy link
Contributor

Neradoc commented Apr 7, 2021

So I just encountered that on the help channel on discord, and the quick solution with that demo was to remove:

print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(secrets["ssid"], secrets["password"])
    except RuntimeError as e:
        print("could not connect to AP, retrying: ", e)
        continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

and replace with this (a few lines lower)

pyportal.network.connect()

Here: https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/5fd9a2e74dbfd6056136a79e2ca3ad645f1a7680/PyPortal_Google_Calendar/code.py#L71-L81

For me it's a problem with duplicating features when using a large device-name library. You can't do that with pins for example because they will be "already in use", and it shouldn't be done with the wifi connection either in these libraries. I would also advise to use requests = pyportal.network.requests or PortalBase.fetch() and similar, especially on the MagTag where you'll otherwise end up with multiple requests.Session (that's a different issue though).

A workaround to still allow connecting outside of the library would be to add something like this to PyPortal.connect(), where setup_when_connected() would basically be _wifi.connect() minus the connection part. Each board module based on PortalBase would need to be modified.

    while not self._wifi.is_connected:
        [...]
    # add:
    if not self.requests:
        self._wifi.setup_after_connection()
        self.requests = self._wifi.requests

After the while loop:

while not self._wifi.is_connected:

That's because that connection code that sets up requests is called if and only if the board is not currently connected to wifi, even when called from get_local_time().

@colejd
Copy link

colejd commented Jul 29, 2021

I'm still seeing this error output on the latest version of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants