Query / Is this possible?

For my lighting in the morning, I have the following:

def initialize(self):
    # Wait for movement to turn lights on in the morning
    self.listen_state(self.morning, entity='binary_sensor.xiaomi_motion_kitchen', old='off', new='on', constrain_input_select='input_select.house,Morning')
    # Turn lights off one hour after sunrise in the morning
    self.run_at_sunrise(self.morning_off_sunrise, offset=3600, constrain_input_select='input_select.house,Morning')

def morning(self, entity, attribute, old, new, kwargs):
    sunrise = self.sunrise() + datetime.timedelta(minutes=60)
    if self.date() == sunrise.date() and self.time() < sunrise.time() and self.get_state('light.living_room') == 'off':
        self.log("Running first morning function")
        self.call_service('light/turn_on', entity_id='light.living_room', profile='standard')
        self.handle = self.listen_state(self.morning_off, entity='sensor.aeotec_zw100_multisensor_6_burglar', new='0', constrain_input_select='input_select.house,Morning')

def morning_off(self, entity, attribute, old, new, kwargs):
    self.call_service('light/turn_off', entity_id='light.living_room')

def morning_off_sunrise(self, kwargs):
    self.call_service('light/turn_off', entity_id='light.living_room')

My question is, is it possible to combine the two ‘morning_off’ functions? The problem I’m getting at the moment (it seems no matter what I’ve tried) is I always end up with a variation of TypeError: morning_off() takes 2 positional arguments but 6 were given

I understand why that’s occurring, but can’t seem to work out for sure if there is a way to combine the functions and have the positional arguments as ‘optional’ for lack of a better word.

It’s not a huge deal in this one app, but I find I’m often running into this “perform action if X happens OR at Y time” quite frequently so it seems like it would be neater to not be duplicating the functions.

Thanks for any advice one way or the other!

you need always 2 types of callbacks because they need different amount of args.
i always end up calling a 3 function from 2 callbacks.

the reason is that a listen_state has the state from an entity and a time callback doesnt.

Thanks @ReneTode, I thought that would be the case. Was just hoping I could somehow cheat and make one callback accept variable arguments.

Thanks again for confirming.

1 Like