Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
entity = values[0]
state = values[1]
else:
entity = value
state = "on"
if entity in conf.ha_state and conf.ha_state[entity]["state"] != state:
unconstrained = False
if key == "constrain_input_select":
values = value.split(",")
entity = values.pop(0)
if entity in conf.ha_state and conf.ha_state[entity]["state"] not in values:
unconstrained = False
if key == "constrain_presence":
if value == "everyone" and not ha.everyone_home():
unconstrained = False
elif value == "anyone" and not ha.anyone_home():
unconstrained = False
elif value == "noone" and not ha.noone_home():
unconstrained = False
if key == "constrain_days":
if today_is_constrained(value):
unconstrained = False
return unconstrained
def today_is_constrained(days):
day = ha.get_now().weekday()
daylist = [ha.day_of_week(day) for day in days.split(",")]
if day in daylist:
return False
return True
# 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 dump_callbacks():
if conf.callbacks == {}:
ha.log(conf.logger, "INFO", "No callbacks")
else:
ha.log(
conf.logger, "INFO",
"--------------------------------------------------"
)
ha.log(conf.logger, "INFO", "Callbacks")
ha.log(
conf.logger, "INFO",
"--------------------------------------------------"
)
for name in conf.callbacks.keys():
ha.log(conf.logger, "INFO", "{}:".format(name))
for uuid_ in conf.callbacks[name]:
ha.log(
conf.logger, "INFO",
" {} = {}".format(uuid_, conf.callbacks[name][uuid_])
)
ha.log(
def check_time_constraint(args, name):
unconstrained = True
if "constrain_start_time" in args or "constrain_end_time" in args:
if "constrain_start_time" not in args:
start_time = "00:00:00"
else:
start_time = args["constrain_start_time"]
if "constrain_end_time" not in args:
end_time = "23:59:59"
else:
end_time = args["constrain_end_time"]
if not ha.now_is_between(start_time, end_time, name):
unconstrained = False
return unconstrained
conf.logger, "DEBUG",
"Process sun: {}, next sunrise: {}, next sunset: {}".format(
action, conf.sun["next_rising"], conf.sun["next_setting"]
)
)
with conf.schedule_lock:
for name in conf.schedule.keys():
for entry in sorted(
conf.schedule[name].keys(),
key=lambda uuid_: conf.schedule[name][uuid_]["timestamp"]
):
schedule = conf.schedule[name][entry]
if schedule["type"] == action and "inactive" in schedule:
del schedule["inactive"]
c_offset = ha.get_offset(schedule)
schedule["timestamp"] = ha.calc_sun(action) + c_offset
schedule["offset"] = c_offset
conf.logger, "INFO",
"AppDaemon Version {} starting".format(__version__)
)
if not conf.apps:
ha.log(
conf.logger, "INFO",
"Apps are disabled"
)
# Check with HA to get various info
ha_config = None
while ha_config is None:
try:
ha_config = ha.get_ha_config()
except:
ha.log(
conf.logger, "WARNING",
"Unable to connect to Home Assistant, retrying in 5 seconds"
)
if conf.loglevel == "DEBUG":
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", "Unexpected error:")
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", traceback.format_exc())
ha.log(conf.logger, "WARNING", '-' * 60)
time.sleep(5)
conf.version = parse_version(ha_config["version"])
conf.ha_config = ha_config
def get_ha_state():
ha.log(conf.logger, "DEBUG", "Refreshing HA state")
states = ha.get_ha_state()
with conf.ha_state_lock:
for state in states:
conf.ha_state[state["entity_id"]] = state
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 = 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,
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 is not None and r != t:
t = math.floor(r)