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