128
128
129
129
import TStat
130
130
131
- def main (tstatAddr , username = None , password = None , calName = "Thermostat" ):
131
+ def main (tstatAddr , commandMap = None , username = None , password = None , calName = "Thermostat" ):
132
132
# Connect to thermostat
133
133
tstat = TStat .TStat (tstatAddr )
134
134
135
+ # Command map is used to translate things like "Wake" into "Heat 70"
136
+ if commandMap is None :
137
+ commandMap = {}
138
+
135
139
# Log in to Google
136
140
calendar_service = gdata .calendar .service .CalendarService ()
137
141
calendar_service .email = username
@@ -165,13 +169,26 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
165
169
closestDT = None
166
170
closestWhen = None
167
171
closestEvent = None
172
+ closestCommand = None
173
+ closestValue = None
168
174
feed = calendar_service .CalendarQuery (query )
169
175
for i , an_event in enumerate (feed .entry ):
170
176
#print '\t%s. %s' % (i, an_event.title.text,)
177
+
178
+ # Try to map named time period into actual command
179
+ text = an_event .title .text .strip ()
180
+ if commandMap .has_key (text ):
181
+ text = commandMap [text ]
182
+
183
+ print "Translated %s into %s" % (an_event .title .text .strip (), text )
184
+
171
185
# Skip events that are not valid commands
172
- (command , value ) = an_event .title .text .splitlines ()[0 ].split ()
186
+ try :
187
+ (command , value ) = text .splitlines ()[0 ].split ()
188
+ except :
189
+ command = text
173
190
if command not in COMMANDS :
174
- print "Warning: '%s' is not a valid command" % an_event . title . text
191
+ print "Warning: '%s' is not a valid command" % text
175
192
continue
176
193
try :
177
194
float (value )
@@ -198,6 +215,8 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
198
215
closestDT = dt
199
216
closestWhen = a_when
200
217
closestEvent = an_event
218
+ closestCommand = command
219
+ closestValue = value
201
220
else :
202
221
if d .days < closest .days :
203
222
continue
@@ -206,14 +225,17 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
206
225
closestDT = dt
207
226
closestWhen = a_when
208
227
closestEvent = an_event
228
+ closestCommand = command
229
+ closestValue = value
209
230
210
231
if closestEvent is None :
211
232
print "No events found"
212
233
return
213
234
214
235
text = closestEvent .title .text
215
236
print "Closest event: %s at %s" % (text , closestDT )
216
- (command , value ) = text .splitlines ()[0 ].split ()
237
+ #(command, value) = text.splitlines()[0].split()
238
+ command , value = (closestCommand , closestValue )
217
239
if command == 'Heat' :
218
240
value = int (value )
219
241
if value >= HEAT_MIN and value <= HEAT_MAX :
@@ -239,4 +261,12 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
239
261
f = open (os .path .expanduser ("~/.google" ))
240
262
username = f .readline ().splitlines ()[0 ]
241
263
password = f .readline ().splitlines ()[0 ]
242
- main (sys .argv [1 ], username = username , password = password , calName = sys .argv [2 ])
264
+ f .close ()
265
+ if os .path .isfile (os .path .expanduser ("~/.tstat_commands" )):
266
+ commandMap = {}
267
+ f = open (os .path .expanduser ("~/.tstat_commands" ))
268
+ for line in f .readlines ():
269
+ key , value = line .split (":" )
270
+ commandMap [key ] = value
271
+ f .close ()
272
+ main (sys .argv [1 ], username = username , password = password , calName = sys .argv [2 ], commandMap = commandMap )
0 commit comments