phlyght/example.py

93 lines
3.3 KiB
Python

from asyncio import get_running_loop, sleep
from logging import getLogger
from random import randint, random
from phlyght import Attributes, HueEntsV2, Phlyght
logger = getLogger()
class PhlyghtClient(Phlyght):
async def on_light_update(self, light: HueEntsV2.Light):
...
async def on_button_update(self, button: HueEntsV2.Button):
if button.id == self.button_1.id:
gradient = None
mirek = None
color = None
brightness = 100.0
alternate_brightness = False
match button.button.last_event:
case "short_release":
color = Attributes.LightColor(
xy=Attributes.XY(x=random(), y=random())
)
mirek = randint(153, 500)
alternate_brightness = True
case "long_release":
mirek = 500
brightness = 100.0
color = Attributes.LightColor(xy=Attributes.XY(x=0.99, y=0.99))
gradient = Attributes.Gradient(
points=[
Attributes.ColorPointColor(
color=Attributes.ColorPoint(
xy=Attributes.XY(x=0.99, y=0.88)
)
),
Attributes.ColorPointColor(
color=Attributes.ColorPoint(
xy=Attributes.XY(x=0.88, y=0.99)
)
),
],
points_capable=2,
)
case "repeat":
brightness = 50.0
color = Attributes.LightColor(
xy=Attributes.XY(x=random(), y=random())
)
for _light in [
self.light_1, # These are all assumed aliased light names
self.light_2,
self.light_3,
self.light_4,
self.light_5,
]:
if color:
_light.color = color
if brightness:
if alternate_brightness:
if _light.dimming.brightness < 30:
brightness = 75.0
else:
brightness = 5.0
_light.dimming = Attributes.Dimming(brightness=brightness)
if mirek:
_light.color_temperature = Attributes.ColorTemp(mirek=mirek)
if gradient:
_light.gradient = gradient
await _light.update()
async def _shift(self, light: HueEntsV2.Light):
while get_running_loop().is_running():
# We can set the values by explicitly setting the attributes
light.color = Attributes.LightColor(xy=Attributes.XY(x=0.99, y=0.88))
await light.update()
await sleep(0.3)
async def on_ready(self): # This will be called once, right after startup
logger.info("Phlyght is ready")
await self.dump("config.yaml")
...
client = PhlyghtClient(max_cache_size=64) # , hue_api_key="", hue_bridge_ip="")
client.run()