Created
October 6, 2016 16:05
-
-
Save gidj/f1c4b1b5754b3d375460b32ecec2ea3d to your computer and use it in GitHub Desktop.
Revised lifxbulb script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- encoding: utf8 -*- | |
from __future__ import division, print_function, division, absolute_import | |
import argparse | |
import os | |
import pylifx | |
def power(bulb, state): | |
if state == 'on': | |
bulb.on() | |
elif state == 'off': | |
bulb.off() | |
else: | |
raise ValueError('Invalid State specified %s' % state) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
subparsers = parser.add_subparsers(dest='command') | |
power_parser = subparsers.add_parser('power', help='Set the bulb power to on or off') | |
power_parser.add_argument('state', choices=('on', 'off')) | |
rgb_parser = subparsers.add_parser('rgb', help='Set the Red/Green/Blue tint') | |
rgb_parser.add_argument('red', type=float) | |
rgb_parser.add_argument('green', type=float) | |
rgb_parser.add_argument('blue', type=float) | |
hsb_parser = subparsers.add_parser('hsb', help='Set the Hue/Saturation/Brightness') | |
hsb_parser.add_argument('hue', type=float, help='Hue value between 0 and 1') | |
hsb_parser.add_argument('saturation', type=float, help='Saturation value between 0 and 1') | |
hsb_parser.add_argument('brightness', type=float, help='Brightness value between 0 and 1') | |
temp_parser = subparsers.add_parser('temperature', help='Set overall color warmth of the bulb') | |
temp_parser.add_argument('kelvin', type=int, help='Temperature value between 0 and 65535') | |
parser.add_argument('--bulb', '-b', dest='bulb_addr', help='Specify a specific bulb you want to alter') | |
parser.add_argument('--fade-time', '-f', type=int, default=1, help='Specify a transition time') | |
args = parser.parse_args() | |
if args.bulb_addr is None: | |
config_path = os.path.expanduser('~/.lifx') | |
if os.path.exists(config_path): | |
with open(config_path, 'r') as fp: | |
args.bulb_addr = fp.read().strip() | |
elif 'LIFXBULB' in os.environ: | |
args.bulb_addr = os.environ['LIFXBULB'] | |
else: | |
raise ValueError('Bulb must be specified with --bulb argument or LIFXBULB environment variable') | |
with pylifx.LifxController(args.bulb_addr) as bulb: | |
if args.command == 'power': | |
power(bulb, args.state) | |
elif args.command == 'rgb': | |
bulb.set_rgb(args.red, args.green, args.blue, args.fade_time) | |
elif args.command == 'hsb': | |
bulb.set_hsb(args.hue, args.saturation, args.brightness, args.fade_time) | |
elif args.command == 'temperature': | |
bulb.set_temperature(args.kelvin, args.fade_time) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment