From 51565e99fca280d6809ffa9d8b9cf8573f859b45 Mon Sep 17 00:00:00 2001 From: mischa Date: Fri, 27 Dec 2019 15:46:43 +0100 Subject: [PATCH] added option to query specific 'id' --- get-lights.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/get-lights.py b/get-lights.py index 9f4c620..f60d943 100755 --- a/get-lights.py +++ b/get-lights.py @@ -23,11 +23,13 @@ import json parser = argparse.ArgumentParser(description="Get all light ids from Hue Bridge") 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, help="light id#") try: args = parser.parse_args() bridge = args.bridge token = args.token + id = args.id except argparse.ArgumentError as e: print(str(e)) @@ -42,11 +44,17 @@ with urllib.request.urlopen(req, context=no_cert_check) as response: content = response.read() json_data = json.loads(content) - -print(f"{'ID':>3s} {'Name':<32s} {'State':<5s} Type") -print ("################################################################################") -for key in json_data: - if not json_data[key]['state']['reachable']: - continue - state = 'on' if json_data[key]['state']['on'] else 'off' - print(f"{key:>3s}: {json_data[key]['name']:<32s} {state:<5s} {json_data[key]['type']}") +if not id: + print(f"{'ID':>3s} {'Name':<32s} {'State':<5s} Type") + print ("################################################################################") + for key in json_data: + if not json_data[key]['state']['reachable']: + continue + state = 'on' if json_data[key]['state']['on'] else 'off' + print(f"{key:>3s}: {json_data[key]['name']:<32s} {state:<5s} {json_data[key]['type']}") +else: + if json_data[str(id)]['state']['reachable']: + state = 'on' if json_data[str(id)]['state']['on'] else 'off' + print(state) + else: + print("unreachable")