93 lines
5.1 KiB
Python
93 lines
5.1 KiB
Python
import appdaemon.plugins.hass.hassapi as hass
|
|
from smartswitch import SmartSwitch
|
|
import ad_toolbox.smartcondition as SmartCondition
|
|
|
|
class SmartLight(SmartSwitch):
|
|
|
|
#@SmartCondition.catch_smartcondition_exception(lambda self, message: self.log_error(message,stop_app = True))
|
|
def on_initialize_smart_object(self):
|
|
self.light_brightness_pct_list = list()
|
|
self.light_brightness_pct = None
|
|
|
|
super().on_initialize_smart_object()
|
|
|
|
if "increase_brightness_events" in self.args:
|
|
self.register_event_from_yaml(self.args["increase_brightness_events"],self.on_increase_brightness_event)
|
|
if "decrease_brightness_events" in self.args:
|
|
self.register_event_from_yaml(self.args["decrease_brightness_events"],self.on_decrease_brightness_event)
|
|
|
|
if "brightness_pct_step" in self.args:
|
|
self.brightness_pct_step = self.args["brightness_pct_step"]
|
|
else: self.brightness_pct_step = 5
|
|
|
|
if "on_events_with_transition" in self.args:
|
|
for key in self.args["on_events_with_transition"]:
|
|
#self.log(f"{key}")
|
|
self.register_event_from_yaml(self.args["on_events_with_transition"][key]["events"],self.on_turn_on_with_transition,key)
|
|
|
|
if "light_brightness_pct" in self.args:
|
|
self.always_change_brightness = False
|
|
|
|
for key in self.args["light_brightness_pct"]:
|
|
if key == 'always_change_brightness':
|
|
self.always_change_brightness = bool(self.args["light_brightness_pct"][key])
|
|
else:
|
|
self.light_brightness_pct_list.append((SmartCondition.Evaluator(self,self.args["light_brightness_pct"][key],condition_name = key, on_update_cb = self.on_update_light_brightness_pct,constants = self.constants, templates_library = self.templates_library, log_callback_trigger_reason = False),key))
|
|
self. on_update_light_brightness_pct()
|
|
|
|
self.listen_state(self.on_state_change,self.entity_id)
|
|
|
|
def on_state_change(self, entity, attribute, old, new, *kwargs):
|
|
if "icon_override" in self.args:
|
|
override_data = self.args['icon_override']
|
|
|
|
def update_icon(entity_id,new_state): self.set_state(entity_id,attributes = { 'icon' : override_data['on_icon'] if new_state == 'on' else override_data['off_icon'] })
|
|
|
|
if isinstance(override_data['dest_entities'],list):
|
|
for target_entity in override_data['dest_entities']:
|
|
update_icon(target_entity,new)
|
|
else:
|
|
update_icon(override_data['dest_entities'],new)
|
|
|
|
|
|
def on_increase_brightness_event(self, event_name, data, kwargs):
|
|
if self.get_state(self.entity_id) != 'off':
|
|
self.call_service("light/turn_on", entity_id = self.entity_id, brightness_step_pct = self.brightness_pct_step)
|
|
|
|
def on_decrease_brightness_event(self, event_name, data, kwargs):
|
|
if self.get_state(self.entity_id) != 'off':
|
|
self.call_service("light/turn_on", entity_id = self.entity_id, brightness_step_pct = -self.brightness_pct_step)
|
|
|
|
def on_turn_on_with_transition(self, event_name, data, kwargs,event_category):
|
|
transition_time = self.args["on_events_with_transition"][event_category]["transition_time"]
|
|
brightness_pct = self.args["on_events_with_transition"][event_category]["brightness_pct"]
|
|
self.log(f"Turn on at {brightness_pct}% with a transition of {transition_time}s")
|
|
self.call_service("light/turn_on", entity_id = self.entity_id,brightness_pct = 1)
|
|
self.call_service("light/turn_on", entity_id = self.entity_id, transition = transition_time,brightness_pct = brightness_pct)
|
|
|
|
def switch_on(self):
|
|
if self.light_brightness_pct != None:
|
|
self.log(f"Turn on {self.entity_id} at {self.light_brightness_pct}%")
|
|
self.call_service("light/turn_on", entity_id = self.entity_id,brightness_pct = self.light_brightness_pct)
|
|
else:
|
|
super().switch_on()
|
|
|
|
def on_update_light_brightness_pct(self):
|
|
for brightness_pct_evaluator in self.light_brightness_pct_list:
|
|
if brightness_pct_evaluator[0].evaluate(False) == SmartCondition.Result.Succeeded:
|
|
if self.light_brightness_pct != brightness_pct_evaluator[1]:
|
|
brightness_pct_evaluator[0].log_callback_trigger_reason()
|
|
brightness_pct_evaluator[0].log_evaluation_result()
|
|
self.light_brightness_pct = brightness_pct_evaluator[1]
|
|
self.log_info(f"New brightness : {self.light_brightness_pct}%")
|
|
break
|
|
|
|
if (self.always_change_brightness or self.get_state(self.entity_id) == "on") and self.light_brightness_pct != None:
|
|
self.call_service("light/turn_on", entity_id = self.entity_id,brightness_pct = self.light_brightness_pct)
|
|
|
|
if int(self.light_brightness_pct) == 0: #some integration seems to not turn off the light when you set up a brightness of 0
|
|
self.log_info("Turning off")
|
|
self.call_service("light/turn_off", entity_id = self.entity_id)
|
|
|
|
|