Skip to content

Build a player provider

A player provider reaches a kind of speaker, such as Sonos, Chromecast or AirPlay. It does not own players: the player controller does. Players explains how one device reached in several ways becomes one player.

Start from _demo_player_provider: provider.py holds the provider, player.py the protocol of one device.

  • For each device it reaches, your provider builds a subclass of PlayerProtocol and calls await mass.players.register(protocol).
  • The controller finds or creates the Player of that device and attaches your protocol to it, which can be after register returns: on_attach() tells you when. The protocol then finds its player as self.player.
  • When the device goes away, call mass.players.unregister(protocol_id). When your provider forgets the device, call mass.players.remove_protocol(protocol_id).
  • Features of the provider itself, such as CREATE_GROUP_PLAYER, go to the constructor as a set, as for any provider.

A protocol reports what its device is and does through its _attr_* values, or by overriding the matching properties:

  • _attr_role: what the protocol is for the device (ProtocolRole: NATIVE, CONTROL, AUDIO or VIRTUAL).
  • _attr_type and _attr_supported_features.
  • _attr_device_info, with every identifier the device really has (MAC address, UUID, serial number). The controller matches protocols to devices on them.
  • The state: _attr_powered, _attr_volume_level, _attr_playback_state, _attr_current_media and the like.

It also carries out the commands: play_media(), stop(), pause(), volume_set(), power() and the others its features allow.

To publish a change, set the _attr_* values and call self.update_state(). The player then derives its state from all its protocols and sends PLAYER_UPDATED when something changed.

List what your provider listens for in its manifest:

{
"mdns_discovery": ["_demo_service_type._tcp.local."],
"upnp_discovery": ["urn:schemas-upnp-org:device:MediaRenderer:1"]
}
  • mdns_discovery takes Zeroconf service types; the server calls on_mdns_service_state_change() for each change.
  • upnp_discovery takes SSDP search targets; the server calls on_upnp_service_discovered().
  • discover_players() registers the known devices that these cannot reach, such as a list in the config. The server calls it when the provider loads and whenever a device may be found again, and replays the cached mDNS records and SSDP searches at the same moments.

The discovery README covers how the server listens.

IMPORTANT

A protocol has to follow rules about the values it reads, grouping, power and the order of on_attach() and on_detach(). Read What a Provider Has to Do in the players README before you write one.

Music Assistant is a project of the Open Home Foundation.