How to get callback from self.listen_event() in appdaemon

This seems like it should be pretty simple but it doesn’t work and I can’t figure out why because I basically just copied the example from the documentation. Anyone know why the callback isn’t working?

import appdaemon.plugins.hass.hassapi as hass

# Playlist in room

class PlaylistInRoom(hass.Hass):

  def initialize(self):
    self.log("Playlist In Room AppDaemon App Initialized")
    self.listen_event(self.mode_event, "MODE_CHANGE")

  def mode_event(self):
    self.log("Got your event, broham")

I get this in the log when I fire the event MODE_CHANGE

2020-10-24 02:07:56.949652 INFO playlist_in_room: Playlist In Room AppDaemon App Initialized
2020-10-24 02:07:56.950845 INFO AppDaemon: App initialization complete
2020-10-24 02:07:57.490666 INFO AppDaemon: New client Admin Client connected
2020-10-24 02:08:21.457419 WARNING AppDaemon: Suspect incorrect signature type for callback mode_event() in playlist_in_room, should be f(self, event, data, kwargs) - discarding
2020-10-24 02:08:21.457662 WARNING playlist_in_room: ------------------------------------------------------------
2020-10-24 02:08:21.457840 WARNING playlist_in_room: Unexpected error in worker for App playlist_in_room:
2020-10-24 02:08:21.458088 WARNING playlist_in_room: Worker Ags: {'id': 'f43f7e241e454c5b88092ab72c165234', 'name': 'playlist_in_room', 'objectid': '079553cd7cb542d8bfe94438fb2d328f', 'type': 'event', 'event': 'MODE_CHANGE', 'function': <bound method PlaylistInRoom.mode_event of <playlist_in_room.PlaylistInRoom object at 0x7f1d30d3eee0>>, 'data': {}, 'pin_app': True, 'pin_thread': 1, 'kwargs': {'__thread_id': 'thread-1'}}
2020-10-24 02:08:21.458268 WARNING playlist_in_room: ------------------------------------------------------------
2020-10-24 02:08:21.459777 WARNING playlist_in_room: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/appdaemon/threading.py", line 933, in worker
    funcref(args["event"], data, self.AD.events.sanitize_event_kwargs(app, args["kwargs"]))
TypeError: mode_event() takes 1 positional argument but 4 were given
2020-10-24 02:08:21.460030 WARNING playlist_in_room: ------------------------------------------------------------

Okay going to answer my own question here. I guess you have to always provide all the arguments in a callback function even if you don’t use them, like this:

  def mode_event(self, event, data, kvargs):

instead of

  def mode_event(self):

like I had before. Strangely the examples in the docs have just
f(self,kvargs) but that wasn’t enough in this case as I also tried that.

2 Likes

Keep an eye on the data of the comments and check the official docs6 the API might changes sometimes.

https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#kwargs