Media player Trådfri remote (appdaemon and deconz)

Wanted to share my easy but, I think, great Appdaemon app for using a Ikea Trådfri remote to control my Sonos without my phone. Can of course be used with other media players as well.

With this app I can use the remote for play/pause, volume up and down, next and previous track.

I get the remote events into Home Assistant via Deconz with a Conbee. This will not work if you use the remote via Trådfri gateway or Hue bridge.

yaml:

sonos_remote_control:
  module: sonos_remote
  class: SonosRemote
  event: deconz_event
  id: sonos_remote
  sonos: media_player.sonos

py-file:

import appdaemon.plugins.hass.hassapi as hass
class SonosRemote(hass.Hass):
    def initialize(self):
        self.sonos = self.args['sonos']
        if 'event' in self.args:
            self.listen_event(self.handle_event, self.args['event'])

    def handle_event(self, event_name, data, kwargs):
        if data['id'] == self.args['id']:
            self.log(data['event'])
            if data['event'] == 1002:
                self.log('Button toggle')
                self.call_service("media_player/media_play_pause", entity_id = self.sonos)

            elif data['event'] == 2002:
                self.log('Button volume up')
                self.call_service("media_player/volume_up", entity_id = self.sonos)

            elif data['event'] == 3002:
                self.log('Button volume down')
                self.call_service("media_player/volume_down", entity_id = self.sonos)

            elif data['event'] == 4002:
                self.log('Button previous')
                self.call_service("media_player/media_previous_track", entity_id = self.sonos)                    

            elif data['event'] == 5002:
                self.log('Button next')
                self.call_service("media_player/media_next_track", entity_id = self.sonos)
7 Likes

Hey @teachingbirds,
I really like your solution to control music with the Tradfri remote. One question: Is it possible to tell a media player to select a specific source with Appdaemon? I cannot get this to work. I have an av receiver and I want it to switch to source HDMI2. In Dev Tools this one works:

selec_source|372x2
19
What would be the equivalent command in Appdaemon? I’ve tried self.call_service("media_player.select_source", entity_id="media_player.av_receiver", source="HDMI2") but it won’t work. Can you help me out?

For Appdaemon you have to use media_player/select_source instead of media_player.select_source. So with a / and not a dot. Then it should work.

2 Likes

Damn, stupid mistake. Thank you. It’s working now.

1 Like