Implementing an MQTT Media Player

I’m developing a media_player entity to use MQTT (more specifically, to communicate with a shairport-sync installation).

My new Python class inherits from MediaPlayerEntity but I’m not sure how to go about the MQTT part. Should I inherit from MQTT too, or is it ok (preferable) to use class composition? I’m not sure what the acceptable style for the project is.

What I’m having trouble with (regardless of those two options), is that I can’t find any references to where the MQTT class is being used so I’m not sure how to initialize it. I think this is the class I need. I basically want access to the convenience methods to subscribe to topics. The other classes in homeassistant.components.mqtt seem to provide more than I need since I won’t (yet) be implementing e.g. service discovery.

I’ve looked at the MQTT binary_sensor and a few others but couldn’t find any hints on how to use the MQTT class properly.

I’m new to reading the project’s code even though I’ve read the architecture docs and all that, so I could be on the wrong path.

1 Like

I got an answer on Discord: I’ll get to know the hass object better. I can use the publish and subscribe methods on the mqtt object there.

Hi, i had the same idea. I have a working mqtt-mediaplayer with templates but its just the first development. I looked at the mqtt fan.py and used the ideas and the subscription.py from mqtt.
In addition i added some features for grouping entities.

Have a look at https://github.com/jonaseickhoff/hass-multiroom-mqtt-mediaplayer

You may then also find this interesting: https://github.com/TroyFernandes/hass-mqtt-mediaplayer.

Hi, that was what i found too. I was very helpful to understand the basics of an integration. But for me there were some services missing (like turn on/off, switching source) and he said, that he did not want to implement these features. In addition i found it more complicated to add new sensors for every attribute, so i decided to develop a mqtt-mediaplayer where you can directly subscribe topics and use templates. Its now working fine with my musiccast2mqtt bridge where i can also join and unjoin groups and zones.