Skip to content

Remove secrets and more #24

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 1 commit into from
Feb 23, 2025
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
13 changes: 7 additions & 6 deletions adafruit_hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ def __init__(
) -> None:
"""
Creates an instance of the Philips Hue Bridge Interface.
:param wifi_manager wifi_manager: WiFiManager from ESPSPI_WiFiManager/ESPAT_WiFiManager
:param wifi_manager wifi_manager: WiFiManager or Session
"""
wifi_type = str(type(wifi_manager))
if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type:
self._wifi = wifi_manager
else:
raise TypeError("This library requires a WiFiManager 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._ip = bridge_ip
self._username = username
if bridge_ip and username is not None:
Expand Down
34 changes: 16 additions & 18 deletions examples/hue_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,48 @@
# SPDX-License-Identifier: MIT

import time
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 Philips Hue Bridge
from adafruit_hue import Bridge

# 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)
esp32_ready = DigitalInOut(board.ESP_BUSY)
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)

# Attempt to load bridge username and IP address from secrets.py
try:
username = secrets["hue_username"]
bridge_ip = secrets["bridge_ip"]
my_bridge = Bridge(wifi, bridge_ip, username)
except:
# Attempt to load bridge username and IP address from settings.toml
username = getenv("hue_username")
bridge_ip = getenv("bridge_ip")

if username is None or bridge_ip is None:
# Perform first-time bridge setup
my_bridge = Bridge(wifi)
ip = my_bridge.discover_bridge()
username = my_bridge.register_username()
print(
'ADD THESE VALUES TO SECRETS.PY: \
raise KeyError(
'Please add these values to your settings.toml: \
\n\t"bridge_ip":"{0}", \
\n\t"hue_username":"{1}"'.format(
ip, username
)
)
raise

my_bridge = Bridge(wifi, bridge_ip, username)

# Enumerate all lights on the bridge
my_bridge.get_lights()
Expand Down