From bac8ecded37c45543418646a20c7990ac3569347 Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 21:47:02 -0700 Subject: [PATCH 1/6] add support for adafruit_requests --- adafruit_lifx.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_lifx.py b/adafruit_lifx.py index cb0becd..ff6a688 100644 --- a/adafruit_lifx.py +++ b/adafruit_lifx.py @@ -41,10 +41,10 @@ def __init__(self, wifi_manager, lifx_token): :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: + if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" or Session in wifi_type: 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, From 73ff95d07da503ee560289c0c5d9a819023f8b12 Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 21:52:35 -0700 Subject: [PATCH 2/6] added example usign adafruit_requests on ESP32-S2 --- examples/lifx_simpletest_esp32s2.py | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/lifx_simpletest_esp32s2.py diff --git a/examples/lifx_simpletest_esp32s2.py b/examples/lifx_simpletest_esp32s2.py new file mode 100644 index 0000000..e7b3fad --- /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) From dd7dd7dee15a57ae5cccf86d9cf54b57025147bc Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 22:05:53 -0700 Subject: [PATCH 3/6] typo --- adafruit_lifx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_lifx.py b/adafruit_lifx.py index ff6a688..482161a 100644 --- a/adafruit_lifx.py +++ b/adafruit_lifx.py @@ -41,7 +41,7 @@ def __init__(self, wifi_manager, lifx_token): :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" or Session in wifi_type: + if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" or "Session" in wifi_type: self._wifi = wifi_manager else: raise TypeError("This library requires a WiFiManager or Session object.") From 747337f4ef0ab11a8d47cceaba8a78bb863cc89d Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 22:27:24 -0700 Subject: [PATCH 4/6] fix pylint-detected bug in conditional --- adafruit_lifx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_lifx.py b/adafruit_lifx.py index 482161a..b9342ed 100644 --- a/adafruit_lifx.py +++ b/adafruit_lifx.py @@ -41,7 +41,7 @@ def __init__(self, wifi_manager, lifx_token): :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" or "Session" in wifi_type: + if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type or "Session" in wifi_type: self._wifi = wifi_manager else: raise TypeError("This library requires a WiFiManager or Session object.") From dd884ea3f3e5106ef48fdc08577ce9e24b952e38 Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 22:37:11 -0700 Subject: [PATCH 5/6] update requirements.txt for adafruit_requests --- requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From dc2ab8b484eb853756215ebc0c44b0f31431cdff Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Wed, 14 Apr 2021 22:58:39 -0700 Subject: [PATCH 6/6] fix lint issues --- adafruit_lifx.py | 10 +++++++++- examples/lifx_simpletest_esp32s2.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/adafruit_lifx.py b/adafruit_lifx.py index b9342ed..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,10 +44,12 @@ 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 or "Session" 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 or Session object.") diff --git a/examples/lifx_simpletest_esp32s2.py b/examples/lifx_simpletest_esp32s2.py index e7b3fad..2898fa2 100644 --- a/examples/lifx_simpletest_esp32s2.py +++ b/examples/lifx_simpletest_esp32s2.py @@ -20,7 +20,7 @@ raise # Set up ESP32-S2 and adafruit_requests session -wifi.radio.connect(ssid=secrets['ssid'],password=secrets['password']) +wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) pool = socketpool.SocketPool(wifi.radio) http_session = adafruit_requests.Session(pool, ssl.create_default_context())