add lightctl.py script to control invidual lights or plug
This commit is contained in:
parent
9a811e476b
commit
1001f21734
76
lightctl.py
Executable file
76
lightctl.py
Executable file
@ -0,0 +1,76 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# Copyright 2019, Mischa Peters <mischa AT high5 DOT nl>, High5!.
|
||||||
|
# Version 1.0 - 20191102
|
||||||
|
#
|
||||||
|
# Control a light or plug
|
||||||
|
#
|
||||||
|
# For example:
|
||||||
|
# $ lightctl.py <bridge IP> <token> -l 24 -a on
|
||||||
|
#
|
||||||
|
# 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="Turn light on")
|
||||||
|
parser.add_argument("bridge", type=str, help="Hue Bridge IP")
|
||||||
|
parser.add_argument("token", type=str, help="Hue API Token")
|
||||||
|
parser.add_argument("-l", "--light", type=int, required=True, help="light id#")
|
||||||
|
parser.add_argument("-a", "--action", type=str, default='on', help="on|pff")
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
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']}")
|
||||||
|
if debug: print (f"Type for {type[:-1]} id {id}: {json_data['config']['archetype']}")
|
||||||
|
return (json_data['state'])
|
||||||
|
|
||||||
|
def put_state(type, id, action):
|
||||||
|
if action == "on":
|
||||||
|
data = b'{"on": true, "bri": 77, "hue": 8402, "sat": 254, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
|
||||||
|
if action == "off":
|
||||||
|
data = b'{"on":false}'
|
||||||
|
|
||||||
|
url = f"https://{bridge}/api/{token}/{type}/{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)
|
||||||
|
|
||||||
|
light_state = get_state("lights", light)
|
||||||
|
|
||||||
|
if action == 'on' and not light_state['on']:
|
||||||
|
if verbose or debug: print ("Lights ON!")
|
||||||
|
put_state("lights", light, "on")
|
||||||
|
if action == 'off' and light_state['on']:
|
||||||
|
if verbose or debug: print ("Light OFF!")
|
||||||
|
put_state("lights", light, "off")
|
Loading…
Reference in New Issue
Block a user