Skip to content

Instantly share code, notes, and snippets.

@slaiyer
Created June 26, 2021 15:43
Show Gist options
  • Save slaiyer/daec8b6d617c01168a32eb1811aeaf1e to your computer and use it in GitHub Desktop.
Save slaiyer/daec8b6d617c01168a32eb1811aeaf1e to your computer and use it in GitHub Desktop.
Yeelight color setter
#!/usr/bin/env python3
"""Yeelight color setter."""
import argparse as arg
import functools as func
import pathlib as path
import sys
import yeelight as y
def main(
args: arg.Namespace,
) -> int:
"""Entry point."""
set_hsv_bulb = func.partial(
set_hsv,
hue=args.hue,
saturation=args.saturation,
lightness=args.lightness,
)
bulb_ip_file = path.Path(__file__).parent / 'bulb-ip'
try:
with open(bulb_ip_file, 'r') as file:
bulb_ip = file.readline().strip()
set_hsv_bulb(bulb_ip=bulb_ip)
except (FileNotFoundError, y.BulbException):
bulb_ip = next(
iter(
y.discover_bulbs(),
)
)['ip'].strip()
set_hsv_bulb(bulb_ip=bulb_ip)
with open(bulb_ip_file, 'w') as file:
file.write(bulb_ip)
return 0
def set_hsv(
bulb_ip: str,
hue: int,
saturation: int,
lightness: int,
) -> None:
"""Starts a flow."""
y.Bulb(
bulb_ip,
power_mode=y.PowerMode.HSV,
auto_on=True,
).set_hsv(
hue=hue,
saturation=saturation,
value=lightness,
)
if __name__ == '__main__':
hue_range = range(1, 361)
percent_range = range(1, 101)
parser = arg.ArgumentParser()
parser.add_argument(
'--hue',
'-c',
type=int,
choices=hue_range,
default=17,
help=f'Hue ({hue_range.start}-{hue_range.stop})',
)
parser.add_argument(
'--saturation',
'-s',
type=int,
choices=percent_range,
default=percent_range.stop - 1,
help='Saturation (%%)',
)
parser.add_argument(
'--lightness',
'-l',
type=int,
choices=percent_range,
default=percent_range.start,
help='Lightness (%%)',
)
arguments = parser.parse_args()
sys.exit(
main(args=arguments),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment