2019-11-01 16:00:50 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright 2019, Mischa Peters <mischa AT high5 DOT nl>, High5!.
|
|
|
|
# Version 1.0 - 20191028
|
2019-11-06 11:13:00 +01:00
|
|
|
# Version 1.1 - 20191106 - added battery status
|
2019-11-01 16:00:50 +01:00
|
|
|
#
|
2019-11-01 16:07:08 +01:00
|
|
|
# Get all sensor IDs (ZLLPresence, ZLLLightLevel and ZLLTemperature)
|
|
|
|
# grouped by ZLLPresence name
|
2019-11-02 10:28:38 +01:00
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
# $ get-sensors.py <bridge IP> <token>
|
|
|
|
#
|
|
|
|
# Follow the steps at the Hue Developer site to get the username/token
|
|
|
|
# https://developers.meethue.com/develop/get-started-2/
|
2019-11-01 16:07:08 +01:00
|
|
|
#
|
2019-11-01 16:00:50 +01:00
|
|
|
# Requires:
|
2019-11-01 16:55:25 +01:00
|
|
|
# - Python >3.6
|
2019-11-01 16:00:50 +01:00
|
|
|
#
|
|
|
|
import argparse
|
|
|
|
import ssl
|
|
|
|
import urllib.request
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import collections
|
|
|
|
|
2019-11-03 14:17:34 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Get all sensor ids from Hue Bridge")
|
2019-11-01 16:00:50 +01:00
|
|
|
parser.add_argument("bridge", type=str, help="Hue Bridge IP")
|
|
|
|
parser.add_argument("token", type=str, help="Hue API Token")
|
2019-11-06 20:19:05 +01:00
|
|
|
parser.add_argument("-b", "--battery", type=int, help="battery check only, threshold, default 20")
|
|
|
|
parser.add_argument("-v", "--verbose", action='store_true', help="verbose")
|
2019-11-01 16:00:50 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
args = parser.parse_args()
|
|
|
|
bridge = args.bridge
|
|
|
|
token = args.token
|
2019-11-06 20:19:05 +01:00
|
|
|
battery = args.battery
|
|
|
|
verbose = args.verbose
|
2019-11-01 16:00:50 +01:00
|
|
|
|
|
|
|
except argparse.ArgumentError as e:
|
|
|
|
print(str(e))
|
|
|
|
|
|
|
|
no_cert_check = ssl.create_default_context()
|
|
|
|
no_cert_check.check_hostname=False
|
|
|
|
no_cert_check.verify_mode=ssl.CERT_NONE
|
|
|
|
|
|
|
|
url = f"https://{bridge}/api/{token}/sensors"
|
|
|
|
req = urllib.request.Request(url)
|
|
|
|
with urllib.request.urlopen(req, context=no_cert_check) as response:
|
|
|
|
content = response.read()
|
|
|
|
json_data = json.loads(content)
|
|
|
|
|
|
|
|
p = re.compile("([a-fA-F0-9]{2}:?){8}")
|
|
|
|
sensors = collections.defaultdict(list);
|
|
|
|
|
|
|
|
for key in json_data:
|
|
|
|
if "uniqueid" in json_data[key]:
|
|
|
|
if p.search(json_data[key]['uniqueid']):
|
|
|
|
if json_data[key]['type'] == 'ZLLPresence':
|
|
|
|
sensors[json_data[key]['uniqueid'][:-8]].insert(0, key)
|
|
|
|
else:
|
|
|
|
sensors[json_data[key]['uniqueid'][:-8]].append(key)
|
|
|
|
|
|
|
|
for key in sensors:
|
|
|
|
for i in sensors[key]:
|
2019-11-06 20:19:05 +01:00
|
|
|
if not battery:
|
|
|
|
if json_data.get(i)['type'] == 'ZLLPresence':
|
|
|
|
print(f"{json_data.get(i)['name']:<32s} ({json_data.get(i)['config']['battery']}%)")
|
|
|
|
print(f"{i:>5s}: {json_data.get(i)['productname']}")
|
|
|
|
if json_data.get(i)['type'] == 'ZLLLightLevel':
|
|
|
|
print(f"{i:>5s}: {json_data.get(i)['productname']}")
|
|
|
|
if json_data.get(i)['type'] == 'ZLLTemperature':
|
|
|
|
print(f"{i:>5s}: {json_data.get(i)['productname']}")
|
|
|
|
else:
|
|
|
|
if int(json_data.get(i)['config']['battery']) < battery:
|
|
|
|
if json_data.get(i)['type'] == 'ZLLPresence':
|
|
|
|
print(f"{json_data.get(i)['name']:<32s} battery level {json_data.get(i)['config']['battery']}%")
|