What's the best way to change the entity of a callback

Hello,

I am just getting started with appdaemon and even python in general. I have been making progress over the last week but now I must ask for some advice.

I have a callback that should fire when a particular media_play changes state to idle.

self.advance_track = self.listen_state(self.next_track, "media_player."+self.media_player, new="idle",  duration = "1")

In HA the media_player is selected from input_select.
self.media_player = self.get_state("input_select.gmusic_player")

How is the best way to change the value of my self.media_player variable that trigger my callback?

Right now I am using a separate callback to cancel / recreate the self.advance_track callback with the new value of self.media_player.

self.listen_state(self.select_player, input_select.gmusic_player)

def select_player(self)
  self.cancel_listen_state(self.advance_track)
  self.media_player = self.get_state("input_select.gmusic_player")
  self.advance_track = self.listen_state(self.next_track, "media_player."+self.media_player, new="idle",  duration = "1"

This seems to work but Iā€™m wondering if this is the best way to handle something like this.

You could also approach this by registering a callback for each media_player in your input_select, and putting a constraint on each callback. Something like this should work:

for media_player in self.split_device_list(self.entities.input_select.gmusic_player.attributes.options):
    self.listen_state(self.next_track, "media_player."+media_player, new="idle",  duration = "1", constrain_input_select="input_select.gmusic_player,"+media_player)

Either approach is totally valid, and there should not be a difference performance-wise. Really just depends on what you prefer.

1 Like