Blame


1 b53fcdf3 2019-11-03 mischa #!/usr/bin/env python3
2 b53fcdf3 2019-11-03 mischa #
3 496ad2f3 2020-05-07 mischa # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 b53fcdf3 2019-11-03 mischa # Version 1.0 - 20191102
5 496ad2f3 2020-05-07 mischa # Version 1.1 - 20200507 - added config file support
6 b53fcdf3 2019-11-03 mischa #
7 3b390038 2019-11-03 mischa # Control a group of lights (room)
8 b53fcdf3 2019-11-03 mischa #
9 b53fcdf3 2019-11-03 mischa # For example:
10 496ad2f3 2020-05-07 mischa # $ groupctl.py <bridge name> -g 4 -a on
11 b53fcdf3 2019-11-03 mischa #
12 b53fcdf3 2019-11-03 mischa # Follow the steps at the Hue Developer site to get the username/token
13 b53fcdf3 2019-11-03 mischa # https://developers.meethue.com/develop/get-started-2/
14 b53fcdf3 2019-11-03 mischa #
15 b53fcdf3 2019-11-03 mischa # Requires:
16 b53fcdf3 2019-11-03 mischa # - Python >3.6
17 b53fcdf3 2019-11-03 mischa #
18 b53fcdf3 2019-11-03 mischa import argparse
19 b53fcdf3 2019-11-03 mischa import ssl
20 b53fcdf3 2019-11-03 mischa import urllib.request
21 b53fcdf3 2019-11-03 mischa import json
22 496ad2f3 2020-05-07 mischa import os
23 496ad2f3 2020-05-07 mischa import configparser
24 b53fcdf3 2019-11-03 mischa
25 3b390038 2019-11-03 mischa parser = argparse.ArgumentParser(description="Control group of lights (room)")
26 496ad2f3 2020-05-07 mischa parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
27 3b390038 2019-11-03 mischa parser.add_argument("-g", "--group", type=int, required=True, help="group id#")
28 b53fcdf3 2019-11-03 mischa parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight")
29 b53fcdf3 2019-11-03 mischa parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
30 b53fcdf3 2019-11-03 mischa parser.add_argument("-d", "--debug", action='store_true', help="debug")
31 b53fcdf3 2019-11-03 mischa
32 b53fcdf3 2019-11-03 mischa try:
33 b53fcdf3 2019-11-03 mischa args = parser.parse_args()
34 496ad2f3 2020-05-07 mischa bridgename = args.bridgename
35 3b390038 2019-11-03 mischa group = args.group
36 b53fcdf3 2019-11-03 mischa action = args.action
37 b53fcdf3 2019-11-03 mischa verbose = args.verbose
38 b53fcdf3 2019-11-03 mischa debug = args.debug
39 b53fcdf3 2019-11-03 mischa
40 b53fcdf3 2019-11-03 mischa except argparse.ArgumentError as e:
41 b53fcdf3 2019-11-03 mischa print(str(e))
42 b53fcdf3 2019-11-03 mischa
43 496ad2f3 2020-05-07 mischa config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')]
44 496ad2f3 2020-05-07 mischa config = configparser.RawConfigParser()
45 496ad2f3 2020-05-07 mischa config.read(config_files)
46 496ad2f3 2020-05-07 mischa bridge = config.get(bridgename, 'ip')
47 496ad2f3 2020-05-07 mischa token = config.get(bridgename, 'token')
48 496ad2f3 2020-05-07 mischa
49 b53fcdf3 2019-11-03 mischa no_cert_check = ssl.create_default_context()
50 b53fcdf3 2019-11-03 mischa no_cert_check.check_hostname=False
51 b53fcdf3 2019-11-03 mischa no_cert_check.verify_mode=ssl.CERT_NONE
52 b53fcdf3 2019-11-03 mischa
53 b53fcdf3 2019-11-03 mischa scenes = {'br': {}, 'ct': {}, 'xt': {}}
54 b53fcdf3 2019-11-03 mischa scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}'
55 b53fcdf3 2019-11-03 mischa scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}'
56 b53fcdf3 2019-11-03 mischa scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}'
57 b53fcdf3 2019-11-03 mischa scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}'
58 b53fcdf3 2019-11-03 mischa scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none", "colormode": "ct"}'
59 b53fcdf3 2019-11-03 mischa scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none", "colormode": "ct"}'
60 b53fcdf3 2019-11-03 mischa scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none", "colormode": "ct"}'
61 b53fcdf3 2019-11-03 mischa scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none", "colormode": "ct"}'
62 b53fcdf3 2019-11-03 mischa 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 b53fcdf3 2019-11-03 mischa 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 b53fcdf3 2019-11-03 mischa 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 b53fcdf3 2019-11-03 mischa 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"}'
66 b53fcdf3 2019-11-03 mischa
67 b53fcdf3 2019-11-03 mischa def get_state(id):
68 b53fcdf3 2019-11-03 mischa url = f"https://{bridge}/api/{token}/groups/{id}"
69 b53fcdf3 2019-11-03 mischa req = urllib.request.Request(url)
70 b53fcdf3 2019-11-03 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
71 b53fcdf3 2019-11-03 mischa content = response.read()
72 b53fcdf3 2019-11-03 mischa json_data = json.loads(content)
73 b53fcdf3 2019-11-03 mischa if debug: print (f"State for group id {id}:\n{json_data['state']}")
74 b53fcdf3 2019-11-03 mischa return (json_data['state'])
75 b53fcdf3 2019-11-03 mischa
76 b53fcdf3 2019-11-03 mischa def put_state(id, state):
77 b53fcdf3 2019-11-03 mischa if not 'colormode' in state:
78 b53fcdf3 2019-11-03 mischa if debug: print("state[colormode] not found, colormode set to: br")
79 b53fcdf3 2019-11-03 mischa colormode = "br"
80 b53fcdf3 2019-11-03 mischa else:
81 b53fcdf3 2019-11-03 mischa if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}")
82 b53fcdf3 2019-11-03 mischa colormode = state['colormode']
83 b53fcdf3 2019-11-03 mischa
84 b53fcdf3 2019-11-03 mischa if action == 'off':
85 b53fcdf3 2019-11-03 mischa if verbose or debug: print("Light {action}!")
86 b53fcdf3 2019-11-03 mischa data = b'{"on": false}'
87 b53fcdf3 2019-11-03 mischa elif action == 'on':
88 b53fcdf3 2019-11-03 mischa if verbose or debug: print("Light {action}!")
89 b53fcdf3 2019-11-03 mischa data = b'{"on": true}'
90 b53fcdf3 2019-11-03 mischa elif action in scenes[colormode]:
91 b53fcdf3 2019-11-03 mischa if verbose or debug: print("Light {action}!")
92 b53fcdf3 2019-11-03 mischa if debug: print(f"Light set to: {scenes[colormode][action]}")
93 b53fcdf3 2019-11-03 mischa data = scenes[colormode][action]
94 b53fcdf3 2019-11-03 mischa
95 b53fcdf3 2019-11-03 mischa url = f"https://{bridge}/api/{token}/groups/{id}/action"
96 b53fcdf3 2019-11-03 mischa req = urllib.request.Request(url=url, data=data, method='PUT')
97 b53fcdf3 2019-11-03 mischa res = urllib.request.urlopen(req, context=no_cert_check)
98 b53fcdf3 2019-11-03 mischa if debug: print (f"PUT response: {res.status} {res.reason}")
99 b53fcdf3 2019-11-03 mischa if verbose or debug: print (f"{res.status} {res.reason}")
100 b53fcdf3 2019-11-03 mischa return(res)
101 b53fcdf3 2019-11-03 mischa
102 3b390038 2019-11-03 mischa group_state = get_state(group)
103 3b390038 2019-11-03 mischa put_state(group, group_state)