Пример #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_at(self, callback, start, **kwargs):
     name = self.name
     now = utils.get_now()
     if start < now:
         raise ValueError("{}: run_at() Start time must be "
                          "in the future".format(self.name))
     exec_time = start.timestamp()
     handle = utils.insert_schedule(name, exec_time, callback, False, None,
                                    **kwargs)
     return handle
Пример #3
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
Пример #4
0
 def run_once(self, callback, start, **kwargs):
     name = self.name
     now = utils.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 = utils.insert_schedule(name, exec_time, callback, False, None,
                                    **kwargs)
     return handle
Пример #5
0
 def run_every(self, callback, start, interval, **kwargs):
     name = self.name
     now = utils.get_now()
     if start < now:
         raise ValueError("start cannot be in the past")
     utils.log(
         conf.logger, "DEBUG",
         "Registering run_every starting {} in {}s intervals for {}".format(
              start, interval, name
         )
     )
     exec_time = start.timestamp()
     handle = utils.insert_schedule(name, exec_time, callback, True, None,
                                 interval=interval, **kwargs)
     return handle
Пример #6
0
 def _schedule_sun(self, name, type_, callback, **kwargs):
     event = utils.calc_sun(type_)
     handle = utils.insert_schedule(
         name, event, callback, True, type_, **kwargs
     )
     return handle
Пример #7
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"])