Callback not firing

I have a switch that my appdaemon app is listening to. When I turn that switch off by hand from the HA interface all behaves as expected, but when ANOTHER APP turns it off it doesn’t run its callback.

Here’s the basic bits of code:

  self.listen_state(self.display_switch, self.args["display_switch"])

  def display_switch(self, entity, attribute, old, new, kwargs):
    if new == "on" and self.get_state(self.args["display"]) != "on":
      self.log("TV switch on zone: {}".format(self.args["zone"]))
    elif new == "off":
      self.log("TV switch {} zone: {}".format(new,self.args["zone"]))
      self.run_in(self.zone_off, 0)

  def zone_off(self, kwargs):
    self.log("zone_off called")
    < blah blah blah>

Here’s the output when I toggle the switch off by hand from HA:

2019-09-20 15:09:45.828805 INFO livingroom_av: new off
2019-09-20 15:09:45.829725 INFO livingroom_av: TV switch off zone: livingroom
2019-09-20 15:09:46.023006 INFO livingroom_av: zone_off called

But when I turn the switch off from another app with


the callback never fires. I’m baffled. I can not think of a reason why this should be the case:

2019-09-20 15:10:15.152976 INFO livingroom_av: new off
2019-09-20 15:10:15.165438 INFO livingroom_av: TV switch off zone: livingroom

did you try replacing
self.run_in(self.zone_off, 0)
by just


def zone_off(self):
  self.log("zone_off called")

Heh. I just took my dog to the park and figured it out while walking around. I have a race condition.

What’s happening is that just before I attempt to throw my “livingroom_tv” switch, I’m switching to a mode where the app that listens to that switch, is input_select constrained from running.