def decide(self, people, message, title=None, all_channels=False, volume=None): #Determine who needs push notifications persons = people.split(',') home = [] not_home = [] if all_channels: home = persons not_home = persons else: for person in persons: switch = dom.get_device('presence', person + 'home') if switch and switch.is_on(): home.append(person) else: not_home.append(person) #Verbal announcement if needed if home: print 'Using speech' thread = Thread(target=self._speak, args=['all', message, volume]) thread.start() if not_home: print 'Sending messages to: ' + ', '.join(not_home) self._message(','.join(not_home), message, title) return "Notifications sent"
def set(self, room, name, state='on'): switch = dom.get_device('light', room + name) switch.set_state(state) if state.isdigit(): return name.capitalize() + " light in " + room.capitalize() + " set to level " + str(state) else: return name.capitalize() + " light in " + room.capitalize() + " switched " + str(state)
def set(self, name, state='on'): switch = dom.get_device('switch', name) switch.set_state(state) if state.isdigit(): return switch.name.capitalize() + " set to level " + str(state) else: return switch.name.capitalize() + " switched " + str(state)
def on(self): global doorbell_active print "Processing doorbell..." doorbell = dom.get_device('mode','doorbell') if doorbell_active: print "Doorbell already active" return "Doorbell already active" doorbell_active = True day = dom.get_device('mode','day') key = 'day' if day.is_on() else 'night' print 'Using ' + key + ' configuration' try: print 'Ringing doorbell...' sonos.interrupt_with_uri(sonos.get_speakers(speakers[key]['speakers']), local, uri, speakers[key]['volume'], 'Doorbell', modify_zones=False) print "Finished" except: print 'Exception encountered during doorbell playback' doorbell_active = False return "Activated doorbell"
def set(self, name, zone, action='arrive'): switch = dom.get_device('presence', name + zone) if not switch: return 'Not tracking ' + name.title() + ' in ' + zone.title() if action == 'arrive': switch.on() return name.title() + ' recorded as arriving in ' + zone.title() elif action == 'leave': switch.off() return name.title() + ' recorded as leaving from ' + zone.title() else: raise Exception('Unknown action: ' + action)
def process(self, name, zone, action='arrive'): global recent_changes event = (name + "." + action + "." + zone).lower() if event not in notify_rules.keys(): return 'No notifications setup for event: ' + event #Event suppression if event in recent_changes.keys(): diff = datetime.datetime.now() - recent_changes[event] if diff.total_seconds() < suppress_time: print "Suppressing event as seen recently" return "Suppressing event as seen recently" recent_changes[event] = datetime.datetime.now() output = '<br>Input: ' + event qualify = True for notification in notify_rules[event]: if 'message' not in notification: output += '<br>Error: No message configured' qualify = False if qualify and 'person' not in notification: output += '<br>Error: No person configured' qualify = False if qualify and 'period' in notification: if not in_period(notification['period']): output += '<br>Not notifying : ' + notification['person'] + ' (Period <> ' + notification['period'] + ')' qualify = False if qualify and 'conditions' in notification: for condition in notification['conditions']: con_switch = dom.get_device('switch', condition['switch']) if con_switch and con_switch.state.lower() != condition['state'].lower(): qualify = False output += '<br>Not notifying: ' + notification['person'] + ' (' + con_switch.name + ' <> ' + condition['state'] + ')' break if qualify: output += '<br>Notifying: ' + notification['person'] if 'link' in notification: pb.send(notification['message'], notification['link'], notification['person'], 'url') else: pb.send(notification['message'], '', notification['person'], 'text') return output
def get(self, room, name): switch = dom.get_device('light', room + name) if switch.state.lower() == "on" or switch.state.lower() == "off": return name.capitalize() + " light in " + room.capitalize() + " is " + switch.state.lower() + " at level " + str(switch.level) else: return name.capitalize() + " light in " + room.capitalize() + " is on at level " + str(switch.level)
def process(self, type, name, state): type = type.lower() name = name.lower() state = state.lower() key = type + '-->' + name + '-->' + state print "Processing switch: " + key output = '<br>Input: ' + key try: if type not in switch_rules: return 'No rules for type in: ' + key if name not in switch_rules[type]: return 'No rules for name in: ' + key if state not in switch_rules[type][name]: return 'No rules for state in: ' + key changes = switch_rules[type][name][state] print "Found " + str(len(changes)) + " items to process" for id, item in enumerate(changes): print "Processing item: " + str(id) #Check conditions qualify = True if 'conditions' in item: for condition in item['conditions']: if 'type' not in condition: output += '<br>Error: No condition type specified - skipping' break if condition['type'] == 'period': if 'expression' not in condition: output += '<br>Error: No expression for condition type period - skipping' break if not in_period(condition['expression']): output += '<br>No change: ' + key + '[' + str(id) + ']: Period <> ' + condition['expression'] qualify = False break else: if 'name' not in condition: output += '<br>Error: No device name set for condition - skipping' break if 'state' not in condition: output += '<br>Error: No state set for condition - skipping' break con_item = dom.get_device(condition['type'], condition['name']) if con_item and con_item.state.lower() != condition['state'].lower(): output += '<br>No Change: ' + key + '[' + str(id) + ']: ' + condition['type'] + '-->' + condition['name'] + ' <> ' + condition['state'] qualify = False break if qualify: output += '<br>All conditions met for: ' + key + '[' + str(id) + ']' if qualify: if 'change' in item: change = item['change'] if 'type' not in change: output += '<br>Error: No type set for change - skipping' continue if 'name' not in change: output += '<br>Error: No name set for change - skipping' continue if 'state' not in change: output += '<br>Error: No state set for change - skipping' continue device = dom.get_device(change['type'], change['name']) if device: device.set_state(change['state']) output += '<br>Change: ' + change['name'] + '-->' + change['state'] else: output += '<br>No change: ' + key + '[' + str(id) + ']: Unknown switch (' + change['name'] + ')' if 'notify' in item: notify = item['notify'] if 'type' not in notify: output += '<br>Error: No type set for notify - skipping' continue if 'message' not in notify: output += '<br>Error: No message set for notify - skipping' continue if 'recipients' not in notify: output += '<br>Error: No receipients set for notify - skipping' continue global recent_changes #Suppression if key in recent_changes: diff = datetime.datetime.now() - recent_changes[key] if diff.total_seconds() < presence_suppression_time: output += "Suppressing notify for repeated presence event: " + key continue recent_changes[key] = datetime.datetime.now() volume = change['volume'] if 'volume' in notify else None recipients = ','.join(notify['recipients']) module = notify_module.Notify() if notify['type'] == 'speak': module.message(recipients, notify['message'], volume) elif notify['type'] == 'alert': module.alert(notify['message']) elif notify['type'] == 'decide': module.notify(recipients, notify['message'], volume) elif notify['type'] == 'push': if 'link' in notify: module.link(recipients, notify['link'], notify['message']) else: module.message(recipients, notify['message']) output += '<br>Notify: ' + notify['type'] + ' to ' + recipients if 'function' in item: function = item['function'] if 'module' not in function: output += '<br>Error: No module set for function - skipping' continue if 'method' not in function: output += '<br>Error: No method set for function - skipping' continue module = False for plugin_name, module in inspect.getmembers(sys.modules['plugins'], inspect.ismodule): if plugin_name == function['module']: for class_name, obj in inspect.getmembers(module, inspect.isclass): module = obj() break if module: print module try: if 'params' in function: response = getattr(module, function['method'])(**function['params']) else: response = getattr(module, function['method']) output += '<br>Called function and got response: ' + response except Exception as e: print e traceback.print_exc() output += '<br>Error: Function call failed' else: output += '<br>Error: Could not locate module/function: ' + function['module'] + "." + function['method'] except Exception as e: print e traceback.print_exc() print output return output
def get(self, name): switch = dom.get_device('switch', name) if switch.state.lower() == "on" or switch.state.lower() == "off": return switch.name.capitalize() + " is " + switch.state.lower() + " at level " + str(switch.level) else: return switch.name.capitalize() + " is on at level " + str(switch.level)
def sync(self, reason='activity'): nest_away = nest.is_away() dom_away_switch = dom.get_device('mode','away') dom_occupied_switch = dom.get_device('mode','occupied') dom_guest_switch = dom.get_device('mode','guest') if nest_away == dom_away_switch.is_on(): return 'Already synced, nothing to do' message = 'Heating.sync - unknown outcome - check code' if reason == 'manual': if nest_away: #Nest is away and Domoticz is home nest.set_home() message = 'Away mode disabled (Manual)' else: #Nest is home and Domoticz is away nest.set_away() if dom_guest_switch.is_on(): dom_guest_switch.off() message = 'Away mode enabled (Manual)' #elif reason == 'geofence': # if nest_away: #Nest is away and Domoticz is home # if dom_occupied_switch.is_on(): # dom_julian_switch = dom.get_device('presence','julianhome') # dom_sarah_switch = dom.get_device('presence','sarahhome') # people_present = [] # if dom_guest_switch.is_on(): # people_present.append('Guest') # if dom_julian_switch.is_on(): # people_present.append('Julian') # if dom_sarah_switch.is_on(): # people_present.append('Sarah') # nest.set_home() # message = 'Away mode suppressed (' + ', '.join(people_present) + ')' # else: # dom_away_switch.on() # message = 'Away mode enabled (no activity)' # # nest.set_home() # dom_away_switch.off() # message = 'Away mode disabled (Someone nearby)' # else: #Nest is home and Domoticz is away # nest.set_away() # dom_away_switch.on() # if dom_guest_switch.is_on(): # dom_guest_switch.off() # message = 'Away mode enabled (and guest mode disabled)' # else: # message = 'Away mode enabled (No-one nearby)' else: #From nest (activity) if nest_away: #Nest is away and Domoticz is home if dom_occupied_switch.is_on(): dom_julian_switch = dom.get_device('presence','julianhome') dom_sarah_switch = dom.get_device('presence','sarahhome') people_present = [] if dom_guest_switch.is_on(): people_present.append('Guest') if dom_julian_switch.is_on(): people_present.append('Julian') if dom_sarah_switch.is_on(): people_present.append('Sarah') nest.set_home() message = 'Away mode suppressed (' + ', '.join(people_present) + ')' else: dom_away_switch.on() message = 'Away mode enabled (No activity)' else: #Nest is home and Domoticz is away dom_away_switch.off() message = 'Away mode disabled (Activity)' pb.send(message,'launch://?url=[action:' + str(lcp_away_action) + ']',notify_person,'url') return message