Files
2026-08-10 19:42:19 +00:00

76 lines
2.5 KiB
Python

import appdaemon.plugins.hass.hassapi as hass
import globals
#
# Turn on entry light when the lock is opened (coming home).
# Listens for 'door_opened' event, turns on control_entity and keeps it on
# for 'timeout' seconds after the last event.
#
# Args:
# control_entity = entity that will be turned on (example: light.light_hall_long)
# timeout = seconds to keep the light on after the last door opening
# events (optionally) = list of event names to listen to (default: ['door_opened'])
# constraint = you can define constraint (input_boolean), that will turn on and off this app.
#
# Release Notes
#
# Version 1.0:
# Initial Version
class LightControlEntry(hass.Hass):
timer = None
def initialize(self):
if not globals.check_args(self, ["control_entity", "timeout"]):
return
self.listen_event_handle_list = []
for event in self.args.get('events', ['door_opened']):
self.listen_event_handle_list.append(self.listen_event(self.event_trigger, event))
def event_trigger(self, event_name, data, kwargs):
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
#turn on control entity and keep it on for 'timeout' seconds after last event.
self.control_entity_on()
self.run_timer()
def terminate(self):
if self.listen_event_handle_list != None:
for listen_event_handle in self.listen_event_handle_list:
self.cancel_listen_event(listen_event_handle)
############ TIMER ########################
def run_timer(self):
if self.timer != None:
self.stop_timer()
self.timer = self.run_in(self.control_entity_off, self.args['timeout'])
def control_entity_on(self):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
entity = self.args['control_entity']
state = self.get_state(entity)
self.turn_on(entity)
self.log("Light on ({}).".format(entity))
def control_entity_off(self, kwargs):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
entity = self.args['control_entity']
state = self.get_state(entity)
self.turn_off(entity)
self.log("Light off ({}).".format(entity))
self.stop_timer()
def stop_timer(self):
if (self.timer == None):
return
self.log('stopping timer.')
self.cancel_timer(self.timer)
self.timer = None