def main(wf): sys.stderr.write('in ha_alarm_invoke, query : ' + str(wf.args) + '\n') #################################################################### # Check that we have an API key saved #################################################################### ##query = wf.args[0] password = util.getPassword(wf) url = util.getURL(wf) json = '{"entity_id": "' + wf.args[0] + '", "code" : "' + wf.args[2] + '""}' #if wf.args[1] == 'disarm' : # result = util.post_json_to_ha(url, 'alarm_control_panel/alarm_disarm', password, json); #elif wf.args[1] == 'arm_home' : # result = util.post_json_to_ha(url, 'alarm_control_panel/alarm_arm_home', password, json); #elif wf.args[1] == 'arm_away' : # result = util.post_json_to_ha(url, 'alarm_control_panel/alarm_arm_away', password, json); cmd = ['/usr/bin/python', wf.workflowfile('update_data.py')] run_in_background('update', cmd) return 0
def main(wf): parser = argparse.ArgumentParser() parser.add_argument('query', nargs='?', default=None) args = parser.parse_args(wf.args) password = util.getPassword(wf) url = util.getURL(wf) sys.stderr.write('Trigger automation : ' + args.query + '\n') result = util.post_to_ha(url, 'automation/trigger', password, args.query) return result
def main(wf): #################################################################### # Get init data #################################################################### password = util.getPassword(wf) url = util.getURL(wf) #################################################################### # Fetch all data in background if the query is empty #################################################################### sys.stderr.write('query : ' + str(wf.args) + '\n') sys.stderr.write('el : ' + str(wf.args[0]) + '\n') sys.stderr.write('action : ' + str(wf.args[1]) + '\n') pin = '' if len(wf.args) == 3: sys.stderr.write('pin : ' + str(wf.args[2]) + '\n') pin = wf.args[2] query = wf.args[0].split(' ') data = util.getData(wf, 'alarm_control_panel') item = data[wf.args[0]] res = [{ 'name': 'Enter Pin Code and press <Enter>' }] # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in res: sys.stderr.write("post : " + str(post) + '\n') wf.add_item( title=post['name'], subtitle='', valid=True, arg=wf.args[0] + ' ' + wf.args[1] + ' ' + pin, #arg='https://browall.duckdns.org:8123/api/services/automation/trigger?api_password=DrumNBass1111', icon=icon.getIcon('mdi:alarm', 'w')) # Send the results to Alfred as XML wf.send_feedback() return 0
def main(wf): sys.stderr.write("in ha_lights_invode : \n") #################################################################### # Check that we have an API key saved #################################################################### ##query = wf.args[0] sys.stderr.write('query : ' + str(wf.args) + '\n') password = util.getPassword(wf) url = util.getURL(wf) if wf.args[1] == 'off': result = util.post_to_ha(url, 'light/turn_off', password, wf.args[0]) else: json = '{"entity_id": "' + wf.args[ 0] + '", "brightness_pct" : ' + wf.args[1] + ', "transition":"5"}' result = util.post_json_to_ha(url, 'light/turn_on', password, json) cmd = ['/usr/bin/python', wf.workflowfile('update_data.py')] run_in_background('update', cmd) return 0
def main(wf): #################################################################### # Get init data #################################################################### parser = argparse.ArgumentParser() parser.add_argument('query', nargs='?', default=None) args = parser.parse_args(wf.args) password = util.getPassword(wf); url = util.getURL(wf); #################################################################### # Fetch all data in background if the query is empty #################################################################### if args.query == None: if not is_running('update'): cmd = ['/usr/bin/python', wf.workflowfile('update_data.py')] run_in_background('update', cmd) data = util.getData(wf, 'sensor') def search_key_for_post(post): """Generate a string search key for a post""" item = data[post] elements = [] elements.append(item['name']) # title of post elements.append(item['friendly_name']) elements.append(item['entity_id']) elements.append(item['unit']) elements.append(item['search_words']) if 'icon' in item.keys(): #sys.stderr.write("icon : " + str(item['icon']) + '\n') icon = item['icon'].split(':') if(len(icon) == 2): elements.append(icon[1]) return u' '.join(elements) def wrapper(): return data posts = wf.cached_data('allSensors', wrapper, max_age=1) # If script was passed a query, use it to filter posts if args.query and data: posts = wf.filter(args.query, data, key=search_key_for_post, min_score=20) if not posts: # we have no data to show, so show a warning and stop wf.add_item('No posts found', icon=ICON_WARNING) wf.send_feedback() return 0 if wf.update_available: # Add a notification to top of Script Filter results wf.add_item('New version available', 'Action this item to install the update', autocomplete='workflow:update', icon=ICON_INFO) # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in posts: #sys.stderr.write("post : " + str(post) + '\n') item = data[post]; ICON = icon.getIcon(item['icon'], 'w'); wf.add_item(title=item['friendly_name'] + ' : ' + item['state'] + ' ' + item['unit'], subtitle=item['entity_id'], valid=False, arg=item['entity_id'], icon=ICON) # Send the results to Alfred as XML wf.send_feedback() return 0;
def main(wf): #################################################################### # Get init data #################################################################### password = util.getPassword(wf) url = util.getURL(wf) #################################################################### # Fetch all data in background if the query is empty #################################################################### sys.stderr.write('query : ' + str(wf.args) + '\n') sys.stderr.write('el : ' + str(wf.args[0]) + '\n') query = wf.args[0].split(' ') data = util.getData(wf, 'alarm_control_panel') item = data[wf.args[0]] def search_key_for_post(post): elements = [] elements.append(post['name']) elements.append(post['value']) return u' '.join(elements) # If script was passed a query, use it to filter posts if item['state'] == 'disarmed': res = [{ 'name': 'Arm home', 'value': 'arm_home' }, { 'name': 'Arm away', 'value': 'arm_away' }] else: res = [{ 'name': 'Disarmed', 'value': 'disarmed' }] if not res: # we have no data to show, so show a warning and stop wf.add_item('No posts found', icon=ICON_WARNING) wf.send_feedback() return 0 # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in res: sys.stderr.write("post : " + str(post) + '\n') wf.add_item( title=post['name'], subtitle='', valid=True, arg=wf.args[0] + " " + post['value'], #arg='https://browall.duckdns.org:8123/api/services/automation/trigger?api_password=DrumNBass1111', icon=icon.getIcon('mdi:alarm', 'w')) # Send the results to Alfred as XML wf.send_feedback() return 0
def main(wf): #################################################################### # Get init data #################################################################### #parser = argparse.ArgumentParser() #parser.add_argument('query', nargs='?', default=None) #args = parser.parse_args(wf.args) password = util.getPassword(wf); url = util.getURL(wf); #################################################################### # Fetch all data in background if the query is empty #################################################################### sys.stderr.write('query : '+ str(wf.args) + '\n') sys.stderr.write('el : '+ str(wf.args[0]) + '\n') query = wf.args[0].split(' ') sys.stderr.write('in : '+ '\n') level = ''; if len(wf.args) == 2 : level = wf.args[1] def search_key_for_post(post): elements = [] elements.append(post['name']) elements.append(post['value']) return u' '.join(elements) def wrapper(): data = [{'name' : 'Off', 'value' : 'off'}, {'name' : 'max', 'value' : '100'}, {'name' : '90%', 'value' : '90'}, {'name' : '80%', 'value' : '80'}, {'name' : '70%', 'value' : '70'}, {'name' : '60%', 'value' : '60'}, {'name' : '50%', 'value' : '50'}, {'name' : '40%', 'value' : '40'}, {'name' : '30%', 'value' : '30'}, {'name' : '20%', 'value' : '20'}, {'name' : '10%', 'value' : '10'}, {'name' : 'min', 'value' : '0'}] return data posts = wf.cached_data('allLightLevels', wrapper, max_age=60) # If script was passed a query, use it to filter posts if level: res = wf.filter(level, posts, key=search_key_for_post, min_score=20) else : res = posts; if not res: # we have no data to show, so show a warning and stop wf.add_item('No posts found', icon=ICON_WARNING) wf.send_feedback() return 0 # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in res: sys.stderr.write("post : " + str(post) + '\n') if post['value'] == 'off' : v_icon = icon.getIcon('light-off','w') else : v_icon = icon.getIcon('light-on','w') wf.add_item(title=post['name'], subtitle='', valid=True, arg=wf.args[0] + " " + post['value'], icon=v_icon) # Send the results to Alfred as XML wf.send_feedback() return 0;
def main(wf): #################################################################### # Get init data #################################################################### parser = argparse.ArgumentParser() parser.add_argument('query', nargs='?', default=None) args = parser.parse_args(wf.args) password = util.getPassword(wf); url = util.getURL(wf); #################################################################### # Fetch all data in background if the query is empty #################################################################### if args.query == None: if not is_running('update'): cmd = ['/usr/bin/python', wf.workflowfile('update_data.py')] run_in_background('update', cmd) data = util.getData(wf, 'alarm_control_panel') def search_key_for_post(post): """Generate a string search key for a post""" item = data[post] elements = [] elements.append(item['friendly_name']) elements.append(item['entity_id']) return u' '.join(elements) def wrapper(): return data posts = wf.cached_data('allAAlarms', wrapper, max_age=1) # If script was passed a query, use it to filter posts if args.query and data: posts = wf.filter(args.query, data, key=search_key_for_post, min_score=20) if not posts: # we have no data to show, so show a warning and stop wf.add_item('No posts found', icon=ICON_WARNING) wf.send_feedback() return 0 if wf.update_available: # Add a notification to top of Script Filter results wf.add_item('New version available', 'Action this item to install the update', autocomplete='workflow:update', icon=ICON_INFO) # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in posts: sys.stderr.write("post : " + str(post) + '\n') item = data[post]; subtitle = '<Enter> to select alarm' wf.add_item(title=item['friendly_name'], subtitle=subtitle, valid=True, arg=item['entity_id'], #arg='https://browall.duckdns.org:8123/api/services/automation/trigger?api_password=DrumNBass1111', icon=icon.getIcon('mdi:alarm','w')) # Send the results to Alfred as XML wf.send_feedback() return 0;
def main(wf): #################################################################### # Get init data #################################################################### parser = argparse.ArgumentParser() parser.add_argument('query', nargs='?', default=None) args = parser.parse_args(wf.args) password = util.getPassword(wf) url = util.getURL(wf) #################################################################### # Fetch all data in background if the query is empty #################################################################### if args.query == None: if not is_running('update'): cmd = ['/usr/bin/python', wf.workflowfile('update_data.py')] run_in_background('update', cmd) data = util.getData(wf, 'automation') def search_key_for_post(post): """Generate a string search key for a post""" item = data[post] elements = [] elements.append(item['name']) # title of post elements.append(item['friendly_name']) elements.append(item['entity_id']) return u' '.join(elements) def wrapper(): return data posts = wf.cached_data('allAutomations', wrapper, max_age=1) # If script was passed a query, use it to filter posts if args.query and data: posts = wf.filter(args.query, data, key=search_key_for_post, min_score=20) if not posts: # we have no data to show, so show a warning and stop wf.add_item('No posts found', icon=ICON_WARNING) wf.send_feedback() return 0 # Loop through the returned posts and add an item for each to # the list of results for Alfred #for post in posts: for post in posts: sys.stderr.write("post : " + str(post) + '\n') item = data[post] subtitle = '<Enter> to trigger automation' wf.add_item(title=item['friendly_name'], subtitle=subtitle, valid=True, arg=item['entity_id'], icon=icon.getIcon('mdi:home', 'w')) # Send the results to Alfred as XML wf.send_feedback() return 0
def updateData(wf): log.debug('Started updateing data') #get URL and password password = util.getPassword(wf) url = util.getURL(wf) url = url + '/api/states?api_password='******'json') r = web.get(url) # throw an error if request failed # Workflow will catch this and show it to the user r.raise_for_status() # Parse the JSON returned by pinboard and extract the posts #data = json.loads(r.text.decode(r.encoding)) data = r.json() #sys.stderr.write('Load entities result : '+ json.dumps(data) + '\n') #Rearange data, so all typs are grouped. main = dict() for item in data: #sys.stderr.write('Entity : '+ json.dumps(item) + '\n') entity_id = item['entity_id'] ent_name_split = entity_id.split('.') ent_type = ent_name_split[0] name = ent_name_split[1] search_words = '' attribute = item['attributes'] icon = '' if 'icon' in attribute.keys(): icon = attribute['icon'] unit = '' if 'unit_of_measurement' in attribute.keys(): unit = attribute['unit_of_measurement'] state = '' if 'state' in item: state = item['state'] latitude = '' if 'latitude' in attribute.keys(): latitude = str(attribute['latitude']) sys.stderr.write("latitude : " + latitude + '\n') longitude = '' if 'longitude' in attribute.keys(): longitude = str(attribute['longitude']) sys.stderr.write("longitude : " + longitude + '\n') friendly_name = item['attributes']['friendly_name'] search_words = getSearchWords(icon, unit, state, friendly_name) ent = { entity_id: { 'type': ent_type, 'name': name, 'entity_id': entity_id, 'icon': icon, 'friendly_name': friendly_name, 'unit': unit, 'state': state, 'longitude': longitude, 'latitude': latitude, 'search_words': search_words } } if ent_type in main.keys(): main[ent_type].update(ent) else: main[ent_type] = dict(ent) if 'temperature' in item['attributes'].keys(): main = copyItemAndAdd(main, ent_type, entity_id, name, friendly_name, 'mdi:thermometer', '', str(item['attributes']['temperature']), 'temperature', longitude, latitude) if 'light_level' in item['attributes'].keys(): main = copyItemAndAdd(main, ent_type, entity_id, name, friendly_name, 'light-on', 'light level', str(item['attributes']['light_level']), 'light_level', longitude, latitude) if 'lux' in item['attributes'].keys(): main = copyItemAndAdd(main, ent_type, entity_id, name, friendly_name, 'light-on', 'lux', str(item['attributes']['lux']), 'lux', longitude, latitude) if 'battery' in item['attributes'].keys(): main = copyItemAndAdd(main, ent_type, entity_id, name, friendly_name, 'mdi:battery', '%', str(item['attributes']['battery']), 'battery', longitude, latitude) #go thur groups and store structure for item in main: wf.store_data(item, main[item]) sys.stderr.write("item : " + str(item) + '\n') return 0