#!/usr/bin/env python3 # # Copyright 2019, Mischa Peters , High5!. # Version 1.0 - 20191030 # # Control a light based on sensor information # Get ['dark'] from sensor ID and switch on/off light ID # depending where your sensor is located you can use ['daylight'] # # For example: # $ daylight-trigger.py -s 50 -l 24 # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ # # Requires: # - Python >3.6 # import argparse import ssl import urllib.request import json parser = argparse.ArgumentParser(description="Control light based on light sensor") parser.add_argument("bridge", type=str, help="Hue Bridge IP") parser.add_argument("token", type=str, help="Hue API Token") parser.add_argument("-s", "--sensor", type=int, required=True, help="sensor id#") parser.add_argument("-l", "--light", type=int, required=True, help="light id#") parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() bridge = args.bridge token = args.token sensor = args.sensor light = args.light action = args.action verbose = args.verbose debug = args.debug except argparse.ArgumentError as e: print(str(e)) no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE scenes = {'br': {}, 'ct': {}, 'xy': {}} scenes['br']['bright'] = b'{"on": true, "bri": 254, "alert": "none"}' scenes['br']['relax'] = b'{"on": true, "bri": 144, "alert": "none"}' scenes['br']['dimmed'] = b'{"on": true, "bri": 77, "alert": "none"}' scenes['br']['nightlight'] = b'{"on": true, "bri": 1, "alert": "none"}' scenes['ct']['bright'] = b'{"on": true, "bri": 254, "ct": 367, "alert": "none", "colormode": "ct"}' scenes['ct']['relax'] = b'{"on": true, "bri": 144, "ct": 447, "alert": "none", "colormode": "ct"}' scenes['ct']['dimmed'] = b'{"on": true, "bri": 77, "ct": 367, "alert": "none", "colormode": "ct"}' scenes['ct']['nightlight'] = b'{"on": true, "bri": 1, "ct": 447, "alert": "none", "colormode": "ct"}' 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"}' 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"}' 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"}' 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"}' def get_state(type, id): url = f"https://{bridge}/api/{token}/{type}/{id}" req = urllib.request.Request(url) with urllib.request.urlopen(req, context=no_cert_check) as response: content = response.read() json_data = json.loads(content) if debug: print (f"State for {type[:-1]} id {id}:\n{json_data['state']}") return (json_data['state']) def put_state(id, state, action): if not 'colormode' in state: if debug: print("state[colormode] not found, colormode set to: br") colormode = "br" else: if debug: print(f"state[colormode] found, colormode set to: {state['colormode']}") colormode = state['colormode'] if action == 'off': if verbose or debug: print(f"Light {action}!") data = b'{"on": false}' elif action == 'on': if verbose or debug: print(f"Light {action}!") data = b'{"on": true}' elif action in scenes[colormode]: if verbose or debug: print(f"Light {action}!") if debug: print(f"Light set to: {scenes[colormode][action]}") data = scenes[colormode][action] url = f"https://{bridge}/api/{token}/lights/{id}/state" req = urllib.request.Request(url=url, data=data, method='PUT') res = urllib.request.urlopen(req, context=no_cert_check) if debug: print (f"PUT response: {res.status} {res.reason}") if verbose or debug: print (f"{res.status} {res.reason}") return(res) sensor_state = get_state("sensors", sensor) light_state = get_state("lights", light) if debug: print (f"Dark: {sensor_state['dark']}, Daylight: {sensor_state['daylight']}, Light On: {sensor_state['daylight']}") if sensor_state['dark'] and not light_state['on']: put_state(light, light_state, action) if not sensor_state['dark'] and light_state['on']: put_state(light, light_state, "off")