Blame


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