def refresh_devices(): stop() tokenFile = open("token.txt") token = tokenFile.read() tokenFile.close() devicesFile = open("devices.txt", "w") with open("endpoints.txt", 'r') as fh: for endpoint in fh.readlines(): if endpoint.__len__() > 1: ### collect devices for each endpoint for deviceType in ("switches", "locks"): endpoint = endpoint.strip() url = "{endpoint}/{deviceType}".format(protocol=PROTOCOL, hostname=HOSTNAME, endpoint=endpoint, deviceType=deviceType) req = urllib2.Request(url) req.add_header('Authorization', "Bearer %s" % token) response = urllib2.urlopen(req) the_page = response.read() jsonData = json.loads(the_page) for device in jsonData: deviceKey = "{endpoint}/{deviceType}/{deviceId}".format(endpoint=endpoint, deviceType=deviceType, deviceId=device['id']) deviceValue = device['name'] if len(device['label']) == 0 else device['label'] deviceCache = "{key}|{value}\n".format(key=deviceKey, value=deviceValue) devicesFile.write(deviceCache) devicesFile.close() return "Your SmartThings device cache has been updated"
def login_command(query=""): stop() feedback = Feedback() if os.path.isfile("token.txt"): tokenFile = open("token.txt") token = tokenFile.read() tokenFile.close() if token.__len__() > 0: feedback.addItem(title="You are already authenticated with SmartThings") return feedback else: feedback.addItem(title="Authenticate with SmartThings", subtitle="You will be forwarded to https://www.smartthings.com") return feedback
def refresh_devices(): stop() tokenFile = open("token.txt") token = tokenFile.read() tokenFile.close() devicesFile = open("devices.txt", "w") with open("endpoints.txt", "r") as fh: for endpoint in fh.readlines(): if endpoint.__len__() > 1: ### collect devices for each endpoint for deviceType in ("switches", "locks"): endpoint = endpoint.strip() url = "{protocol}://{hostname}{endpoint}/{deviceType}".format( protocol=PROTOCOL, hostname=HOSTNAME, endpoint=endpoint, deviceType=deviceType ) req = urllib2.Request(url) req.add_header("Authorization", "Bearer %s" % token) response = urllib2.urlopen(req) the_page = response.read() jsonData = json.loads(the_page) for device in jsonData: deviceKey = "{endpoint}/{deviceType}/{deviceId}".format( endpoint=endpoint, deviceType=deviceType, deviceId=device["id"] ) deviceValue = device["name"] if len(device["label"]) == 0 else device["label"] deviceCache = "{key}:{value}\n".format(key=deviceKey, value=deviceValue) devicesFile.write(deviceCache) devicesFile.close() return "Your SmartThings device cache has been updated"
def device_collector(query=""): stop() feedback = Feedback() args = string.split(query, " ") command = args[-1] if len(command) < 1: command = args[-2] switchCommandsFile = open("commands.switches.txt") switchCommands = switchCommandsFile.read() switchCommandsFile.close() lockCommandsFile = open("commands.locks.txt") lockCommands = lockCommandsFile.read() lockCommandsFile.close() executable = False if command in switchCommands or command in lockCommands: if len(command) > 0: executable = True else: command = '' deviceFilter = '' for currentArg in args: if currentArg != command: deviceFilter += ' {ca}'.format(ca=currentArg) deviceFilter = deviceFilter.strip() tokenFile = open("token.txt") token = tokenFile.read() tokenFile.close() with open("endpoints.txt", 'r') as fh: try: deviceFile = open("devices.txt") for deviceDataString in deviceFile.readlines(): deviceData = string.split(deviceDataString, ":") deviceEndpoint = deviceData[0].strip() deviceLabel = deviceData[1].strip() if len(deviceFilter) > 0: if not deviceFilter.lower() in deviceLabel.lower(): continue arg = "{deviceEndpoint}.{command}".format( deviceEndpoint=deviceEndpoint, command=command) title = deviceLabel if command.__len__() > 1: if command in switchCommands: title = "Turn {command} {device}".format( command=command, device=deviceLabel) else: title = "{command} {device}".format( command=command.capitalize(), device=deviceLabel) feedback.addItem(title=title, subtitle=deviceLabel, arg=arg, valid=executable, autocomplete=' {l}'.format(l=deviceLabel)) deviceFile.close() except IOError: print "Uh oh" return feedback
def device_collector(query=""): stop() feedback = Feedback() args = string.split(query, " ") command = args[-1] if len(command) < 1: command = args[-2] switchCommandsFile = open("commands.switches.txt") switchCommands = switchCommandsFile.read() switchCommandsFile.close() lockCommandsFile = open("commands.locks.txt") lockCommands = lockCommandsFile.read() lockCommandsFile.close() executable = False if command in switchCommands or command in lockCommands: if len(command) > 0: executable = True else: command = '' deviceFilter = '' for currentArg in args: if currentArg != command: deviceFilter += ' {ca}'.format(ca=currentArg) deviceFilter = deviceFilter.strip() tokenFile = open("token.txt") token = tokenFile.read() tokenFile.close() with open("endpoints.txt", 'r') as fh: try: deviceFile = open("devices.txt") for deviceDataString in deviceFile.readlines(): deviceData = string.split(deviceDataString, ":") deviceEndpoint = deviceData[0].strip() deviceLabel = deviceData[1].strip() if len(deviceFilter) > 0: if not deviceFilter.lower() in deviceLabel.lower(): continue arg = "{deviceEndpoint}.{command}".format(deviceEndpoint=deviceEndpoint, command=command) title = deviceLabel if command.__len__() > 1: if command in switchCommands: title = "Turn {command} {device}".format(command=command, device=deviceLabel) else: title = "{command} {device}".format(command=command.capitalize(), device=deviceLabel) feedback.addItem(title=title, subtitle=deviceLabel, arg=arg, valid=executable, autocomplete=' {l}'.format(l=deviceLabel)) deviceFile.close() except IOError: print "Uh oh" return feedback