aboutsummaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
authorMichael McVady <michaelm@telnyx.com>2025-09-29 17:47:53 -0500
committerMichael McVady <michaelm@telnyx.com>2025-09-29 17:47:53 -0500
commit568ff6592ba7ea6650900ac1411e217bf045dfd6 (patch)
tree130a65e56522619fd3144412702ad396a2dd7020 /app.py
parent3f9be3294f254a96d91532001ac12861a66f9f53 (diff)
Only set one light (`lux`)
Diffstat (limited to 'app.py')
-rw-r--r--app.py72
1 files changed, 40 insertions, 32 deletions
diff --git a/app.py b/app.py
index e36d571..1c0ba8c 100644
--- a/app.py
+++ b/app.py
@@ -4,19 +4,14 @@ from dataclasses import dataclass
from random import randint
from jinja2 import Environment
-from lifxlan import LifxLAN
+from lifxlan import LifxLAN, Light
from litestar import Litestar, get, post
from litestar.contrib.jinja import JinjaTemplateEngine
+from litestar.di import Provide
+from litestar.logging import LoggingConfig
from litestar.response import Template
from litestar.template import TemplateConfig
-logging.basicConfig(level=logging.DEBUG)
-log = logging.getLogger()
-
-# FIXME: discovery happens here, but things can change.
-# Try power cycling the light and see what happens.
-lifxlan = LifxLAN()
-
DEFAULT_COLOR = "#FF0000"
TEMPLATE_STR = """<!DOCTYPE html>
<html>
@@ -30,6 +25,11 @@ TEMPLATE_STR = """<!DOCTYPE html>
</html>
"""
+lux: Light | None = None
+
+logging.basicConfig(level=logging.DEBUG)
+log = logging.getLogger()
+
@dataclass
class RGBColor:
@@ -69,6 +69,21 @@ class HSBKColor:
return f"HSBKColor(hue={self.hue}, saturation={self.saturation}, brightness={self.brightness}, kelvin={self.kelvin})"
+def get_lux() -> Light:
+ global lux
+ if lux:
+ return lux
+
+ for light in LifxLAN().get_lights():
+ if light.get_label() == "lux":
+ log.info(f"Found lux light: {light.get_label()} {light.get_mac_addr()}")
+ lux = light
+ return lux
+
+ log.error("Could not find lux light")
+ raise RuntimeError("Could not find lux light")
+
+
def get_random_hsbk_color() -> HSBKColor:
return HSBKColor(
hue=randint(0, 65535),
@@ -78,15 +93,13 @@ def get_random_hsbk_color() -> HSBKColor:
)
-def random_light() -> Template:
+def random_color(lux: Light) -> Template:
hsbk = get_random_hsbk_color()
log.info(f"Setting color to {hsbk} {hsbk.rgb}")
try:
- # colors = lifxlan.get_color_all_lights()
- lifxlan.set_color_all_lights(
- [hsbk.hue, hsbk.saturation, hsbk.brightness, hsbk.kelvin],
- rapid=True,
+ lux.set_color(
+ [hsbk.hue, hsbk.saturation, hsbk.brightness, hsbk.kelvin], rapid=True
)
except Exception as e:
log.exception("Error setting color")
@@ -109,30 +122,15 @@ def random_light() -> Template:
@get("/")
-async def get_index() -> Template:
+async def get_index(lux: Light = Provide(get_lux)) -> Template:
"""Return index"""
- return random_light()
+ return random_color(lux)
@post("/")
-async def post_index() -> Template:
+async def post_index(lux: Light = Provide(get_lux)) -> Template:
"""Update index"""
- return random_light()
-
-
-# @post("/")
-# async def post_index(
-# hue: int, saturation: int, brightness: int, kelvin: int
-# ) -> dict[str, str]:
-# try:
-# lifxlan.set_color_all_lights(
-# [hue, saturation, brightness, kelvin],
-# rapid=True,
-# )
-# except Exception as e:
-# return {"status": "error", "message": str(e)}
-
-# return {"status": "ok"}
+ return random_color(lux)
app = Litestar(
@@ -140,7 +138,17 @@ app = Litestar(
get_index,
post_index,
],
+ dependencies={"lux": Provide(get_lux)},
template_config=TemplateConfig(
instance=JinjaTemplateEngine.from_environment(Environment())
),
+ logging_config=LoggingConfig(
+ root={"level": "INFO", "handlers": ["queue_listener"]},
+ formatters={
+ "standard": {
+ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
+ }
+ },
+ log_exceptions="always",
+ ),
)