#!/usr/bin/env python3 # # Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191102 # Version 1.1 - 20200507 - added config file support # # Control a light or plug # # For example: # $ lightctl.py -l 24 -a relax # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ # # Requires: # - Python >3.6 # import argparse import ssl import urllib.request import json import os import configparser parser = argparse.ArgumentParser(description="Control light") parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-l", "--light", type=int, required=True, help="light id#") parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight|state") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() bridgename = args.bridgename light = args.light action = args.action verbose = args.verbose debug = args.debug except argparse.ArgumentError as e: print(str(e)) config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] config = configparser.RawConfigParser() config.read(config_files) bridge = config.get(bridgename, 'ip') token = config.get(bridgename, 'token') no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE scenes = {'br': {}, 'ct': {}, 'xy': {}} scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}' scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none"}' scenes['xy']['bright'] = b'{"on": true, "bri": 254, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none"}' scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}' scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none"}' scenes['xy']['relax'] = b'{"on": true, "bri": 144, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}' scenes['br']['morning'] = b'{"on": true, "bri": 100, "alert": "none"}' scenes['ct']['morning'] = b'{"on": true, "bri": 100, "ct": 447, "alert": "none"}' scenes['xy']['morning'] = b'{"on": true, "bri": 100, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}' scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}' scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none"}' scenes['xy']['dimmed'] = b'{"on": true, "bri": 77, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none"}' scenes['br']['evening'] = b'{"on": true, "bri": 63, "alert": "none"}' scenes['ct']['evening'] = b'{"on": true, "bri": 63, "ct": 447, "alert": "none"}' scenes['xy']['evening'] = b'{"on": true, "bri": 63, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}' scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}' scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none"}' scenes['xy']['nightlight'] = b'{"on": true, "bri": 1, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.561, 0.4042], "ct": 367, "alert": "none"}' def get_state(id): url = f"https://{bridge}/api/{token}/lights/{id}" req = urllib.request.Request(url) with urllib.request.urlopen(req, context=no_cert_check) as response: content = response.read() json_data = json.loads(content) if debug: print (f"State for light id {id}:\n{json_data['state']}") if debug: print (f"Type for light id {id}: {json_data['config']['archetype']}") if 'state' in json_data: return (json_data['state']) else: print(f"id {id} doesn't exist") return -1 def put_state(id, state): if not 'colormode' in state: if debug: print("state[colormode] not found, colormode set to: br") colormode = "br" else: if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}") colormode = state['colormode'] if action == 'off': if verbose or debug: print(f"Light {action}!") data = b'{"on": false}' elif action == 'on': if verbose or debug: print(f"Light {action}!") data = b'{"on": true}' elif action in scenes[colormode]: if verbose or debug: print(f"Light {action}!") if debug: print(f"Light set to: {scenes[colormode][action]}") data = scenes[colormode][action] url = f"https://{bridge}/api/{token}/lights/{id}/state" req = urllib.request.Request(url=url, data=data, method='PUT') res = urllib.request.urlopen(req, context=no_cert_check) if debug: print (f"PUT response: {res.status} {res.reason}") if verbose or debug: print (f"{res.status} {res.reason}") return(res) light_state = get_state(light) if not action == 'state': put_state(light, light_state) else: if light_state['reachable']: state = 'on' if light_state['on'] else 'off' print(state) else: print("unreachable")