added option to query specific 'id'

This commit is contained in:
mischa 2019-12-27 15:46:43 +01:00
parent ef999189ea
commit 51565e99fc
1 changed files with 16 additions and 8 deletions

View File

@ -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")