def listen_state(self, function, entity=None, **kwargs): name = self.name if entity is not None and "." in entity: self._check_entity(entity) with conf.callbacks_lock: if name not in conf.callbacks: conf.callbacks[name] = {} handle = uuid.uuid4() conf.callbacks[name][handle] = { "name": name, "id": conf.objects[name]["id"], "type": "state", "function": function, "entity": entity, "kwargs": kwargs } # # In the case of a quick_start parameter, # start the clock immediately if the device is already in the new state # if "immediate" in kwargs and kwargs["immediate"] is True: if entity is not None and "new" in kwargs and "duration" in kwargs: if conf.ha_state[entity]["state"] == kwargs["new"]: exec_time = ha.get_now_ts() + int(kwargs["duration"]) kwargs["handle"] = ha.insert_schedule( name, exec_time, function, False, None, entity=entity, attribute=None, old_state=None, new_state=kwargs["new"], **kwargs ) return handle
def check_and_disapatch(name, function, entity, attribute, new_state, old_state, cold, cnew, kwargs): if attribute == "all": dispatch_worker(name, {"name": name, "id": conf.objects[name]["id"], "type": "attr", "function": function, "attribute": attribute, "entity": entity, "new_state": new_state, "old_state": old_state, "kwargs": kwargs}) else: if old_state == None: old = None else: if attribute in old_state: old = old_state[attribute] elif attribute in old_state['attributes']: old = old_state['attributes'][attribute] else: old = None if attribute in 'new_state': new = new_state[attribute] elif attribute in new_state['attributes']: new = new_state['attributes'][attribute] else: new = None if (cold == None or cold == old) and (cnew == None or cnew == new): if "duration" in kwargs: # Set a timer exec_time = ha.get_now_ts() + int(kwargs["duration"]) kwargs["handle"] = ha.insert_schedule(name, exec_time, function, False, None, entity = entity, attribute = attribute, old_state = old, new_state = new, **kwargs) else: # Do it now dispatch_worker(name, {"name": name, "id": conf.objects[name]["id"], "type": "attr", "function": function, "attribute": attribute, "entity": entity, "new_state": new, "old_state": old, "kwargs": kwargs}) else: if "handle" in kwargs: #cancel timer ha.cancel_timer(name, kwargs["handle"])
def run_in(self, callback, seconds, **kwargs): name = self.name ha.log(conf.logger, "DEBUG", "Registering run_in in {} seconds for {}".format(seconds, name)) # convert seconds to an int if possible since a common pattern is to pass this through from the config file which is a string exec_time = ha.get_now_ts() + int(seconds) handle = ha.insert_schedule(name, exec_time, callback, False, None, **kwargs) return handle
def run_at(self, callback, start, **kwargs): name = self.name now = ha.get_now() if start < now: raise ValueError("{}: run_at() Start time must be " "in the future".format(self.name)) exec_time = start.timestamp() handle = ha.insert_schedule(name, exec_time, callback, False, None, **kwargs) return handle
def run_once(self, callback, start, **kwargs): name = self.name now = ha.get_now() today = now.date() event = datetime.datetime.combine(today, start) if event < now: one_day = datetime.timedelta(days=1) event = event + one_day exec_time = event.timestamp() handle = ha.insert_schedule(name, exec_time, callback, False, None, **kwargs) return handle
def run_every(self, callback, start, interval, **kwargs): name = self.name now = ha.get_now() if start < now: raise ValueError("start cannot be in the past") ha.log( conf.logger, "DEBUG", "Registering run_every starting {} in {}s intervals for {}".format( start, interval, name ) ) exec_time = start.timestamp() handle = ha.insert_schedule(name, exec_time, callback, True, None, interval=interval, **kwargs) return handle
def _schedule_sun(self, name, type_, callback, **kwargs): event = ha.calc_sun(type_) handle = ha.insert_schedule( name, event, callback, True, type_, **kwargs ) return handle