Skip to content

Commit f6448ab

Browse files
Paul JenningsPaul Jennings
authored andcommitted
Added command mapping.
1 parent aeb2453 commit f6448ab

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

TStatGcal.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@
128128

129129
import TStat
130130

131-
def main(tstatAddr, username=None, password=None, calName="Thermostat"):
131+
def main(tstatAddr, commandMap=None, username=None, password=None, calName="Thermostat"):
132132
# Connect to thermostat
133133
tstat = TStat.TStat(tstatAddr)
134134

135+
# Command map is used to translate things like "Wake" into "Heat 70"
136+
if commandMap is None:
137+
commandMap = {}
138+
135139
# Log in to Google
136140
calendar_service = gdata.calendar.service.CalendarService()
137141
calendar_service.email = username
@@ -165,13 +169,26 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
165169
closestDT = None
166170
closestWhen = None
167171
closestEvent = None
172+
closestCommand = None
173+
closestValue = None
168174
feed = calendar_service.CalendarQuery(query)
169175
for i, an_event in enumerate(feed.entry):
170176
#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+
171185
# 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
173190
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
175192
continue
176193
try:
177194
float(value)
@@ -198,6 +215,8 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
198215
closestDT = dt
199216
closestWhen = a_when
200217
closestEvent = an_event
218+
closestCommand = command
219+
closestValue = value
201220
else:
202221
if d.days < closest.days:
203222
continue
@@ -206,14 +225,17 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
206225
closestDT = dt
207226
closestWhen = a_when
208227
closestEvent = an_event
228+
closestCommand = command
229+
closestValue = value
209230

210231
if closestEvent is None:
211232
print "No events found"
212233
return
213234

214235
text = closestEvent.title.text
215236
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)
217239
if command == 'Heat':
218240
value = int(value)
219241
if value >= HEAT_MIN and value <= HEAT_MAX:
@@ -239,4 +261,12 @@ def main(tstatAddr, username=None, password=None, calName="Thermostat"):
239261
f = open(os.path.expanduser("~/.google"))
240262
username = f.readline().splitlines()[0]
241263
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

Comments
 (0)