forked from Einstein42/myq-garage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myq-garage.py
executable file
·398 lines (352 loc) · 15.3 KB
/
myq-garage.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/python3
## Python to interface with MyQ garage doors.
## Thanks to xKing for the new API stuff. Find him on the UDI Forums.
'''
The MIT License (MIT)
Copyright (c) 2015
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from __future__ import print_function
import requests
from requests.auth import HTTPBasicAuth
from requests.utils import quote
import sys
import time
import datetime
import os
import logging
import logging.handlers
import json
try:
from ConfigParser import RawConfigParser
except ImportError as e:
from configparser import RawConfigParser
# Try to use the C implementation first, falling back to python, these libraries are usually built-in libs.
try:
from xml.etree import cElementTree as ElementTree
except ImportError as e:
from xml.etree import ElementTree
requests.packages.urllib3.disable_warnings()
config = RawConfigParser()
config.read('config.ini')
#main Configuration
USERNAME = config.get('main', 'USERNAME')
PASSWORD = config.get('main', 'PASSWORD')
BRAND = config.get('main', 'BRAND')
TOKENTTL = config.get('main', 'TOKENTTL')
# ISY Configuration
USE_ISY = config.get('ISYConfiguration', 'USE_ISY') == 'True'
ISY_HOST = config.get('ISYConfiguration', 'ISY_HOST')
ISY_PORT = config.get('ISYConfiguration', 'ISY_PORT')
ISY_USERNAME = config.get('ISYConfiguration', 'ISY_USERNAME')
ISY_PASSWORD = config.get('ISYConfiguration', 'ISY_PASSWORD')
ISY_VAR_PREFIX = config.get('ISYConfiguration', 'ISY_VAR_PREFIX')
#MyQ API Configuration
if (BRAND.lower() == 'chamberlain'):
SERVICE = config.get('APIglobal', 'ChamberSERVICE')
APPID = config.get('APIglobal', 'ChamberAPPID')
CULTURE = 'en'
BRANDID = '2'
elif (BRAND.lower() == 'craftsman'):
SERVICE = config.get('APIglobal', 'CraftSERVICE')
APPID = config.get('APIglobal', 'CraftAPPID')
CULTURE = 'en'
BRANDID = '3'
else:
print(BRAND, " is not a valid brand name. Check your configuration")
def setup_log(name):
# Log Location
PATH = os.getcwd()
if not os.path.exists(PATH + '/logs'):
os.makedirs(PATH + '/logs')
LOG_FILENAME = PATH + "/logs/myq-garage.log"
LOG_LEVEL = logging.INFO # Could be e.g. "DEBUG" or "WARNING"
#### Logging Section ################################################################################
LOGGER = logging.getLogger('sensors')
LOGGER.setLevel(LOG_LEVEL)
# Set the log level to LOG_LEVEL
# Make a handler that writes to a file,
# making a new file at midnight and keeping 3 backups
HANDLER = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when="midnight", backupCount=30)
# Format each log message like this
FORMATTER = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
# Attach the formatter to the handler
HANDLER.setFormatter(FORMATTER)
# Attach the handler to the logger
LOGGER.addHandler(HANDLER)
return LOGGER
class MyQLogger(object):
""" Logger Class """
def __init__(self, logger, level):
"""Needs a logger and a logger level."""
self.logger = logger
self.level = level
def write(self, logmessage):
""" Only log if there is a message (not just a new line) """
if logmessage.rstrip() != "":
self.logger.log(self.level, logmessage.rstrip())
def read(self, logmessage):
"""" Does nothing, pylist complained """
pass
class Device:
def __init__(self, id, name, state, uptime):
self.id = id
self.name = name
self.state = state
self.time = uptime
def isy_set_var_state(id, name, varname, value):
init, val = isy_get_var_state(id)
if value == int(val):
#print(varname, "is already set to ", val)
LOGGER.info('%s is already set to %s', varname, val)
return
try:
r = requests.get('http://' + ISY_HOST + ':' + ISY_PORT + '/rest/vars/set/2/' + id + '/' + str(value), auth=HTTPBasicAuth(ISY_USERNAME, ISY_PASSWORD))
except requests.exceptions.RequestException as err:
LOGGER.error('Caught Exception in isy_set_var_state: ' + str(err))
return
if int(r.status_code) == 404:
print(str(id), " not found on ISY. Response was 404")
LOGGER.error("%s not found on ISY. Response was 404", id)
elif int(r.status_code) != 200:
print("Status change failed, response from ISY: ", str(r.status_code), str(r.text))
LOGGER.error('Status change failed, response from ISY: %s - %s', str(r.status_code), str(r.text))
else:
print('{} changed successfully to {}'.format(varname, value))
def isy_get_var_state(id):
try:
r = requests.get('http://' + ISY_HOST + ':' + ISY_PORT + '/rest/vars/get/2/' + id, auth=HTTPBasicAuth(ISY_USERNAME, ISY_PASSWORD))
except requests.exceptions.RequestException as err:
LOGGER.error('Caught Exception in isy_get_var_state: ' + str(err))
return
tree = ElementTree.fromstring(r.text)
init = tree.find('init').text
value = tree.find('val').text
LOGGER.info('Get_Var_State: init: %s - val: %s', init, value)
return init, value
def isy_get_var_id(name):
varname = str(ISY_VAR_PREFIX + name.replace(" ", "_"))
try:
r = requests.get('http://' + ISY_HOST + ':' + ISY_PORT + '/rest/vars/definitions/2', auth=HTTPBasicAuth(ISY_USERNAME, ISY_PASSWORD))
except requests.exceptions.RequestException as err:
LOGGER.error('Caught Exception in isy_get_var_id: ' + str(err))
return
#LOGGER.info('Get_Var_ID: Request response: %s %s', r.status_code, r.text)
tree = ElementTree.fromstring(r.text)
LOGGER.info('Searching ISY Definitions for %s', varname)
valid = False
for e in tree.findall('e'):
if e.get('name') == varname:
valid = True
id, name = e.get('id'), e.get('name')
if valid:
#id, name = child.get('id'), child.get('name')
LOGGER.info('State variable: %s found with ID: %s', name, id)
else:
print("State variable: " + varname + " not found in ISY variable list")
print("Fix your state variables on the ISY. Then enable the ISY section again.")
sys.exit(5)
init, value = isy_get_var_state(id)
LOGGER.info('ISY Get Var ID Return - id: %s - varname: %s - init: %s - value: %s', id, varname, init, value)
return id, varname, init, value
class MyQ:
def __init__(self):
baseurl = SERVICE + "/api/v4"
self.session = requests.Session()
self.appid = APPID
self.username = USERNAME
self.password = PASSWORD
self.headers = { "User-Agent": "Chamberlain/3.73",
"BrandId": BRANDID,
"ApiVersion": "4.1",
"Culture": CULTURE,
"MyQApplicationId": self.appid }
self.authurl = baseurl+"/User/Validate"
self.enumurl = baseurl+"/userdevicedetails/get"
self.seturl = baseurl+"/DeviceAttribute/PutDeviceAttribute"
self.geturl = baseurl+"/deviceattribute/getdeviceattribute"
self.tokenfname="/tmp/myqtoken.json"
self.tokentimeout=TOKENTTL
self.read_token()
def save_token(self):
if (float(self.tokentimeout) > 0):
ts=time.time()
token_file={}
token_file["SecurityToken"]=self.securitytoken
token_file["TimeStamp"]=datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
json_data=json.dumps(token_file)
f = open(self.tokenfname,"w")
f.write(json_data)
f.close()
os.chmod(self.tokenfname, 0o600)
def read_token(self):
if (os.path.isfile(self.tokenfname)):
with open(self.tokenfname,"r") as f:
data = f.read()
res = json.loads(data) if hasattr(json, "loads") else json.read(data)
self.securitytoken = res["SecurityToken"]
else:
self.login()
def login(self):
payload = { "username": self.username, "password": self.password }
req = self.session.post(self.authurl, headers=self.headers, json=payload)
if (req.status_code != requests.codes.ok):
print ("Login err code: " + req.status_code)
sys.exit(-1)
res = req.json()
if (res["ReturnCode"] == "0"):
self.securitytoken = res["SecurityToken"]
self.save_token()
else:
print ("Authentication Failed")
sys.exit(-1)
# State = 0 for closed/off or 1 for open/on
def set_state(self, device, device_type, desired_state):
if device.state in ['Open', 'On'] and desired_state == 1:
print(device.name + ' already ' + device.state + '.')
sys.exit(5)
if device.state in ['Closed', 'Off'] and desired_state == 0:
print(device.name + ' already ' + device.state + '.')
sys.exit(6)
post_data = {
"AttributeName" : "desired" + device_type + "state",
"MyQDeviceId" : device.id,
"ApplicationId" : self.appid,
"AttributeValue" : desired_state,
"SecurityToken" : self.securitytoken,
"format" : "json",
"nojsoncallback" : "1"
}
self.session.headers.update({ "SecurityToken": self.securitytoken })
payload = { "appId": self.appid, "SecurityToken": self.securitytoken }
req = self.session.put(self.seturl, headers=self.headers, params=payload, data=post_data)
if (req.status_code != requests.codes.ok):
print ("Enum err code: " + req.status_code)
return -1
res = req.json()
if (res["ReturnCode"] == "0"):
print ("status changed")
return True
else:
print ("Can't set state, bad token?")
return False
def fetch_device_json(self):
payload = {
"appId": self.appid,
"SecurityToken": self.securitytoken,
"filterOn": "true",
"format":
"json",
"nojsoncallback": "1" }
self.session.headers.update({ "SecurityToken": self.securitytoken })
req = self.session.get(self.enumurl, headers=self.headers, params=payload)
if (req.status_code != requests.codes.ok):
print ("Enum err code: " + req.status_code)
return -1
return req.json()
def get_state(self, dev_type, value):
# States value from API returns an interger, the index corresponds to the below list. Zero is not used.
GARAGE_STATES = ['','Open','Closed','Stopped','Opening','Closing']
# "3" corresponds to a MyQ light
if dev_type == 3:
if value == 0:
return "Off"
elif value == 1:
return "On"
return GARAGE_STATES[value]
def get_devices(self):
res = self.fetch_device_json()
# MyQ will tell us if our token is no longer valid. If so, delete the token and login again.
if (res["ReturnCode"] == "-3333"):
os.remove(self.tokenfname)
self.read_token()
res = self.fetch_device_json()
instances = []
if (res["ReturnCode"] == "0"):
devices = [d for d in res["Devices"] if d["MyQDeviceTypeId"] in [2,3,17]]
for d in devices:
dev_type = int(d["MyQDeviceTypeId"])
dev_id = d["MyQDeviceId"]
for attr in d["Attributes"]:
if (attr["AttributeDisplayName"] == "desc"):
desc = str(attr["Value"])
elif (attr["AttributeDisplayName"] in ["doorstate", "lightstate"]):
state = self.get_state(dev_type, int(attr["Value"]))
updtime = float(attr["UpdatedTime"])
timestamp = time.strftime("%a %d %b %Y %H:%M:%S", time.localtime(updtime / 1000.0))
instances.append(Device(dev_id, desc, state, timestamp))
return instances
def show_usage():
print('Usage: \n' +
' ' + sys.argv[0] + ' status\n' +
' ' + sys.argv[0] + ' [open/close/on/off] [device ID]')
sys.exit(1)
def myq_main():
if len(sys.argv) < 2:
show_usage()
elif len(sys.argv) == 2:
if sys.argv[1].lower() == 'status':
desired_state = 2
else:
show_usage()
else:
if sys.argv[1].lower() == 'close' and sys.argv[2]:
device_type = 'door'
desired_state = 0
elif sys.argv[1].lower() == 'open' and sys.argv[2]:
device_type = 'door'
desired_state = 1
elif sys.argv[1].lower() == 'off' and sys.argv[2]:
device_type = 'light'
desired_state = 0
elif sys.argv[1].lower() == 'on' and sys.argv[2]:
device_type = 'light'
desired_state = 1
else:
show_usage()
myq = MyQ()
device_instances = myq.get_devices()
if desired_state == 2:
for inst in device_instances:
print ('{} is {}. Last changed at {}'.format(inst.name, inst.state, inst.time))
LOGGER.info('%s is %s. Last changed at %s', inst.name, inst.state, inst.time)
if USE_ISY:
id, varname, init, value = isy_get_var_id(inst.name)
value = 1 if inst.state in ["Open","On"] else 0
isy_set_var_state(id, inst.name, varname, value)
else:
success = False
for inst in device_instances:
name = " ".join(sys.argv[2:])
if name.lower() == inst.name.lower():
myq.set_state(inst, device_type, desired_state)
if USE_ISY:
id, varname, init, value = isy_get_var_id(inst.name)
isy_set_var_state(id, inst.name, varname, desired_state)
print(inst.name + ' found. Setting state to ' + sys.argv[1].lower())
success = True
if not success:
print(name + ' not found in available devices.')
#####################################################
if __name__ == "__main__":
LOGGER = setup_log('myq-garage')
LOGGER.info('==================================STARTED==================================')
# Replace stdout with logging to file at INFO level
# sys.stdout = SensorLogger(LOGGER, logging.INFO)
# Replace stderr with logging to file at ERROR level
sys.stderr = MyQLogger(LOGGER, logging.ERROR)
myq_main()