I don’t have AppDaemon even running any more, but I was able to verify what I thought to be true using the API.
If I do: set_state(‘switch.testme’,‘on’) … then, in the UI, we see the switch. If I then toggle that switch in the UI, the following event it emitted.
{"event_type":"call_service","event":{"domain":"switch","service":"turn_off","service_data":{"entity_id":"switch.testme"}}}
So, based on the AppDaemon docs, I would need something like…
listen_event(my_call_back, event = "call_service")
then, in my_call_back… I would do…
def my_call_back(self,event_name,data, kwargs):
if(data.domain == "switch" and data.service_data.entity_id == "switch.testme"):
# yes... this is me... do the things
else:
# this was a service_call to something that isn't me... so ignore it.
Also, keep in mind, you’ll need to data.domain == “homeassistant” as well. For UI interaction, it’ll generally be switch.turn_off/turn_on. But, in automations, one might call homeassistant.turn_on/turn_off… so you need to catch them both.
Somewhere in the code of your call back, you need to make sure to self.set_state() back on if they turned the device on or off if they turned it off. If you’re doing something more than an input_boolean type behavior, generally you’d want to run the code to turn the thing on first, and then report back with the state saying that it’s on.