1 b455762f 2019-12-27 mischa #!/usr/bin/env python3
3 b455762f 2019-12-27 mischa # Copyright 2019, Mischa Peters <mischa AT high5 DOT nl>, High5!.
4 b455762f 2019-12-27 mischa # Version 1.0 - 20191227
6 b455762f 2019-12-27 mischa # Create a new user on the bridge
8 b455762f 2019-12-27 mischa # For example:
9 496ad2f3 2020-05-07 mischa # $ add-newdeveloper.py <bridge IP>
11 b455762f 2019-12-27 mischa # Follow the steps at the Hue Developer site to get the username/token
12 b455762f 2019-12-27 mischa # https://developers.meethue.com/develop/get-started-2/
14 b455762f 2019-12-27 mischa # Requires:
15 b455762f 2019-12-27 mischa # - Python >3.6
17 b455762f 2019-12-27 mischa import argparse
18 b455762f 2019-12-27 mischa import ssl
19 b455762f 2019-12-27 mischa import urllib.request
20 b455762f 2019-12-27 mischa import json
22 496ad2f3 2020-05-07 mischa parser = argparse.ArgumentParser(description="Create a new developer token on the Hue Bridge")
23 496ad2f3 2020-05-07 mischa parser.add_argument("bridge", type=str, help="Hue Bridge IP address")
26 b455762f 2019-12-27 mischa args = parser.parse_args()
27 b455762f 2019-12-27 mischa bridge = args.bridge
29 b455762f 2019-12-27 mischa except argparse.ArgumentError as e:
30 b455762f 2019-12-27 mischa print(str(e))
32 b455762f 2019-12-27 mischa no_cert_check = ssl.create_default_context()
33 b455762f 2019-12-27 mischa no_cert_check.check_hostname=False
34 b455762f 2019-12-27 mischa no_cert_check.verify_mode=ssl.CERT_NONE
36 b455762f 2019-12-27 mischa data = b'{"devicetype": "my_hue_app#device"}'
37 b455762f 2019-12-27 mischa url = f"https://{bridge}/api"
38 b455762f 2019-12-27 mischa req = urllib.request.Request(url=url, data=data, method='POST')
39 b455762f 2019-12-27 mischa with urllib.request.urlopen(req, context=no_cert_check) as response:
40 b455762f 2019-12-27 mischa content = response.read()
41 b455762f 2019-12-27 mischa json_data = json.loads(content)
42 b455762f 2019-12-27 mischa print (f"{json_data}")