Blame


1 023ec66d 2019-11-01 mischa #!/usr/bin/env python3
2 023ec66d 2019-11-01 mischa #
3 496ad2f3 2020-05-07 mischa # Copyright 2019-2020, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 023ec66d 2019-11-01 mischa # Version 1.0 - 20191028
5 875f2ac4 2019-11-03 mischa # Version 1.1 - 20191103 - added ['state']['on']
6 496ad2f3 2020-05-07 mischa # Version 1.2 - 20200507 - added config file support
7 023ec66d 2019-11-01 mischa #
8 875f2ac4 2019-11-03 mischa # Get all light ids and state
9 9a811e47 2019-11-02 mischa #
10 9a811e47 2019-11-02 mischa # For example:
11 496ad2f3 2020-05-07 mischa # $ get-lights.py <bridge name>
12 ccecd017 2019-11-01 mischa #
13 9a811e47 2019-11-02 mischa # Follow the steps at the Hue Developer site to get the username/token
14 9a811e47 2019-11-02 mischa # https://developers.meethue.com/develop/get-started-2/
15 9a811e47 2019-11-02 mischa #
16 023ec66d 2019-11-01 mischa # Requires:
17 a1a54f01 2019-11-01 mischa # - Python >3.6
18 023ec66d 2019-11-01 mischa #
19 023ec66d 2019-11-01 mischa import argparse
20 023ec66d 2019-11-01 mischa import ssl
21 023ec66d 2019-11-01 mischa import urllib.request
22 023ec66d 2019-11-01 mischa import json
23 496ad2f3 2020-05-07 mischa import os
24 496ad2f3 2020-05-07 mischa import configparser
25 023ec66d 2019-11-01 mischa
26 3b390038 2019-11-03 mischa parser = argparse.ArgumentParser(description="Get all light ids from Hue Bridge")
27 496ad2f3 2020-05-07 mischa parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
28 51565e99 2019-12-27 mischa parser.add_argument("-i", "--id", type=int, help="light id#")
29 023ec66d 2019-11-01 mischa
30 023ec66d 2019-11-01 mischa try:
31 023ec66d 2019-11-01 mischa args = parser.parse_args()
32 496ad2f3 2020-05-07 mischa bridgename = args.bridgename
33 51565e99 2019-12-27 mischa id = args.id
34 023ec66d 2019-11-01 mischa
35 023ec66d 2019-11-01 mischa except argparse.ArgumentError as e:
36 023ec66d 2019-11-01 mischa print(str(e))
37 023ec66d 2019-11-01 mischa
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')
43 496ad2f3 2020-05-07 mischa
44 023ec66d 2019-11-01 mischa no_cert_check = ssl.create_default_context()
45 023ec66d 2019-11-01 mischa no_cert_check.check_hostname=False
46 023ec66d 2019-11-01 mischa no_cert_check.verify_mode=ssl.CERT_NONE
47 023ec66d 2019-11-01 mischa
48 023ec66d 2019-11-01 mischa url = f"https://{bridge}/api/{token}/lights"
49 023ec66d 2019-11-01 mischa req = urllib.request.Request(url)
50 023ec66d 2019-11-01 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
51 023ec66d 2019-11-01 mischa content = response.read()
52 023ec66d 2019-11-01 mischa json_data = json.loads(content)
53 023ec66d 2019-11-01 mischa
54 51565e99 2019-12-27 mischa if not id:
55 51565e99 2019-12-27 mischa print(f"{'ID':>3s} {'Name':<32s} {'State':<5s} Type")
56 51565e99 2019-12-27 mischa print ("################################################################################")
57 51565e99 2019-12-27 mischa for key in json_data:
58 51565e99 2019-12-27 mischa if not json_data[key]['state']['reachable']:
59 51565e99 2019-12-27 mischa continue
60 51565e99 2019-12-27 mischa state = 'on' if json_data[key]['state']['on'] else 'off'
61 51565e99 2019-12-27 mischa print(f"{key:>3s}: {json_data[key]['name']:<32s} {state:<5s} {json_data[key]['type']}")
62 51565e99 2019-12-27 mischa else:
63 51565e99 2019-12-27 mischa if json_data[str(id)]['state']['reachable']:
64 51565e99 2019-12-27 mischa state = 'on' if json_data[str(id)]['state']['on'] else 'off'
65 51565e99 2019-12-27 mischa print(state)
66 51565e99 2019-12-27 mischa else:
67 51565e99 2019-12-27 mischa print("unreachable")