1 5f20079a 2019-11-03 mischa #!/usr/bin/env python3
3 496ad2f3 2020-05-07 mischa # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 5f20079a 2019-11-03 mischa # Version 1.0 - 20191103
5 496ad2f3 2020-05-07 mischa # Version 1.1 - 20200507 - added config file support
7 5f20079a 2019-11-03 mischa # Collect all information of given id
9 5f20079a 2019-11-03 mischa # For example:
10 496ad2f3 2020-05-07 mischa # $ get-id.py <bridge name> -t sensors -i 6
12 5f20079a 2019-11-03 mischa # Requires:
13 5f20079a 2019-11-03 mischa # - Python 3.x
15 5f20079a 2019-11-03 mischa import argparse
16 5f20079a 2019-11-03 mischa import ssl
17 5f20079a 2019-11-03 mischa import urllib.request
18 5f20079a 2019-11-03 mischa import json
20 496ad2f3 2020-05-07 mischa import configparser
22 5f20079a 2019-11-03 mischa parser = argparse.ArgumentParser(description="Get id information")
23 496ad2f3 2020-05-07 mischa parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
24 5f20079a 2019-11-03 mischa parser.add_argument("-i", "--id", type=int, default='1', help="id#")
25 e79b0f50 2019-11-06 mischa parser.add_argument("-t", "--type", type=str, default='lights', help="lights|sensors|groups|rules")
26 5f20079a 2019-11-03 mischa parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
29 5f20079a 2019-11-03 mischa args = parser.parse_args()
30 496ad2f3 2020-05-07 mischa bridgename = args.bridgename
31 5f20079a 2019-11-03 mischa id = args.id
32 5f20079a 2019-11-03 mischa type = args.type
33 5f20079a 2019-11-03 mischa verbose = args.verbose
35 5f20079a 2019-11-03 mischa except argparse.ArgumentError as e:
36 5f20079a 2019-11-03 mischa print(str(e))
38 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')]
39 496ad2f3 2020-05-07 mischa config = configparser.RawConfigParser()
40 496ad2f3 2020-05-07 mischa config.read(config_files)
41 496ad2f3 2020-05-07 mischa bridge = config.get(bridgename, 'ip')
42 496ad2f3 2020-05-07 mischa token = config.get(bridgename, 'token')
44 5f20079a 2019-11-03 mischa no_cert_check = ssl.create_default_context()
45 5f20079a 2019-11-03 mischa no_cert_check.check_hostname=False
46 5f20079a 2019-11-03 mischa no_cert_check.verify_mode=ssl.CERT_NONE
48 5f20079a 2019-11-03 mischa url = f"https://{bridge}/api/{token}/{type}/{id}"
49 5f20079a 2019-11-03 mischa req = urllib.request.Request(url)
50 5f20079a 2019-11-03 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
51 5f20079a 2019-11-03 mischa content = response.read()
52 5f20079a 2019-11-03 mischa json_data = json.loads(content)
54 5f20079a 2019-11-03 mischa if not 'state' in json_data:
55 5f20079a 2019-11-03 mischa print(f"{type[:-1]} id {id} doesn't exist")
57 5f20079a 2019-11-03 mischa if verbose: print("json dump:")
58 5f20079a 2019-11-03 mischa if verbose: print(json.dumps(json_data, indent=4, sort_keys=True))
59 5f20079a 2019-11-03 mischa if verbose: print("state")
60 5f20079a 2019-11-03 mischa print(json_data['state'])