Multiple objects of same class

Hi,

I have four objects defined for one class and they are called at different times. It turns off different lights in the house based on trigger time.

timenew = self.parse_time(self.args["trigger_time"])
self.log('Time to trigger - argument : {}'.format(timenew))
self.run_daily(self.run_daily_callback, timenew)

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')`

apps.yaml:

bedtime_lights_1:
  module: bedtimelights
  class: TurnOffLightsAtBedtime
  constrain_input_boolean: input_boolean.bedtime_lights
  # trigger this exactly at this time
  trigger_time: "21:30:00"
  entities:
    - switch.ge_12722_onoff_relay_switch_switch_4
bedtime_lights_2:
  module: bedtimelights
  class: TurnOffLightsAtBedtime
  constrain_input_boolean: input_boolean.bedtime_lights
  trigger_time: "22:00:00"
  entities:
    - switch.switch_three
bedtime_lights_3:
  module: bedtimelights
  class: TurnOffLightsAtBedtime
  constrain_input_boolean: input_boolean.bedtime_lights
  trigger_time: "23:15:00" 
  entities:
    - switch.switch_five
bedtime_lights_4:
  module: bedtimelights
  class: TurnOffLightsAtBedtime
  constrain_input_boolean: input_boolean.bedtime_lights,on
  trigger_time: "23:35:00"
  entities:
    - scene.turn_off_lights_at_bedtime

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.

I think the name is stored in self.name offhand without checking

EDIT: Confirmed!

1 Like

you could use self.args[“entities”] in your log.

Or alternatively, you could use an inherited class

class TurnOffLightsAtBedtime(appapi.AppDaemon):
    def initialize(self):
        timenew = self.parse_time(self.args["trigger_time"])
        self.log('Time to trigger - argument : {}'.format(timenew))
        self.run_daily(self.run_daily_callback, timenew)

    def run_daily_callback(self, kwargs):
        self.log('TurnOffLightsAtBedtime: Daily Callback Function triggered')

class TurnOffLightsAtBedtimeAndNotify(TurnOffLightsAtBedtime):
    def run_daily_callback(self, kwargs):
        super.run_daily_callback()
        self.log("And now notify")

1 Like

then you still wouldnt have the name from the different entities :wink:
he has entity names in his apps.yaml that he want to have in his notify.

appnames or extra parameters are there only unnaccesary part that he doesnt need.

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.

i would do that with args also :wink:

in the apps.yaml:
last_one: True # or False

in the app:
if self.args[“last_one”]:
self.notify(…)

Thank you @aimc That worked :ok_hand:

@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):

        self.log('TurnOffLightsAtBedtime: Daily Callback Function triggered : ' + self.name)

Log:

2017-10-25 09:42:00.689970 INFO bedtime_lights_5_test: TurnOffLightsAtBedtime: Daily Callback Function triggered : bedtime_lights_5_test

1 Like

Here is the update function with solution:

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())
1 Like