-
Notifications
You must be signed in to change notification settings - Fork 14
/
getconfig.py
executable file
·139 lines (126 loc) · 5.15 KB
/
getconfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# Adoptd from https://github.com/google/python-laurel/blob/master/laurel/__init__.py
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests
import getpass
import json
import random
API_TIMEOUT = 20
# https://github.com/unixpickle/cbyge/blob/main/login.go
# https://github.com/juanboro/cync2mqtt/blob/main/src/acync/__init__.py
def randomLoginResource():
return ''.join([chr(ord('a')+random.randint(0,26)) for i in range(0,16)])
def authenticate():
"""Authenticate with the API and get a token."""
global debug_output
API_AUTH = "https://api.gelighting.com/v2/two_factor/email/verifycode"
auth_data = {
'corp_id': "1007d2ad150c4000",
'email': username,
"local_lang": "en-us"
}
r = requests.post(API_AUTH, json=auth_data, timeout=API_TIMEOUT)
code = input("Enter emailed code: ")
API_AUTH = "https://api.gelighting.com/v2/user_auth/two_factor"
auth_data = {
'corp_id': "1007d2ad150c4000",
'email': username,
'password': password,
"two_factor": code,
"resource": randomLoginResource()
}
r = requests.post(API_AUTH, json=auth_data, timeout=API_TIMEOUT)
debug_output += "\"authenticate()\":" + r.text + ","
try:
return (
r.json()['access_token'],
r.json()['refresh_token'],
r.json()['user_id']
)
except KeyError:
raise(LaurelException('API authentication failed'))
def get_access_token (refresh_token):
"""Get new access_token for subsequent request"""
global debug_output
API_AUTH = "https://api.gelighting.com/v2/user/token/refresh"
auth_data = {"refresh_token": refresh_token}
r = requests.post(API_AUTH, json=auth_data, timeout=API_TIMEOUT)
debug_output += "\"get_access_token()\":" + r.text + ","
return r.json()['access_token']
def get_devices(auth_token, user):
"""Get a list of devices for a particular user."""
global debug_output
API_DEVICES = "https://api2.xlink.cn/v2/user/{user}/subscribe/devices"
headers = {'Access-Token': auth_token}
r = requests.get(API_DEVICES.format(user=user), headers=headers,
timeout=API_TIMEOUT)
debug_output += "\"get_devices()\":" + r.text
return r.json()
def get_properties(auth_token, product_id, device_id):
"""Get properties for a single device."""
global debug_output
API_DEVICE_INFO = "https://api2.xlink.cn/v2/product/{product_id}/device/{device_id}/property"
headers = {'Access-Token': auth_token}
r = requests.get(
API_DEVICE_INFO.format(product_id=product_id, device_id=device_id),
headers=headers,
timeout=API_TIMEOUT
)
debug_output += "," + "\"get_properties()\":" + r.text
return r.json()
errormsg = ""
debug_output = "{"
# have a refresh token and user_id already?
refresh_token = getpass.getpass("Refresh token (if available): ")
user_id = getpass.getpass("`user_id` (if available): ")
is_debug = getpass.getpass("Enable debug output? (y/N): ").lower() == "y"
# otherwise, get authenticate
if not (refresh_token and user_id):
username = input("Cync Username/Email: ")
password = getpass.getpass()
access_token, refresh_token, user_id = authenticate()
print("light:")
devices = get_devices(get_access_token(refresh_token), user_id)
for device in devices:
product_id = device['product_id']
device_id = device['id']
username = device['mac']
access_key = device['access_key']
print(" - platform: gelight")
print(" password: {}".format(access_key))
print(" username: {}".format(username))
print(" lights:")
device_info = get_properties(get_access_token(refresh_token), product_id, device_id)
try:
for bulb in device_info['bulbsArray']:
id = int(bulb['deviceID']) % 1000
mac = [bulb['mac'][i:i+2] for i in range(0, 12, 2)]
mac = "%s:%s:%s:%s:%s:%s" % (mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
name = bulb['displayName']
device_type = bulb['deviceType']
print(" - id: {}".format(id))
print(" mac: {}".format(mac.lower()))
print(" name: {}".format(name))
print(" type: {}".format(device_type))
except KeyError:
errormsg+="Warning: Missing bulb info.\n"
print(errormsg)
if (is_debug):
import json
debug_json = debug_output + "}"
print("=================== debug output ===================")
print(debug_json)
print("========== debug output (pretty printed) ==========")
print(json.dumps(json.loads(debug_json), indent = 2))