forked from zpriddy/ZP_EchoNestPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echopy_nest.py
182 lines (115 loc) · 5.86 KB
/
echopy_nest.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
import json
import nestpy_app
import echopy_doc
import nestpy_lib as nest
import nestpy_settings as settings
appVersion = 1.0
def data_init():
global MyDataStore
MyDataStore = DataStore()
def data_handler(rawdata):
global MyDataStore
#print rawdata['session']
currentSession = MyDataStore.getSession(rawdata['session'])
currentUser = MyDataStore.getUser(rawdata['session'])
currentRequest = rawdata['request']
response = request_handler(currentSession, currentUser, currentRequest)
#print json.dumps({"version":appVersion,"response":response},sort_keys=True,indent=4)
return json.dumps({"version":appVersion,"response":response},indent=2,sort_keys=True)
def request_handler(session, user, request):
requestType = request['type']
if requestType == "LaunchRequest":
return launch_request(session, user, request)
elif requestType == "IntentRequest":
return intent_request(session, user, request)
def launch_request(session, user, request):
if not nest.isValidUser(user.getUserId()):
output_speech = "Current user is not a valid nest user. Please look at the Echo app for help"
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Setting Nest Temp"
card_content = "Current user is not a valid nest user. Please authenticate user with userId: " + user.getUserId() + " to Nest as instructed in the README"
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True}
return response
else:
output_speech = "Welcome to Nest Control App. Please say a command."
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Welcome"
card_content = "Welcome to Nest Control App. Please say a command."
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':False}
return response
def intent_request(session, user, request):
print "intent_request"
if not nest.isValidUser(user.getUserId()):
output_speech = "Current user is not a valid nest user. Please look at the Echo app for help"
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Setting Nest Temp"
card_content = "Current user is not a valid nest user. Please authenticate user with userId: " + user.getUserId() + " to Nest as instructed in the README"
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True}
return response
else:
if request['intent']['name'] == "NestSetTempIntent":
nestTempValue = request['intent']['slots']['temp']['value']
output_speech = "Setting Nest to " + str(nestTempValue) + " degrees fahrenheit"
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Setting Nest Temp"
card_content = "Telling Nest to set to " + str(nestTempValue) + " degrees fahrenheit."
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True}
if int(nestTempValue) <= 90:
nest.setTemperatureTargetAll(user.getUserId(),int(nestTempValue))
return response
elif request['intent']['name'] == "NestCoolDownIntent":
setTemp = nest.setTurnDownTemperatureAll(user.getUserId())
output_speech = "Turning down the Nest"
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Setting Nest Temp"
card_content = "Telling Nest to set to " + "str(setTemp+2)" + " degrees fahrenheit."
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True}
return response
elif request['intent']['name'] == "NestWarmUpIntent":
setTemp = nest.setTurnUpTemperatureAll(user.getUserId())
output_speech = "Turning up the Nest"
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Setting Nest Temp"
card_content = "Telling Nest to set to " + "str(setTemp+2)" + " degrees fahrenheit."
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':True}
return response
elif request['intent']['name'] == "HelpIntent":
output_speech = "This is the Nest control app. You can tell me to set temperature to 74 degrees fahrenheit. You can also say that you are too hot or too cold and I will adjust the temperature by two degrees."
output_type = "PlainText"
card_type = "Simple"
card_title = "Nest Control - Help"
card_content = "This is the Nest control app. You can tell me to set temperature to 74 degrees. You can also say that you are too hot or too cold and I will adjust the temperature by two degrees."
response = {"outputSpeech": {"type":output_type,"text":output_speech},"card":{"type":card_type,"title":card_title,"content":card_content},'shouldEndSession':False}
return response
else:
return launch_request(session, user, request) ##Just do the same thing as launch request
class Session:
def __init__(self,sessionData):
self.sessionId = sessionData['sessionId']
def getSessionID(self):
return self.sessionId
class User:
def __init__(self,userId):
self.userId = userId
self.settings = {}
def getUserId(self):
return self.userId
class DataStore:
def __init__(self):
self.sessions = {}
self.users = {}
def getSession(self,session):
if session['new'] is True or session['sessionId'] not in self.sessions:
self.sessions[session['sessionId']] = Session(session)
return self.sessions[session['sessionId']]
def getUser(self,session):
userId = session['user']['userId']
if userId not in self.users:
self.users[userId] = User(userId)
return self.users[userId]