Blob


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