Skip to content

Add support for adafruit_requests.Session #11

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

Merged
merged 6 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions adafruit_lifx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
57 changes: 57 additions & 0 deletions examples/lifx_simpletest_esp32s2.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

Adafruit-Blinka
Adafruit_CircuitPython_ESP32SPI
adafruit_circuitpython_requests