예제 #1
0
    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 = utils.get_now_ts() + int(kwargs["duration"])
                    kwargs["handle"] = utils.insert_schedule(
                        name, exec_time, function, False, None,
                        entity=entity,
                        attribute=None,
                        old_state=None,
                        new_state=kwargs["new"], **kwargs
                )

        return handle
예제 #2
0
 def run_in(self, callback, seconds, **kwargs):
     name = self.name
     utils.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 = utils.get_now_ts() + int(seconds)
     handle = utils.insert_schedule(name, exec_time, callback, False, None,
                                    **kwargs)
     return handle
예제 #3
0
def do_every(period, f):
    t = math.floor(utils.get_now_ts())
    count = 0
    t_ = math.floor(time.time())
    while not conf.stopping:
        count += 1
        delay = max(t_ + count * period - time.time(), 0)
        yield from asyncio.sleep(delay)
        t += conf.interval
        r = yield from f(t)
        if r is not None and r != t:
            #print("r: {}, t: {}".format(r,t))
            t = r
            t_ = r
            count = 0
예제 #4
0
def is_dst():
    return bool(time.localtime(utils.get_now_ts()).tm_isdst)
예제 #5
0
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 is 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 new_state is None:
            new = None
        else:
            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 is None or cold == old) and (cnew is None or cnew == new):
            if "duration" in kwargs:
                # Set a timer
                exec_time = utils.get_now_ts() + int(kwargs["duration"])
                kwargs["handle"] = utils.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
                utils.cancel_timer(name, kwargs["handle"])