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 do_every(period,f): def g_tick(): t = math.floor(time.time()) count = 0 while True: count += 1 yield max(t + count*period - time.time(),0) g = g_tick() t = math.floor(ha.get_now_ts()) while True: time.sleep(next(g)) t += conf.interval r = f(t) if r != t: t = math.floor(r)
def is_dst( ): return bool(time.localtime(ha.get_now_ts()).tm_isdst)