Blob


1 #!/usr/bin/env python3
2 #
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
6 #
7 # Control a group of lights (room)
8 #
9 # For example:
10 # $ groupctl.py <bridge name> -g 4 -a on
11 #
12 # Follow the steps at the Hue Developer site to get the username/token
13 # https://developers.meethue.com/develop/get-started-2/
14 #
15 # Requires:
16 # - Python >3.6
17 #
18 import argparse
19 import ssl
20 import urllib.request
21 import json
22 import os
23 import configparser
25 parser = argparse.ArgumentParser(description="Control group of lights (room)")
26 parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
27 parser.add_argument("-g", "--group", type=int, required=True, help="group id#")
28 parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight")
29 parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
30 parser.add_argument("-d", "--debug", action='store_true', help="debug")
32 try:
33 args = parser.parse_args()
34 bridgename = args.bridgename
35 group = args.group
36 action = args.action
37 verbose = args.verbose
38 debug = args.debug
40 except argparse.ArgumentError as e:
41 print(str(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': {}, 'xt': {}}
54 scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}'
55 scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}'
56 scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}'
57 scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}'
58 scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none", "colormode": "ct"}'
59 scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none", "colormode": "ct"}'
60 scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none", "colormode": "ct"}'
61 scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none", "colormode": "ct"}'
62 scenes['xt']['bright'] = b'{"on": true, "bri": 254, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
63 scenes['xt']['relax'] = b'{"on": true, "bri": 144, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none", "colormode": "xy"}'
64 scenes['xt']['dimmed'] = b'{"on": true, "bri": 77, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
65 scenes['xt']['nightlight'] = b'{"on": true, "bri": 1, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.561, 0.4042], "ct": 367, "alert": "none", "colormode": "xy"}'
67 def get_state(id):
68 url = f"https://{bridge}/api/{token}/groups/{id}"
69 req = urllib.request.Request(url)
70 with urllib.request.urlopen(req, context=no_cert_check) as response:
71 content = response.read()
72 json_data = json.loads(content)
73 if debug: print (f"State for group id {id}:\n{json_data['state']}")
74 return (json_data['state'])
76 def put_state(id, state):
77 if not 'colormode' in state:
78 if debug: print("state[colormode] not found, colormode set to: br")
79 colormode = "br"
80 else:
81 if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}")
82 colormode = state['colormode']
84 if action == 'off':
85 if verbose or debug: print("Light {action}!")
86 data = b'{"on": false}'
87 elif action == 'on':
88 if verbose or debug: print("Light {action}!")
89 data = b'{"on": true}'
90 elif action in scenes[colormode]:
91 if verbose or debug: print("Light {action}!")
92 if debug: print(f"Light set to: {scenes[colormode][action]}")
93 data = scenes[colormode][action]
95 url = f"https://{bridge}/api/{token}/groups/{id}/action"
96 req = urllib.request.Request(url=url, data=data, method='PUT')
97 res = urllib.request.urlopen(req, context=no_cert_check)
98 if debug: print (f"PUT response: {res.status} {res.reason}")
99 if verbose or debug: print (f"{res.status} {res.reason}")
100 return(res)
102 group_state = get_state(group)
103 put_state(group, group_state)