diff options
-rw-r--r-- | app.py | 72 | ||||
-rw-r--r-- | docker-compose.yml | 1 |
2 files changed, 40 insertions, 33 deletions
@@ -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", + ), ) diff --git a/docker-compose.yml b/docker-compose.yml index 9ed9244..3a2695e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,4 @@ services: - lux: container_name: lux image: lux:latest |