aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael McVady <michaelm@telnyx.com>2024-01-28 21:29:27 -0600
committerMichael McVady <michaelm@telnyx.com>2024-01-28 21:29:27 -0600
commitefa3572376e4402dbe1f55e2ae72c9b6e3019fbf (patch)
treefe2a1997be51c3bea78aac53cc59f6908d0e6cb6
parent89e72db15c4d4feac71a0a84e9be431ccb22d548 (diff)
Add HTML content
-rw-r--r--README.md5
-rw-r--r--app.py69
-rw-r--r--requirements.txt1
3 files changed, 67 insertions, 8 deletions
diff --git a/README.md b/README.md
index 1f48fd3..2de66c5 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,8 @@
# lux
Controls a [lifx lightbulb](https://www.lifx.com/collections/lightbulbs) somewhere in the world.
+
+Mostly an excuse to checkout
+[litestar](https://litestar.dev/)
+and
+[lifxlan](https://github.com/mclarkk/lifxlan). \ No newline at end of file
diff --git a/app.py b/app.py
index 954edc1..0cc26b3 100644
--- a/app.py
+++ b/app.py
@@ -1,31 +1,81 @@
+import logging
+from colorsys import hsv_to_rgb
from random import randint
+from textwrap import dedent
+from jinja2 import Environment
from lifxlan import LifxLAN
from litestar import Litestar, get, post
+from litestar.contrib.jinja import JinjaTemplateEngine
+from litestar.response import Template
+from litestar.template import TemplateConfig
lifxlan = LifxLAN()
+DEFAULT_COLOR = "#FF0000"
+
+logging.basicConfig(level=logging.DEBUG)
+log = logging.getLogger()
+
@get("/")
-async def get_index() -> dict[str, str]:
+async def get_index() -> Template:
+ template_string = dedent(
+ """
+ <!DOCTYPE html>
+ <html>
+ <body style="background-color: {{ color }}">
+ <h1>{{ message }}</h1>
+ </body>
+ </html>
+ """
+ ).strip()
+
+ hue = randint(0, 65535)
+ saturation = randint(0, 65535)
+ brightness = randint(0, 65535)
+ kelvin = randint(0, 9000)
+ log.info(f"Setting color to {hue}, {saturation}, {brightness}, {kelvin}")
+
try:
# colors = lifxlan.get_color_all_lights()
+
lifxlan.set_color_all_lights(
- [randint(0, 65535), randint(0, 65535), randint(0, 65535), randint(0, 9000)],
+ [hue, saturation, brightness, kelvin],
rapid=True,
)
except Exception as e:
- return {"status": "error", "message": str(e)}
+ log.exception("Error setting color")
+ return Template(
+ template_str=template_string,
+ context={"color": DEFAULT_COLOR, "message": str(e)},
+ )
- # return {"status": str(colors)}
- return {"status": "ok"}
+ rgb_color = DEFAULT_COLOR
+ try:
+ r, g, b = hsv_to_rgb(hue / 65535, saturation / 65535, brightness / 65535)
+ log.info("RGB: %s, %s, %s", r, g, b)
+ rgb_color = "#%02x%02x%02x" % (int(r * 255), int(g * 255), int(b * 255))
+ log.info("hex RGB: %s", rgb_color)
+ except Exception as e:
+ log.exception("Error converting color")
+ return Template(
+ template_str=template_string,
+ context={"color": DEFAULT_COLOR, "message": str(e)},
+ )
+
+ return Template(
+ template_str=template_string, context={"color": rgb_color, "message": ""}
+ )
@post("/")
-async def post_index() -> dict[str, str]:
+async def post_index(
+ hue: int, saturation: int, brightness: int, kelvin: int
+) -> dict[str, str]:
try:
lifxlan.set_color_all_lights(
- [randint(0, 65535), randint(0, 65535), randint(0, 65535), randint(0, 9000)],
+ [hue, saturation, brightness, kelvin],
rapid=True,
)
except Exception as e:
@@ -38,5 +88,8 @@ app = Litestar(
[
get_index,
post_index,
- ]
+ ],
+ template_config=TemplateConfig(
+ instance=JinjaTemplateEngine.from_environment(Environment())
+ ),
)
diff --git a/requirements.txt b/requirements.txt
index b78356f..7bb223a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,2 +1,3 @@
lifxlan
litestar
+litestar[jinja2]