diff --git a/adafruit_lifx.py b/adafruit_lifx.py index cb0becd..434a4a4 100644 --- a/adafruit_lifx.py +++ b/adafruit_lifx.py @@ -21,6 +21,12 @@ * Adafruit ESP32SPI or ESP_ATcontrol library: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol + +or: + +* Adafruit_requests library: + https://github.com/adafruit/Adafruit_CircuitPython_Requests + """ __version__ = "0.0.0-auto.0" @@ -38,13 +44,15 @@ def __init__(self, wifi_manager, lifx_token): """ Creates an instance of the LIFX HTTP API client. :param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager + or session from adafruit_requests.Session :param str lifx_token: LIFX API token (https://api.developer.lifx.com/docs/authentication) """ wifi_type = str(type(wifi_manager)) - if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type: + 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 object.") + raise TypeError("This library requires a WiFiManager or Session object.") self._lifx_token = lifx_token self._auth_header = { "Authorization": "Bearer %s" % self._lifx_token, diff --git a/examples/lifx_simpletest_esp32s2.py b/examples/lifx_simpletest_esp32s2.py new file mode 100644 index 0000000..2898fa2 --- /dev/null +++ b/examples/lifx_simpletest_esp32s2.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import board +import busio +from digitalio import DigitalInOut +import wifi +import socketpool +import ssl +import adafruit_requests +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 + +# Set up ESP32-S2 and adafruit_requests session +wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) +pool = socketpool.SocketPool(wifi.radio) +http_session = adafruit_requests.Session(pool, ssl.create_default_context()) + +# Add your LIFX Personal Access token to secrets.py +# (to obtain a token, visit: https://cloud.lifx.com/settings) +lifx_token = secrets["lifx_token"] + +# Set this to your LIFX light separator label +# https://api.developer.lifx.com/docs/selectors +lifx_light = "label:Lamp" + +# Initialize the LIFX API Client +lifx = adafruit_lifx.LIFX(http_session, lifx_token) + +# List all lights +lights = lifx.list_lights() + +# Turn on the light +print("Turning on light...") +lifx.toggle_light(lifx_light) + +# Set the light's brightness to 50% +light_brightness = 0.5 +lifx.set_brightness(lifx_light, light_brightness) + +# Cycle the light using the colors of the Python logo +colors = ["yellow", "blue", "white"] +for color in colors: + print("Setting light to: ", color) + lifx.set_color(lifx_light, power="on", color=color, brightness=light_brightness) + +# Turn off the light +print("Turning off light...") +lifx.toggle_light(lifx_light) diff --git a/requirements.txt b/requirements.txt index f772f7b..eb6fcbc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ Adafruit-Blinka Adafruit_CircuitPython_ESP32SPI +adafruit_circuitpython_requests