#!/usr/bin/env python3 # # Copyright 2019, Mischa Peters , High5!. # Version 1.0 - 20191028 # Version 1.1 - 20191103 - added ['state']['on'] # # Get all light ids and state # # For example: # $ get-lights.py # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ # # Requires: # - Python >3.6 # import argparse import ssl import urllib.request 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)) 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) 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")