appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

[Feature Request]: Custom decorators

Open dekiesel opened this issue 4 years ago • 8 comments

Let's assume you have a smart light and a motion sensor in your hallway:

You want to the smart bulb to turn on when there is motion. But you only want it to turn on when the sun is down.

It'd be great if we could write our own decorators (like @sun_down) that would only execute a callback if the condition is met.

    @sun_down
    def state_changed_cb(self, entity, attribute, old, new, kwargs):
         ...

This is just an example, I know the sun up condition can be added to listen_state()-function, but with more complex logic it becomes a hassle.

dekiesel avatar May 04 '21 11:05 dekiesel

I just realized that I already can do that.... I am sorry for the brain fart.

dekiesel avatar May 04 '21 11:05 dekiesel

Hello @dekiesel,

It is possible to do what you want using constraints here. Though it will be nice to be able to turn these into decorators which is not possible yet

Odianosen25 avatar May 04 '21 11:05 Odianosen25

@Odianosen25

Ok, maybe the request wasn't too far off :)

dekiesel avatar May 04 '21 11:05 dekiesel

I just realized that I already can do that.... I am sorry for the brain fart.

Lols it’s fine

Odianosen25 avatar May 04 '21 11:05 Odianosen25

Ok, maybe the request wasn't too far off :)

no it’s not. It’s kind of been requested before alongside the ability to make custom constraints global. Until we can do this, will kind of not be able to make constraints decorators

Odianosen25 avatar May 04 '21 11:05 Odianosen25

@Odianosen25

The custom constraints are on a per app basis, if I understood them correctly. My hope with decorators was that they could be used globally. Because a lot of the "if this then that" stuff is very repetetive.

Should I keep this feature request open or close it? Sorry for the confusion.

dekiesel avatar May 04 '21 11:05 dekiesel

The custom constraints are on a per app basis

Unfortunately it still is on a per app bases. It is possible though to have an app with all the constraint functions, then using a get_app api use the function into another app's register_contraint api call.

That way you keep all your constraints in one app, and simply reuse them as you like.

Regards

Odianosen25 avatar May 04 '21 11:05 Odianosen25

and the advantage from what odia suggests is that you can set vars in the constraints that can be changed from other apps. like:

def my_constraint(self, data):
    if my_var > 10:
        return True
    return False

def my_var(self,var):
    self.my_var = var

in another app:

 self.get_app("constraints").my_var(12)

in a third app:

    self.register_constraint("cn")

def cn(self, data):
    return self.get_app("constraints").my_constraint(data)

you could even have different app instances from the constraint app, that would have different settings. that way this:

    self.register_constraint("cn")

def cn(self, data):
    return self.get_app("constraints2").my_constraint(data)

would do the same but with different data.

ReneTode avatar May 04 '21 13:05 ReneTode