Ok, I’m at my wits end with *args and **kwargs
I have a lot of automations created, and honestly, I have had to do trial and error when it comes to *args and **kwargs on each and every automation.
Now I understand *args are individual argument values, without keywords, and I understand that **kwargs are key/value pairs, my problem is, when to use them, because there are apparently pre-determined and required variables flying all over the place.
There is entity, attribute, old, new and kwargs, and I can’t figure out when they are sent, where they are available, if they are global or local to each individual class or what???
So just about every one of my automations I end up with errors about a function requiring a set number of positional arguments but only receiving less than that, or more than that expected number.
The documentation says the layout for using run_in is:
self.handle = self.run_in(callback, delay, **kwargs)
and the layout for the callback is:
def callback(self, **kwargs):
But using those together, results in errors.
So I have tried all of the following, all with different errors:
self.lock_timer = self.run_in(self.send_notification, 10)
self.lock_timer = self.run_in(self.send_notification, 10, entity=entity)
self.lock_timer = self.run_in(self.send_notification, 10, kwargs)
self.lock_timer = self.run_in(self.send_notification, 10, **kwargs)
And I have used all of the following as a scheme for the callback function:
def send_notification(self, **kwargs):
def send_notification(self, *args, **kwargs):
def send_notification(self, entity):
def send_notification(self, entity, attribute, old, new, kwargs):
No matter what combination I use, about half the time I get either an error telling me I am not sending the right amount of arguments and the other half of the time I get this error:
KeyError: ‘attribute’
Most of the time I can muddle through, and often get it to pass without an error, but even during those times, I feel like it may be working, but it is not correct, or I’m passing variables that I don’t need to just to stop from getting errors.
Most of my automations start out with a listen_state in the initialize function, and they work fine as long as my callback function looks like this:
def callback(self, entity, attribute, old, new, kwargs):
But beyond calling from the initialize function to the first callback, everything goes off the rails. From within the callback that was called by initialize, I then have a run_in call to call another callback, but I never know what to put in that callback. I can’t list it like this:
def second_callback(self, entity, attribute, old, new, kwargs):
because I get errors that the run_in is not providing enough arguments, so its just completely confusing. Even if I follow the documentation on the correct layout of a callback, which is different form a listen callback and a schedule callback, but the layout given by the documentation always errors out.
Then you said in a pervious post not to use (*args, **kwargs) and to only use **kwargs, but I have several automations that will not run without errors unless I have both *args and **kwargs in the function. It just doesn’t seem consistent, and I can’t figure out how to get the entity, new and old states to pass from the initial function call to subsequent function calls.
Sorry, I’m just very frustrated right now