This repository was archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
58 lines (47 loc) · 1.34 KB
/
server.py
File metadata and controls
58 lines (47 loc) · 1.34 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
import bulbs
import json
import logging
import os.path
from flask import Flask, request, render_template
from flask_caching import Cache
from wyze_sdk import Client
from wyze_sdk.errors import WyzeApiError
def get_client(cache):
client = cache.get('client')
try:
client.user_get_info()
except WyzeApiError as e:
if 'refresh the token' in str(e):
logging.info('refreshing auth token')
client = bulbs.create_client()
client.refresh_token()
bulbs.write_tokens(client)
cache.set('client', client)
else:
raise e
return client
def create_app():
cache = Cache(config={
'CACHE_TYPE': 'SimpleCache',
'CACHE_DEFAULT_TIMEOUT': 0,
})
app = Flask(__name__)
cache.init_app(app)
client = bulbs.create_client()
bulbs.write_tokens(client)
cache.set('client', client)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/load', methods=['POST'])
def load():
name = request.form.get('name')
if name is None:
return 'name is not set', 400
client = get_client(cache)
try:
bulbs.load_state(client, name)
except KeyError as e:
return str(e), 400
return f'Set {name} lights', 200
return app