Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
"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
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
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
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