From 5f20079a628af90cf2fb78607171822084cacea4 Mon Sep 17 00:00:00 2001 From: mischa Date: Sun, 3 Nov 2019 16:55:07 +0100 Subject: [PATCH] added get-id.py --- daylight-trigger.py | 2 +- get-id.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 get-id.py diff --git a/daylight-trigger.py b/daylight-trigger.py index e450df6..49935e0 100755 --- a/daylight-trigger.py +++ b/daylight-trigger.py @@ -8,7 +8,7 @@ # depending where your sensor is located you can use ['daylight'] # # For example: -# $ daylight-trigger -s 50 -l 24 +# $ daylight-trigger.py -s 50 -l 24 # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ diff --git a/get-id.py b/get-id.py new file mode 100755 index 0000000..116e22a --- /dev/null +++ b/get-id.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# +# Copyright 2019, Mischa Peters , High5!. +# Version 1.0 - 20191103 +# +# Collect all information of given id +# +# For example: +# $ get-id.py -t sensors -i 6 +# +# Requires: +# - Python 3.x +# +import argparse +import ssl +import urllib.request +import json + +parser = argparse.ArgumentParser(description="Get id information") +parser.add_argument("bridge", type=str, help="Hue Bridge IP") +parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("-i", "--id", type=int, default='1', help="id#") +parser.add_argument("-t", "--type", type=str, default='lights', help="lights|sensors|groups") +parser.add_argument("-v", "--verbose", action='store_true', help="verbose") + +try: + args = parser.parse_args() + bridge = args.bridge + token = args.token + id = args.id + type = args.type + verbose = args.verbose + +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}/{type}/{id}" +req = urllib.request.Request(url) +with urllib.request.urlopen(req, context=no_cert_check) as response: + content = response.read() +json_data = json.loads(content) + +if not 'state' in json_data: + print(f"{type[:-1]} id {id} doesn't exist") +else: + if verbose: print("json dump:") + if verbose: print(json.dumps(json_data, indent=4, sort_keys=True)) + if verbose: print("state") + print(json_data['state'])