1 cb73c075 2019-10-31 mischa #!/usr/bin/env python3
3 496ad2f3 2020-05-07 mischa # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 cb73c075 2019-10-31 mischa # Version 1.0 - 20191030
5 496ad2f3 2020-05-07 mischa # Version 1.1 - 20200507 - added config file support
7 3b390038 2019-11-03 mischa # Control a light based on sensor information
8 ccecd017 2019-11-01 mischa # Get ['dark'] from sensor ID and switch on/off light ID
9 cb73c075 2019-10-31 mischa # depending where your sensor is located you can use ['daylight']
11 ccecd017 2019-11-01 mischa # For example:
12 496ad2f3 2020-05-07 mischa # $ daylight-trigger.py <bridge name> -s 50 -l 24
14 496ad2f3 2020-05-07 mischa # Add the following to crontab:
15 496ad2f3 2020-05-07 mischa # */5 * * * * /<path-to-your-script>/daylight-trigger.py <bridge name> -s 50 -l 24 -a dimmed
17 9a811e47 2019-11-02 mischa # Follow the steps at the Hue Developer site to get the username/token
18 9a811e47 2019-11-02 mischa # https://developers.meethue.com/develop/get-started-2/
20 cb73c075 2019-10-31 mischa # Requires:
21 a1a54f01 2019-11-01 mischa # - Python >3.6
23 cb73c075 2019-10-31 mischa import argparse
24 cb73c075 2019-10-31 mischa import ssl
25 cb73c075 2019-10-31 mischa import urllib.request
26 cb73c075 2019-10-31 mischa import json
28 496ad2f3 2020-05-07 mischa import configparser
30 3b390038 2019-11-03 mischa parser = argparse.ArgumentParser(description="Control light based on light sensor")
31 496ad2f3 2020-05-07 mischa parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
32 cb73c075 2019-10-31 mischa parser.add_argument("-s", "--sensor", type=int, required=True, help="sensor id#")
33 cb73c075 2019-10-31 mischa parser.add_argument("-l", "--light", type=int, required=True, help="light id#")
34 780060e5 2019-11-03 mischa parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight")
35 cb73c075 2019-10-31 mischa parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
36 cb73c075 2019-10-31 mischa parser.add_argument("-d", "--debug", action='store_true', help="debug")
39 cb73c075 2019-10-31 mischa args = parser.parse_args()
40 496ad2f3 2020-05-07 mischa bridgename = args.bridgename
41 cb73c075 2019-10-31 mischa sensor = args.sensor
42 cb73c075 2019-10-31 mischa light = args.light
43 780060e5 2019-11-03 mischa action = args.action
44 cb73c075 2019-10-31 mischa verbose = args.verbose
45 cb73c075 2019-10-31 mischa debug = args.debug
47 cb73c075 2019-10-31 mischa except argparse.ArgumentError as e:
48 cb73c075 2019-10-31 mischa print(str(e))
50 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')]
51 496ad2f3 2020-05-07 mischa config = configparser.RawConfigParser()
52 496ad2f3 2020-05-07 mischa config.read(config_files)
53 496ad2f3 2020-05-07 mischa bridge = config.get(bridgename, 'ip')
54 496ad2f3 2020-05-07 mischa token = config.get(bridgename, 'token')
56 cb73c075 2019-10-31 mischa no_cert_check = ssl.create_default_context()
57 cb73c075 2019-10-31 mischa no_cert_check.check_hostname=False
58 cb73c075 2019-10-31 mischa no_cert_check.verify_mode=ssl.CERT_NONE
60 79874a80 2019-11-03 mischa scenes = {'br': {}, 'ct': {}, 'xy': {}}
61 780060e5 2019-11-03 mischa scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}'
62 780060e5 2019-11-03 mischa scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}'
63 780060e5 2019-11-03 mischa scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}'
64 780060e5 2019-11-03 mischa scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}'
65 780060e5 2019-11-03 mischa scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none", "colormode": "ct"}'
66 780060e5 2019-11-03 mischa scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none", "colormode": "ct"}'
67 780060e5 2019-11-03 mischa scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none", "colormode": "ct"}'
68 780060e5 2019-11-03 mischa scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none", "colormode": "ct"}'
69 79874a80 2019-11-03 mischa scenes['xy']['bright'] = b'{"on": true, "bri": 254, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
70 79874a80 2019-11-03 mischa scenes['xy']['relax'] = b'{"on": true, "bri": 144, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.5019, 0.4152], "ct": 447, "alert": "none", "colormode": "xy"}'
71 79874a80 2019-11-03 mischa scenes['xy']['dimmed'] = b'{"on": true, "bri": 77, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
72 79874a80 2019-11-03 mischa scenes['xy']['nightlight'] = b'{"on": true, "bri": 1, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.561, 0.4042], "ct": 367, "alert": "none", "colormode": "xy"}'
74 cb73c075 2019-10-31 mischa def get_state(type, id):
75 cb73c075 2019-10-31 mischa url = f"https://{bridge}/api/{token}/{type}/{id}"
76 cb73c075 2019-10-31 mischa req = urllib.request.Request(url)
77 cb73c075 2019-10-31 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
78 cb73c075 2019-10-31 mischa content = response.read()
79 cb73c075 2019-10-31 mischa json_data = json.loads(content)
80 cb73c075 2019-10-31 mischa if debug: print (f"State for {type[:-1]} id {id}:\n{json_data['state']}")
81 cb73c075 2019-10-31 mischa return (json_data['state'])
83 780060e5 2019-11-03 mischa def put_state(id, state, action):
84 780060e5 2019-11-03 mischa if not 'colormode' in state:
85 780060e5 2019-11-03 mischa if debug: print("state[colormode] not found, colormode set to: br")
86 780060e5 2019-11-03 mischa colormode = "br"
88 780060e5 2019-11-03 mischa if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}")
89 780060e5 2019-11-03 mischa colormode = state['colormode']
91 780060e5 2019-11-03 mischa if action == 'off':
92 780060e5 2019-11-03 mischa if verbose or debug: print(f"Light {action}!")
93 780060e5 2019-11-03 mischa data = b'{"on": false}'
94 780060e5 2019-11-03 mischa elif action == 'on':
95 780060e5 2019-11-03 mischa if verbose or debug: print(f"Light {action}!")
96 780060e5 2019-11-03 mischa data = b'{"on": true}'
97 780060e5 2019-11-03 mischa elif action in scenes[colormode]:
98 780060e5 2019-11-03 mischa if verbose or debug: print(f"Light {action}!")
99 780060e5 2019-11-03 mischa if debug: print(f"Light set to: {scenes[colormode][action]}")
100 780060e5 2019-11-03 mischa data = scenes[colormode][action]
102 780060e5 2019-11-03 mischa url = f"https://{bridge}/api/{token}/lights/{id}/state"
103 cb73c075 2019-10-31 mischa req = urllib.request.Request(url=url, data=data, method='PUT')
104 cb73c075 2019-10-31 mischa res = urllib.request.urlopen(req, context=no_cert_check)
105 cb73c075 2019-10-31 mischa if debug: print (f"PUT response: {res.status} {res.reason}")
106 cb73c075 2019-10-31 mischa if verbose or debug: print (f"{res.status} {res.reason}")
107 cb73c075 2019-10-31 mischa return(res)
109 cb73c075 2019-10-31 mischa sensor_state = get_state("sensors", sensor)
110 cb73c075 2019-10-31 mischa light_state = get_state("lights", light)
111 cb73c075 2019-10-31 mischa if debug: print (f"Dark: {sensor_state['dark']}, Daylight: {sensor_state['daylight']}, Light On: {sensor_state['daylight']}")
113 cb73c075 2019-10-31 mischa if sensor_state['dark'] and not light_state['on']:
114 780060e5 2019-11-03 mischa put_state(light, light_state, action)
115 cb73c075 2019-10-31 mischa if not sensor_state['dark'] and light_state['on']:
116 780060e5 2019-11-03 mischa put_state(light, light_state, "off")