Skip to content

Commit edf4346

Browse files
authored
Merge pull request #11 from todbot/master
Add support for adafruit_requests.Session
2 parents c4f41c1 + dc2ab8b commit edf4346

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

adafruit_lifx.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
* Adafruit ESP32SPI or ESP_ATcontrol library:
2222
https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI
2323
https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol
24+
25+
or:
26+
27+
* Adafruit_requests library:
28+
https://github.com/adafruit/Adafruit_CircuitPython_Requests
29+
2430
"""
2531

2632
__version__ = "0.0.0-auto.0"
@@ -38,13 +44,15 @@ def __init__(self, wifi_manager, lifx_token):
3844
"""
3945
Creates an instance of the LIFX HTTP API client.
4046
:param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
47+
or session from adafruit_requests.Session
4148
:param str lifx_token: LIFX API token (https://api.developer.lifx.com/docs/authentication)
4249
"""
4350
wifi_type = str(type(wifi_manager))
44-
if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type:
51+
allowed_wifi_types = ("ESPSPI_WiFiManager", "ESPAT_WiFiManager", "Session")
52+
if any(x in wifi_type for x in allowed_wifi_types):
4553
self._wifi = wifi_manager
4654
else:
47-
raise TypeError("This library requires a WiFiManager object.")
55+
raise TypeError("This library requires a WiFiManager or Session object.")
4856
self._lifx_token = lifx_token
4957
self._auth_header = {
5058
"Authorization": "Bearer %s" % self._lifx_token,

examples/lifx_simpletest_esp32s2.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import busio
6+
from digitalio import DigitalInOut
7+
import wifi
8+
import socketpool
9+
import ssl
10+
import adafruit_requests
11+
import neopixel
12+
13+
import adafruit_lifx
14+
15+
# Get wifi details and more from a secrets.py file
16+
try:
17+
from secrets import secrets
18+
except ImportError:
19+
print("WiFi and API secrets are kept in secrets.py, please add them there!")
20+
raise
21+
22+
# Set up ESP32-S2 and adafruit_requests session
23+
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"])
24+
pool = socketpool.SocketPool(wifi.radio)
25+
http_session = adafruit_requests.Session(pool, ssl.create_default_context())
26+
27+
# Add your LIFX Personal Access token to secrets.py
28+
# (to obtain a token, visit: https://cloud.lifx.com/settings)
29+
lifx_token = secrets["lifx_token"]
30+
31+
# Set this to your LIFX light separator label
32+
# https://api.developer.lifx.com/docs/selectors
33+
lifx_light = "label:Lamp"
34+
35+
# Initialize the LIFX API Client
36+
lifx = adafruit_lifx.LIFX(http_session, lifx_token)
37+
38+
# List all lights
39+
lights = lifx.list_lights()
40+
41+
# Turn on the light
42+
print("Turning on light...")
43+
lifx.toggle_light(lifx_light)
44+
45+
# Set the light's brightness to 50%
46+
light_brightness = 0.5
47+
lifx.set_brightness(lifx_light, light_brightness)
48+
49+
# Cycle the light using the colors of the Python logo
50+
colors = ["yellow", "blue", "white"]
51+
for color in colors:
52+
print("Setting light to: ", color)
53+
lifx.set_color(lifx_light, power="on", color=color, brightness=light_brightness)
54+
55+
# Turn off the light
56+
print("Turning off light...")
57+
lifx.toggle_light(lifx_light)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
Adafruit_CircuitPython_ESP32SPI
7+
adafruit_circuitpython_requests

0 commit comments

Comments
 (0)