3 # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 # Version 1.0 - 20191102
5 # Version 1.1 - 20200507 - added config file support
7 # Control a light or plug
10 # $ lightctl.py <bridge name> -l 24 -a relax
12 # Follow the steps at the Hue Developer site to get the username/token
13 # https://developers.meethue.com/develop/get-started-2/
25 parser = argparse.ArgumentParser(description="Control light")
26 parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
27 parser.add_argument("-l", "--light", type=int, required=True, help="light id#")
28 parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight|state")
29 parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
30 parser.add_argument("-d", "--debug", action='store_true', help="debug")
33 args = parser.parse_args()
34 bridgename = args.bridgename
37 verbose = args.verbose
40 except argparse.ArgumentError as e:
43 config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')]
44 config = configparser.RawConfigParser()
45 config.read(config_files)
46 bridge = config.get(bridgename, 'ip')
47 token = config.get(bridgename, 'token')
49 no_cert_check = ssl.create_default_context()
50 no_cert_check.check_hostname=False
51 no_cert_check.verify_mode=ssl.CERT_NONE
53 scenes = {'br': {}, 'ct': {}, 'xy': {}}
54 scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}'
55 scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none"}'
56 scenes['xy']['bright'] = b'{"on": true, "bri": 254, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none"}'
57 scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}'
58 scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none"}'
59 scenes['xy']['relax'] = b'{"on": true, "bri": 144, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}'
60 scenes['br']['morning'] = b'{"on": true, "bri": 100, "alert": "none"}'
61 scenes['ct']['morning'] = b'{"on": true, "bri": 100, "ct": 447, "alert": "none"}'
62 scenes['xy']['morning'] = b'{"on": true, "bri": 100, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}'
63 scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}'
64 scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none"}'
65 scenes['xy']['dimmed'] = b'{"on": true, "bri": 77, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none"}'
66 scenes['br']['evening'] = b'{"on": true, "bri": 63, "alert": "none"}'
67 scenes['ct']['evening'] = b'{"on": true, "bri": 63, "ct": 447, "alert": "none"}'
68 scenes['xy']['evening'] = b'{"on": true, "bri": 63, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none"}'
69 scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}'
70 scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none"}'
71 scenes['xy']['nightlight'] = b'{"on": true, "bri": 1, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.561, 0.4042], "ct": 367, "alert": "none"}'
74 url = f"https://{bridge}/api/{token}/lights/{id}"
75 req = urllib.request.Request(url)
76 with urllib.request.urlopen(req, context=no_cert_check) as response:
77 content = response.read()
78 json_data = json.loads(content)
79 if debug: print (f"State for light id {id}:\n{json_data['state']}")
80 if debug: print (f"Type for light id {id}: {json_data['config']['archetype']}")
81 if 'state' in json_data:
82 return (json_data['state'])
84 print(f"id {id} doesn't exist")
87 def put_state(id, state):
88 if not 'colormode' in state:
89 if debug: print("state[colormode] not found, colormode set to: br")
92 if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}")
93 colormode = state['colormode']
96 if verbose or debug: print(f"Light {action}!")
97 data = b'{"on": false}'
99 if verbose or debug: print(f"Light {action}!")
100 data = b'{"on": true}'
101 elif action in scenes[colormode]:
102 if verbose or debug: print(f"Light {action}!")
103 if debug: print(f"Light set to: {scenes[colormode][action]}")
104 data = scenes[colormode][action]
106 url = f"https://{bridge}/api/{token}/lights/{id}/state"
107 req = urllib.request.Request(url=url, data=data, method='PUT')
108 res = urllib.request.urlopen(req, context=no_cert_check)
109 if debug: print (f"PUT response: {res.status} {res.reason}")
110 if verbose or debug: print (f"{res.status} {res.reason}")
113 light_state = get_state(light)
114 if not action == 'state':
115 put_state(light, light_state)
117 if light_state['reachable']:
118 state = 'on' if light_state['on'] else 'off'