-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
134 lines (122 loc) · 4.58 KB
/
server.py
File metadata and controls
134 lines (122 loc) · 4.58 KB
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
from flask import Flask, request, jsonify
from wyze_sdk import Client
from wyze_sdk.errors import WyzeApiError
from wyze_sdk.errors import WyzeClientConfigurationError
import os
app = Flask(__name__)
# Get the API password key from the environment variables
api_password = os.getenv('KEY') or 'mykey'
always_refresh = bool(os.getenv('ALWAYS_REFRESH')) or True
client = Client()
# create client
try:
print("Using environment variables for USERNAME, PASSWORD, API_KEY, KEY_ID, and TOTP")
print("Get an API key/id from: https://developer-api-console.wyze.com/#/apikey/view")
username = os.getenv('USERNAME')
password = os.getenv('PASSWORD')
api_key = os.getenv('API_KEY')
key_id = os.getenv('KEY_ID')
totp = os.getenv('TOTP')
client = Client(email=username, password=password, totp_key=totp, api_key=api_key, key_id=key_id)
# test the API
out_test = {}
for plug in client.plugs.list():
out_test[plug.mac] = plug.to_dict()
print("Connected to Wyze API")
except WyzeApiError as error:
print(f'Got an error: {error}')
except WyzeClientConfigurationError as error:
print(f'Got an error. May be caused by invalid API KEY/ID: {error}')
except Exception as error:
print(f'Got an error: {error}')
@app.route('/plug/on', methods=['GET'])
def plug_on():
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
print("Token refreshed")
except WyzeApiError as e:
print(f'Token refresh error: {e}')
try:
macs = request.args.get('macs').split(',')
result = None
for mac in macs:
plug = client.plugs.info(device_mac=mac)
result = client.plugs.turn_on(
device_mac=plug.mac,
device_model=plug.product.model
)
return jsonify(message=result.data), result.status_code
except WyzeApiError as e:
print(f'Got an error: {e}')
client.refresh_token()
print("Token refreshed")
else:
return jsonify(message="Invalid API key"), 403
@app.route('/plug/off', methods=['GET'])
def plug_off():
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
print("Token refreshed")
except WyzeApiError as e:
print(f'Token refresh error: {e}')
try:
macs = request.args.get('macs').split(',')
result = None
for mac in macs:
plug = client.plugs.info(device_mac=mac)
result = client.plugs.turn_off(
device_mac=plug.mac,
device_model=plug.product.model
)
return jsonify(message=result.data), result.status_code
except WyzeApiError as e:
print(f'Got an error: {e}')
client.refresh_token()
print("Token refreshed")
else:
return jsonify(message="Invalid API key"), 403
@app.route('/plug', methods=['GET'])
def plug_list():
# Check the 'X-Api-Key' header of the request
if request.args.get('key') == api_password:
try:
if always_refresh:
client.refresh_token()
print("Token refreshed")
except WyzeApiError as e:
print(f'Token refresh error: {e}')
if request.args.get('macs') is not None:
result = []
try:
macs = request.args.get('macs').split(',')
for mac in macs:
plug = client.plugs.info(device_mac=mac)
result.append(client.plugs.turn_off(
device_mac=plug.mac,
device_model=plug.product.model
))
except WyzeApiError as e:
print(f'Got an error: {e}')
client.refresh_token()
print("Token refreshed")
return jsonify(message=result.to_dict()), 200
else:
out = {}
try:
plugs = client.plugs.list()
for plug in plugs:
out[plug.mac] = plug.to_dict()
except WyzeApiError as e:
print(f'Got an error: {e}')
client.refresh_token()
print("Token refreshed")
return jsonify(out), 200
else:
return jsonify(message="Invalid API key"), 403
if __name__ == "__main__":
# Note: Make sure to use '0.0.0.0' as the host in Docker
app.run(host='0.0.0.0', port=5000)