1 023ec66d 2019-11-01 mischa #!/usr/bin/env python3
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 eb78856c 2019-11-06 mischa # Version 1.1 - 20191106 - added battery status
6 496ad2f3 2020-05-07 mischa # Version 1.2 - 20200507 - added config file support
7 17c6e3a6 2020-05-09 mischa # Version 1.3 - 20200509 - added dimmer switch
9 17c6e3a6 2020-05-09 mischa # Get all sensor IDs ZZLSwitch, ZLLPresence (+ ZLLLightLevel and ZLLTemperature)
10 17c6e3a6 2020-05-09 mischa # grouped by ZZLSwitch and ZLLPresence name
12 9a811e47 2019-11-02 mischa # For example:
13 496ad2f3 2020-05-07 mischa # $ get-sensors.py <bridge name>
15 9a811e47 2019-11-02 mischa # Follow the steps at the Hue Developer site to get the username/token
16 9a811e47 2019-11-02 mischa # https://developers.meethue.com/develop/get-started-2/
18 023ec66d 2019-11-01 mischa # Requires:
19 a1a54f01 2019-11-01 mischa # - Python >3.6
21 023ec66d 2019-11-01 mischa import argparse
22 023ec66d 2019-11-01 mischa import ssl
23 023ec66d 2019-11-01 mischa import urllib.request
24 023ec66d 2019-11-01 mischa import json
26 023ec66d 2019-11-01 mischa import collections
28 496ad2f3 2020-05-07 mischa import configparser
30 3b390038 2019-11-03 mischa parser = argparse.ArgumentParser(description="Get all sensor ids from Hue Bridge")
31 496ad2f3 2020-05-07 mischa parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf")
32 1dd8b4e2 2019-11-06 mischa parser.add_argument("-b", "--battery", type=int, help="battery check only, threshold, default 20")
33 1dd8b4e2 2019-11-06 mischa parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
36 023ec66d 2019-11-01 mischa args = parser.parse_args()
37 496ad2f3 2020-05-07 mischa bridgename = args.bridgename
38 1dd8b4e2 2019-11-06 mischa battery = args.battery
39 1dd8b4e2 2019-11-06 mischa verbose = args.verbose
41 023ec66d 2019-11-01 mischa except argparse.ArgumentError as e:
42 023ec66d 2019-11-01 mischa print(str(e))
44 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')]
45 496ad2f3 2020-05-07 mischa config = configparser.RawConfigParser()
46 496ad2f3 2020-05-07 mischa config.read(config_files)
47 496ad2f3 2020-05-07 mischa bridge = config.get(bridgename, 'ip')
48 496ad2f3 2020-05-07 mischa token = config.get(bridgename, 'token')
50 023ec66d 2019-11-01 mischa no_cert_check = ssl.create_default_context()
51 023ec66d 2019-11-01 mischa no_cert_check.check_hostname=False
52 023ec66d 2019-11-01 mischa no_cert_check.verify_mode=ssl.CERT_NONE
54 023ec66d 2019-11-01 mischa url = f"https://{bridge}/api/{token}/sensors"
55 023ec66d 2019-11-01 mischa req = urllib.request.Request(url)
56 023ec66d 2019-11-01 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
57 023ec66d 2019-11-01 mischa content = response.read()
58 023ec66d 2019-11-01 mischa json_data = json.loads(content)
60 023ec66d 2019-11-01 mischa p = re.compile("([a-fA-F0-9]{2}:?){8}")
61 023ec66d 2019-11-01 mischa sensors = collections.defaultdict(list);
63 023ec66d 2019-11-01 mischa for key in json_data:
64 7378e680 2019-11-06 mischa if "uniqueid" in json_data[key]:
65 023ec66d 2019-11-01 mischa if p.search(json_data[key]['uniqueid']):
66 023ec66d 2019-11-01 mischa if json_data[key]['type'] == 'ZLLPresence':
67 023ec66d 2019-11-01 mischa sensors[json_data[key]['uniqueid'][:-8]].insert(0, key)
68 4348738b 2019-11-06 mischa elif json_data[key]['type'] == 'ZGPSwitch':
69 4348738b 2019-11-06 mischa # Don't add ZGPSwitch, doesn't have a battery component
72 023ec66d 2019-11-01 mischa sensors[json_data[key]['uniqueid'][:-8]].append(key)
74 023ec66d 2019-11-01 mischa for key in sensors:
75 023ec66d 2019-11-01 mischa for i in sensors[key]:
76 1dd8b4e2 2019-11-06 mischa if not battery:
77 17c6e3a6 2020-05-09 mischa if json_data.get(i)['type'] == 'ZLLSwitch':
78 17c6e3a6 2020-05-09 mischa print(f"{json_data.get(i)['name']:<32s} ({json_data.get(i)['config']['battery']}%)")
79 17c6e3a6 2020-05-09 mischa print(f"{i:>5s}: {json_data.get(i)['productname']}")
80 1dd8b4e2 2019-11-06 mischa if json_data.get(i)['type'] == 'ZLLPresence':
81 1dd8b4e2 2019-11-06 mischa print(f"{json_data.get(i)['name']:<32s} ({json_data.get(i)['config']['battery']}%)")
82 1dd8b4e2 2019-11-06 mischa print(f"{i:>5s}: {json_data.get(i)['productname']}")
83 1dd8b4e2 2019-11-06 mischa if json_data.get(i)['type'] == 'ZLLLightLevel':
84 1dd8b4e2 2019-11-06 mischa print(f"{i:>5s}: {json_data.get(i)['productname']}")
85 1dd8b4e2 2019-11-06 mischa if json_data.get(i)['type'] == 'ZLLTemperature':
86 1dd8b4e2 2019-11-06 mischa print(f"{i:>5s}: {json_data.get(i)['productname']}")
88 5883ed95 2019-11-06 mischa if not "battery" in json_data.get(i)['config']:
89 4348738b 2019-11-06 mischa print(f"Sensor without battery property found: {i}")
90 5883ed95 2019-11-06 mischa print(json.dumps(json_data.get(i), indent=4, sort_keys=True))
92 1dd8b4e2 2019-11-06 mischa if int(json_data.get(i)['config']['battery']) < battery:
93 1dd8b4e2 2019-11-06 mischa if json_data.get(i)['type'] == 'ZLLPresence':
94 1dd8b4e2 2019-11-06 mischa print(f"{json_data.get(i)['name']:<32s} battery level {json_data.get(i)['config']['battery']}%")