Blob


1 #!/usr/bin/env python3
2 #
3 # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 # Version 1.0 - 20191028
5 # Version 1.1 - 20200507 - added config file support
6 #
7 # Get all light IDs
8 #
9 # For example:
10 # $ get-lights.py <bridge name>
11 #
12 # Follow the steps at the Hue Developer site to get the username/token
13 # https://developers.meethue.com/develop/get-started-2/
14 #
15 # Requires:
16 # - Python >3.6
17 #
18 import argparse
19 import ssl
20 import urllib.request
21 import json
22 import os
23 import configparser
25 parser = argparse.ArgumentParser(description="Get all group ids from Hue Bridge")
26 parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
28 try:
29 args = parser.parse_args()
30 bridgename = args.bridgename
32 except argparse.ArgumentError as e:
33 print(str(e))
35 config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')]
36 config = configparser.RawConfigParser()
37 config.read(config_files)
38 bridge = config.get(bridgename, 'ip')
39 token = config.get(bridgename, 'token')
41 no_cert_check = ssl.create_default_context()
42 no_cert_check.check_hostname=False
43 no_cert_check.verify_mode=ssl.CERT_NONE
45 url = f"https://{bridge}/api/{token}/groups"
46 req = urllib.request.Request(url)
47 with urllib.request.urlopen(req, context=no_cert_check) as response:
48 content = response.read()
49 json_data = json.loads(content)
51 print(f"{'ID':>2s}: {'Name':<30s} Type")
52 print ("################################################################################")
53 for key in json_data:
54 print(f"{key:>2s}: {json_data[key]['name']:<30s} {json_data[key]['type']}")