-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
211 lines (147 loc) · 4.75 KB
/
server.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import requests
from flask import Flask, render_template, jsonify
from flask_httpauth import HTTPBasicAuth
from soco import SoCo
from phue import Bridge
from vars import *
app = Flask(__name__)
app.debug = False
auth = HTTPBasicAuth()
@auth.get_password
def get_pw(username):
if username in users:
return users.get(username)
return None
@app.route("/")
def index():
return render_template('index.html', your_name=YOUR_NAME)
def get_sonos_playlist(sonos, title):
playlists = sonos.get_sonos_playlists()
for playlist in playlists:
if playlist.title == title:
return playlist
return None
@app.route("/automation/bttn/sexy-time/")
@auth.login_required
def sexy_time():
# connect to the Sonos
sonos = SoCo(SONOS_IP)
# connect to Philips Hue Bridge
hue = Bridge(ip=HUE_IP,
username=HUE_USERNAME)
# get queue
queue = sonos.get_queue()
# if we:
# * already have a queue
# * music is playing
# * we are already playing a queue that begins with "Let's Get It On"
# ...then skip to the next track
if len(queue) > 0 and \
sonos.get_current_transport_info()['current_transport_state'] == "PLAYING" and \
queue[0].title == SEXY_TIME_FIRST_TRACK:
sonos.next()
# else, intitiate a fresh Sexy Time
else:
# clear Sonos queue
sonos.clear_queue()
# turn off shuffle and repeat
sonos.play_mode = 'NORMAL'
# set volume
sonos.volume = 45
# play Sexy Time playlist
playlist = get_sonos_playlist(sonos, SEXY_TIME_PLAYLIST_NAME)
if playlist:
sonos.add_to_queue(playlist)
sonos.play()
# dim the lights (bri out of 254) over the pre-defined amount of time
command = {
'transitiontime': (SEXY_TIME_DIMMER_SECONDS * 10),
'on': True,
'bri': SEXY_TIME_DIMMER_BRIGHTNESS
}
hue.set_light(SEXY_TIME_LIGHTS, command)
return jsonify(status="success")
@app.route("/automation/bttn/party/")
@auth.login_required
def party():
# connect to the Sonos
sonos = SoCo(SONOS_IP)
# get queue
queue = sonos.get_queue()
# if we:
# * already have a queue
# * music is playing
# ...then skip to the next track
if len(queue) > 0 and sonos.get_current_transport_info()['current_transport_state'] == "PLAYING":
sonos.next()
# else, intitiate a fresh Party Time
else:
# clear Sonos queue
sonos.clear_queue()
# turn on shuffle, turn off repeat
sonos.play_mode = 'SHUFFLE_NOREPEAT'
# set volume
sonos.volume = 45
# play Party playlist
playlist = get_sonos_playlist(sonos, PARTY_TIME_PLAYLIST_NAME)
if playlist:
sonos.add_to_queue(playlist)
sonos.play()
return jsonify(status="success")
@app.route("/automation/arriving-home/")
@auth.login_required
def arriving_home():
# connect to Philips Hue Bridge
hue = Bridge(ip=HUE_IP,
username=HUE_USERNAME)
# set the lights to appropriate brightness over appropriate time
command = {
'transitiontime': (ARRIVING_HOME_DIMMER_SECONDS * 10),
'on': True,
'bri': ARRIVING_HOME_DIMMER_BRIGHTNESS
}
hue.set_light(ARRIVING_HOME_LIGHTS, command)
# connect to the Sonos
sonos = SoCo(SONOS_IP)
# clear the queue
sonos.clear_queue()
# set volume
sonos.volume = ARRIVING_HOME_VOLUME
# play Arriving Home playlist
playlist = get_sonos_playlist(sonos, ARRIVING_HOME_PLAYLIST_NAME)
if playlist:
sonos.add_to_queue(playlist)
# turn on shuffle, turn off repeat
sonos.play_mode = 'SHUFFLE_NOREPEAT'
# play
sonos.play()
# we're in shuffle mode, but the first track is always the same
sonos.next()
return jsonify(status="success")
@app.route("/automation/stop/")
@auth.login_required
def bttn_stop():
# connect to the Sonos
sonos = SoCo(SONOS_IP)
# connect to Philips Hue Bridge
hue = Bridge(ip=HUE_IP,
username=HUE_USERNAME)
# stop the Sonos and reset to sensible defaults
queue = sonos.get_queue()
sonos.clear_queue()
sonos.volume = STOP_VOLUME
sonos.play_mode = 'NORMAL'
sonos.stop()
# set the lights back to a sensible default
command = {
'transitiontime': (STOP_DIMMER_SECONDS * 10),
'on': True,
'bri': STOP_DIMMER_BRIGHTNESS
}
hue.set_light(STOP_LIGHTS, command)
return jsonify(status="success")
if (app.debug):
from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, port=9000)