How do I figure out which object called ‘run_daily_callback’?
I want to send the message based on the object that was instantiated. For an example, if it’s ‘bedtime_lights_4’ then I want to send a message to my phone (via IFTTT) that all lights were turned off.
def run_daily_callback(self, kwargs):
self.log('TurnOffLightsAtBedtime: Daily Callback Function triggered')`
You can add a parameter to the app to distinguish individual apps. I was looking for a way to find the name of the current app, but I don’t think there is one.
It depends on exactly what is needed. If you needed access to the names of the entities, you need to access them through args.
However, if all you need is a message when the last one is turned off, you can declare the last one with class TurnOffLightsAtBedtimeAndNotify.
The advantage of this approach is that if you change which one is last, you can just change the configuration in apps.yaml, and there would be no changes to the code.
@gpbenton I was looking general way to identify the object and don’t want to add another parameter to identify the object. I liked your inherited class idea. I may use that in the future.
def run_daily_callback(self, kwargs):
# enable only to get addl info
# self.log(str(self.get_scheduler_entries()))
self.log('TurnOffLightsAtBedtime: Daily Callback Function triggered : ' + self.name)
msg = ""
for entity_id in self.args["entities"]:
device, entity = self.split_entity(entity_id)
# self.log("EntityId : {} - Device : {} - Entity: {}".format(entity_id, device, entity))
if device != 'scene':
# if the lights are on then call turn_off
if self.get_state(entity_id).lower() == 'on':
self.turn_off(entity_id)
if len(self.args["entities"]) > 1:
msg += self.friendly_name(entity_id) + ", "
else:
msg += self.friendly_name(entity_id)
# self.log(msg)
else:
self.log("EntityId : {} - State : {}".format(self.friendly_name(entity_id), self.get_state(entity_id).lower()))
else:
# scene returns 'scening' for get_state
self.turn_off(entity_id)
msg += self.friendly_name(entity_id)
if msg != "":
# self.log(msg)
# if this is last one before bed time then send custom message
if self.name == 'bedtime_lights_4':
msg = "Good night. All lights turned off. \n Check the doors. Sleep tight \n"
self.utils.send_notification_msg(msg, announce=True)
else:
msg += " turned OFF at " + datetime.now().strftime('%H:%M:%S')
self.utils.send_notification_msg(msg)
Notification message call:
def send_notification_msg(self, msg, notify=True, announce=False, frontend=False):
if notify:
self.call_service('ifttt/trigger', event='My_Home', value1=msg)
if announce:
self.call_service('tts/google_say', entity_id='media_player.living_room_home', message=msg)
if frontend:
self.call_service('persistent_notification/create',
title="Attention", message=msg+ " - " + self.get_timenow())