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