hue/get-lights.py

48 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
#
# Copyright 2019, Mischa Peters <mischa AT high5 DOT nl>, High5!.
# Version 1.0 - 20191028
#
2019-11-01 16:07:08 +01:00
# Get all light IDs
2019-11-02 10:28:38 +01:00
#
# For example:
# $ get-lights.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
#
# Requires:
2019-11-01 16:55:25 +01:00
# - Python >3.6
#
import argparse
import ssl
import urllib.request
import json
2019-11-03 14:17:34 +01:00
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")
try:
args = parser.parse_args()
bridge = args.bridge
token = args.token
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}/lights"
req = urllib.request.Request(url)
with urllib.request.urlopen(req, context=no_cert_check) as response:
content = response.read()
json_data = json.loads(content)
2019-11-02 08:59:13 +01:00
print(f"{'ID':>2s}: {'Name':<30s} Type")
print ("################################################################################")
for key in json_data:
2019-11-03 17:09:18 +01:00
print(f"{key:>2s}: {json_data[key]['name']:<32s} {json_data[key]['type']}")