Media Player as MQTT

Hello!

I would like to use MQTT for a media player.

I am using shairport to airplay music in a whole-home audio solution. Shairport supports MQTT, and I am able to get shairport to publish topics for status. I am also able to control Shairport via MQTT.

I found the “Universal Media Player” Configuration in the documentation (https://www.home-assistant.io/integrations/universal/) that might do what I’m looking for.

If I were to use Universal media player, I imagine it would look something like:

media_player:
  - platform: universal
    name: Shairport
    commands:
      volume_down:
        service: mqtt.publish
        data: 
          topic: shairport-sync/snapcast/remote
          payload: "volumedown"
          qos: 2
          retain: false
...

Would that be possible or am I totally off on how to implement this?

I don’t believe there is anyway to use MQTT as a media player it’s been asked many times.

1 Like

Yeah it looks like you are right. I just found the feature request: MQTT Media Player

Thanks for the response!

For shairpoint integration see Simple multiroom with Home Assistant

Hi,

Not sure if you found a solution already, but I made a custom component for this since I’m also using a MQTT enabled media player and I couldn’t really find any alternative.

If you’d like to try it out (and hopefully tell me if it works for you) that would be great!

Heres the link: https://github.com/TroyFernandes/hass-mqtt-mediaplayer

Sweet thanks! I’ll give it a try!

I am intrigued at what sort of media device you have that has an mqtt interface?

It’s not so much the media player itself. The media player’s called Musicbee and it supports plugins. So I made a MQTT Remote plugin. Basically just publishes current song info and listens for commands through MQTT.

Looks quite cool. If I wasn’t wedded to Squeezebox I would look further.

Great that their system is open enough for that.

Not sure if you tried it already, but I finally understood what you meant by the example in your original post and it made sense for me to implement the volume_up and volume_down independently of the volume_set.

Anyways with your example, I think the configuration.yaml would look something like this

media_player:  
  - platform: mqtt-mediaplayer
    name: "Shairport"
    vol_down:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "volumedown"
    vol_up:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "volumeup"

I pushed the changes to the repo. Also, if vol_up and/or vol_down are used, the volume slider (aka. the volume_set service) becomes disabled. Just something to note

Thanks! I got it working!

Here is my config:

  - platform: mqtt-mediaplayer
    name: "Airplay"
    topic:
      song_title: "shairport-sync/snapcast/title"
      song_artist: "shairport-sync/snapcast/artist"
      song_album: "shairport-sync/snapcast/album"
      album_art: "shairport-sync/snapcast/cover"
      player_status: "shairport-sync/snapcast/manual/status"
    status_keyword: "playing"
    vol_down:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "volumedown"
    vol_up:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "volumeup"
    next:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "nextitem"
    previous:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "previtem"
    play:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "play"
    pause:
      service: mqtt.publish
      data:
        topic: "shairport-sync/snapcast/remote"
        payload: "pause"

Volume slider doesn’t work because I don’t think shairport allows for volume setting via a command anyway. But the volume up/down is working! Thank you for this!

1 Like

Hi all,

I’m a noob at HA and mqtt and have a following issue:

I have an ESP32 based Webradio which supports mqtt and I managed to configure the information of the station playing as well as the controls to play next/previous station using hass-mqtt-mediaplayer. The card on my dashboard shows up and everything is in order!
I’ve been using vol_up and vol_down commands and they seem to work correctly.

However, I would like to use the volume slider as my webradio accepts a command “volume=[value]”.
How do I define this value to be sent via mqtt? Could someone please help me with this one?

Thanks in advance.

Hi Troy,
Do you mind sharing your MQTT Musicbee plugin? Could not find it on git, or on Musicbee plugin page.
Thanks in advance.

Please find my code from configuration.yaml attached:

#MQTT media player
media_player:  
  - platform: mqtt-mediaplayer
    name: "Webradio"
    topic:
      song_title: "{{ states('sensor.webradio_station') }}"
      song_artist: "{{ states('sensor.webradio_stream') }}"
      song_volume: "{{ states('sensor.webradio_volume') }}"
      player_status: "{{ states('sensor.webradio_status') }}"
      album_art: "musicbee/albumart"
    status_keyword: "1"
    volume:
        service: mqtt.publish
        data:
          topic: "musicbee/command"
          payload: "{\"command\":\"volume_set\", \"args\":{\"volume\":\"{{volume}}\"}}" # <<< Not configured yet :(
    next:
      service: mqtt.publish
      data:
        topic: "webradio/command"
        payload: "uppreset=1"
    previous:
      service: mqtt.publish
      data:
        topic: "webradio/command"
        payload: "downpreset=1"
    play:
      service: mqtt.publish
      data:
        topic: "webradio/command"
        payload: "resume"
    pause:
      service: mqtt.publish
      data:
        topic: "webradio/command"
        payload: "stop"

Git link: https://github.com/TroyFernandes/hass-mqtt-mediaplayer

I have a sample mqtt command for volume change to be used:
$ mosquitto_pub -h broker.hivemq.com -t espradio -m volume=80

As mentioned above, I would like to send an mqtt.publish command “volume=[value from the volume slider on the card]”.
All other commands which are described in the code work correctly.
Also, I noticed that if I change the volume using the buttons, the slider itself updates to a correct volume setting when received an mqtt message “volume=[current volume value]” from the radio.

Would it be possible to fetch the current slider value and send it to the radio?

Hope I made something clear here :slight_smile:

Just started using MusicBee and love it.
Can this be done without running a mqtt server or is that required.
I use the MusicBee remote app on Android and it works without mqtt server.
Thanks…

Hi @_troy, seconding the request of @tus. Would love to integrate my MB installation with HA. Cheers in advance.

Just a little heads-up to anybody else who uses MusicBee and wanted to integrate it with HA. Luckily for us, @_troy recently rediscovered a backup of his original MQTT plugin for MB and made it available as a repo on GH: GitHub - TroyFernandes/Musicbee-MQTT: Send the current playing song info in Musicbee to an MQTT Server

I have since successfully created a media player entity from my MB instance that also has all basic capabilities (cover art, skipping, volume control).