Table of Contents

Class MediaPlayer

Namespace
HaMqttDiscoverable.Entities
Assembly
HaMqttDiscoverable.dll

A media player, built from several individually-discoverable MQTT entities (a power switch, a volume number, optionally a mute switch and a source select, a handful of sensors for metadata, and transport buttons).

public sealed class MediaPlayer : IAsyncDisposable
Inheritance
MediaPlayer
Implements
Inherited Members

Examples

var player = new MediaPlayer(connection, new MediaPlayerConfig
{
    Name = "Living Room TV",
    UniqueId = "living-room-tv",
    Device = device,
    Sources = new List<string> { "HDMI 1", "HDMI 2", "Chromecast" },
});

player.PowerCommandReceived += async isOn => { SetPower(isOn); await player.PublishPowerAsync(isOn); };
player.PlayRequested += async () => { Play(); await player.PublishPlaybackStateAsync(MediaPlayerPlaybackState.Playing); };

await player.PublishDiscoveryAsync();

Remarks

Home Assistant's built-in MQTT integration has no native media_player platform - only component types like sensor, switch, number, select, etc. are MQTT-discoverable. This class works around that by publishing a bundle of those real entities under one device. To present them to Home Assistant as a single media_player entity, wire them together with Home Assistant's built-in "Universal Media Player" (universal) platform - see the README for a ready-to-use YAML example.

Constructors

MediaPlayer(HaMqttConnection, MediaPlayerConfig)

Creates a media player's sub-entities from the given config. Call PublishDiscoveryAsync(CancellationToken) to publish all of them.

public MediaPlayer(HaMqttConnection connection, MediaPlayerConfig config)

Parameters

connection HaMqttConnection
config MediaPlayerConfig

Properties

Artist

Reports the artist of the currently playing media.

public Sensor Artist { get; }

Property Value

Sensor

Mute

Mutes/unmutes the player, or null if the config didn't set SupportsMute.

public Switch? Mute { get; }

Property Value

Switch

Next

Requests skipping to the next track.

public Button Next { get; }

Property Value

Button

Pause

Requests playback pause.

public Button Pause { get; }

Property Value

Button

Play

Requests playback start.

public Button Play { get; }

Property Value

Button

PlaybackState

Reports playback state - publish one of the MediaPlayerPlaybackState constants.

public Sensor PlaybackState { get; }

Property Value

Sensor

Power

Turns the player on/off.

public Switch Power { get; }

Property Value

Switch

Previous

Requests skipping to the previous track.

public Button Previous { get; }

Property Value

Button

Source

Selects the active input source, or null if the config had no Sources.

public Select? Source { get; }

Property Value

Select

Stop

Requests playback stop.

public Button Stop { get; }

Property Value

Button

Title

Reports the title of the currently playing media.

public Sensor Title { get; }

Property Value

Sensor

Volume

The current volume, from VolumeMin to VolumeMax.

public Number Volume { get; }

Property Value

Number

Methods

DisposeAsync()

Unsubscribes every sub-entity's command topic. Does not remove anything from Home Assistant - call RemoveAsync(CancellationToken) for that.

public ValueTask DisposeAsync()

Returns

ValueTask

PublishArtistAsync(string, CancellationToken)

Publishes the artist of the currently playing media.

public Task PublishArtistAsync(string artist, CancellationToken cancellationToken = default)

Parameters

artist string
cancellationToken CancellationToken

Returns

Task

PublishDiscoveryAsync(CancellationToken)

Publishes the Home Assistant discovery payload for every sub-entity.

public Task PublishDiscoveryAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

PublishMuteAsync(bool, CancellationToken)

Publishes the player's mute state. Does nothing if Mute is null.

public Task PublishMuteAsync(bool isMuted, CancellationToken cancellationToken = default)

Parameters

isMuted bool
cancellationToken CancellationToken

Returns

Task

PublishPlaybackStateAsync(string, CancellationToken)

Publishes the player's playback state - use one of the MediaPlayerPlaybackState constants.

public Task PublishPlaybackStateAsync(string state, CancellationToken cancellationToken = default)

Parameters

state string
cancellationToken CancellationToken

Returns

Task

PublishPowerAsync(bool, CancellationToken)

Publishes the player's power state.

public Task PublishPowerAsync(bool isOn, CancellationToken cancellationToken = default)

Parameters

isOn bool
cancellationToken CancellationToken

Returns

Task

PublishSourceAsync(string, CancellationToken)

Publishes the player's active source. Does nothing if Source is null.

public Task PublishSourceAsync(string source, CancellationToken cancellationToken = default)

Parameters

source string
cancellationToken CancellationToken

Returns

Task

PublishStateAsync(MediaPlayerSnapshot, CancellationToken)

Publishes every non-null field of snapshot.

public Task PublishStateAsync(MediaPlayerSnapshot snapshot, CancellationToken cancellationToken = default)

Parameters

snapshot MediaPlayerSnapshot
cancellationToken CancellationToken

Returns

Task

PublishTitleAsync(string, CancellationToken)

Publishes the title of the currently playing media.

public Task PublishTitleAsync(string title, CancellationToken cancellationToken = default)

Parameters

title string
cancellationToken CancellationToken

Returns

Task

PublishVolumeAsync(double, CancellationToken)

Publishes the player's current volume level.

public Task PublishVolumeAsync(double volume, CancellationToken cancellationToken = default)

Parameters

volume double
cancellationToken CancellationToken

Returns

Task

RemoveAsync(CancellationToken)

Removes every sub-entity from Home Assistant.

public Task RemoveAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

Events

MuteCommandReceived

Raised when Home Assistant sends a mute command. Never raised if Mute is null.

public event Func<bool, Task>? MuteCommandReceived

Event Type

Func<bool, Task>

NextTrackRequested

Raised when Home Assistant requests skipping to the next track.

public event Func<Task>? NextTrackRequested

Event Type

Func<Task>

PauseRequested

Raised when Home Assistant requests playback pause.

public event Func<Task>? PauseRequested

Event Type

Func<Task>

PlayRequested

Raised when Home Assistant requests playback start.

public event Func<Task>? PlayRequested

Event Type

Func<Task>

PowerCommandReceived

Raised when Home Assistant sends a power command.

public event Func<bool, Task>? PowerCommandReceived

Event Type

Func<bool, Task>

PreviousTrackRequested

Raised when Home Assistant requests skipping to the previous track.

public event Func<Task>? PreviousTrackRequested

Event Type

Func<Task>

SourceCommandReceived

Raised when Home Assistant sends a new source selection. Never raised if Source is null.

public event Func<string, Task>? SourceCommandReceived

Event Type

Func<string, Task>

StopRequested

Raised when Home Assistant requests playback stop.

public event Func<Task>? StopRequested

Event Type

Func<Task>

VolumeCommandReceived

Raised when Home Assistant sends a new volume level.

public event Func<double, Task>? VolumeCommandReceived

Event Type

Func<double, Task>