I’m wondering if I can change the state of e.g. a switch while temporarily suppressing callbacks for state changes of that switch. I tried the following (pseudo) code:
def initialize(self):
self.handle = self.listen_state(<some func>, <switch>, old="off", new="on")
def triggered_by_some_external_event(self, ...):
self.cancel_listen_state(self.handle)
self.turn_on(<switch>)
self.handle = self.listen_state(<somefunc>, <switch>, old="off", new="on")
this seems to be problematic as by the time the switch is actually turning on, the new listen_state call has already completed, meaning <some_func> will be executed when the function triggered_by_some_external_event is ran. If I put a time.sleep(1) after the turn_on call, everything is working as expected.
My goal is to turn on the switch without executing <some_func>.
With hass automations I can do a wait_template, between turning on the switch and re-adding the callback, but I can’t seem to find something similar in appdaemon.
The only other working solution I came up with
def triggered_by_some_external_event(self, ...):
self.cancel_listen_state(self.handle)
self.listen_state(reset_listener, <switch>, old="off", new="on")
self.turn_on(<switch>)
def reset_listener(self, ...):
self.handle = self.listen_state(<somefunc>, <switch>, old="off", new="on")
which seems to be suboptimal too, since I am not guaranteed that the reset listener is added before the switches state transition.
Is there a different way of doing this that I have not figured out yet? Or is either of the above (sleeping or callback chaining) the preferred way of doing things?