104 lines
4.3 KiB
Python
Executable File
104 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
#
|
|
# Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
|
|
# Version 1.0 - 20191102
|
|
# Version 1.1 - 20200507 - added config file support
|
|
#
|
|
# Control a group of lights (room)
|
|
#
|
|
# For example:
|
|
# $ groupctl.py <bridge name> -g 4 -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
|
|
import os
|
|
import configparser
|
|
|
|
parser = argparse.ArgumentParser(description="Control group of lights (room)")
|
|
parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
|
|
parser.add_argument("-g", "--group", type=int, required=True, help="group 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()
|
|
bridgename = args.bridgename
|
|
group = args.group
|
|
action = args.action
|
|
verbose = args.verbose
|
|
debug = args.debug
|
|
|
|
except argparse.ArgumentError as e:
|
|
print(str(e))
|
|
|
|
config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')]
|
|
config = configparser.RawConfigParser()
|
|
config.read(config_files)
|
|
bridge = config.get(bridgename, 'ip')
|
|
token = config.get(bridgename, 'token')
|
|
|
|
no_cert_check = ssl.create_default_context()
|
|
no_cert_check.check_hostname=False
|
|
no_cert_check.verify_mode=ssl.CERT_NONE
|
|
|
|
scenes = {'br': {}, 'ct': {}, 'xt': {}}
|
|
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['xt']['bright'] = b'{"on": true, "bri": 254, "hue": 8402, "sat": 140, "effect": "none", "xy": [0.4578, 0.41], "ct": 367, "alert": "none", "colormode": "xy"}'
|
|
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"}'
|
|
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"}'
|
|
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"}'
|
|
|
|
def get_state(id):
|
|
url = f"https://{bridge}/api/{token}/groups/{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 group id {id}:\n{json_data['state']}")
|
|
return (json_data['state'])
|
|
|
|
def put_state(id, state):
|
|
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("Light {action}!")
|
|
data = b'{"on": false}'
|
|
elif action == 'on':
|
|
if verbose or debug: print("Light {action}!")
|
|
data = b'{"on": true}'
|
|
elif action in scenes[colormode]:
|
|
if verbose or debug: print("Light {action}!")
|
|
if debug: print(f"Light set to: {scenes[colormode][action]}")
|
|
data = scenes[colormode][action]
|
|
|
|
url = f"https://{bridge}/api/{token}/groups/{id}/action"
|
|
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)
|
|
|
|
group_state = get_state(group)
|
|
put_state(group, group_state)
|