diff --git a/adafruit_lifx.py b/adafruit_lifx.py index 5028e74..6e3af0d 100644 --- a/adafruit_lifx.py +++ b/adafruit_lifx.py @@ -45,18 +45,17 @@ class LIFX: """HTTP Interface for interacting with the LIFX API - :param wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager - or session from adafruit_requests.Session + :param wifi_manager wifi_manager: WiFiManager or Session :param str lifx_token: LIFX API token (https://api.developer.lifx.com/docs/authentication) """ def __init__(self, wifi_manager: HTTPProtocol, lifx_token: str) -> None: - wifi_type = str(type(wifi_manager)) - allowed_wifi_types = ("ESPSPI_WiFiManager", "ESPAT_WiFiManager", "Session") - if any(x in wifi_type for x in allowed_wifi_types): - self._wifi = wifi_manager - else: - raise TypeError("This library requires a WiFiManager or Session object.") + for attr in ("get", "post", "put"): + if not hasattr(wifi_manager, attr): + error = "This library requires a WiFiManager or Session object with a " + error += f"`{attr}` method, not {type(wifi_manager)}" + raise TypeError(error) + self._wifi = wifi_manager self._lifx_token = lifx_token self._auth_header = { "Authorization": "Bearer %s" % self._lifx_token, diff --git a/examples/lifx_simpletest.py b/examples/lifx_simpletest.py index 98b0221..4766c3d 100644 --- a/examples/lifx_simpletest.py +++ b/examples/lifx_simpletest.py @@ -1,21 +1,19 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import board import busio from digitalio import DigitalInOut from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager import neopixel import adafruit_lifx -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi and API secrets are kept in secrets.py, please add them there!") - raise +# Get WiFi details, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") # ESP32 SPI esp32_cs = DigitalInOut(board.ESP_CS) @@ -23,12 +21,15 @@ esp32_reset = DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) +wifi = WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Add your LIFX Personal Access token to secrets.py # (to obtain a token, visit: https://cloud.lifx.com/settings) -lifx_token = secrets["lifx_token"] +lifx_token = getenv("lifx_token") + +if lifx_token is None: + raise KeyError("Please add your lifx token to settings.toml") # Set this to your LIFX light separator label # https://api.developer.lifx.com/docs/selectors