hue/get-groups.py

55 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
# Version 1.0 - 20191028
# Version 1.1 - 20200507 - added config file support
#
# Get all light IDs
#
# For example:
# $ get-lights.py <bridge name>
#
# 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="Get all group ids from Hue Bridge")
parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
try:
args = parser.parse_args()
bridgename = args.bridgename
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
url = f"https://{bridge}/api/{token}/groups"
req = urllib.request.Request(url)
with urllib.request.urlopen(req, context=no_cert_check) as response:
content = response.read()
json_data = json.loads(content)
print(f"{'ID':>2s}: {'Name':<30s} Type")
print ("################################################################################")
for key in json_data:
print(f"{key:>2s}: {json_data[key]['name']:<30s} {json_data[key]['type']}")