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
connectionHaMqttConnectionconfigMediaPlayerConfig
Properties
Artist
Reports the artist of the currently playing media.
public Sensor Artist { get; }
Property Value
Mute
Mutes/unmutes the player, or null if the config didn't set SupportsMute.
public Switch? Mute { get; }
Property Value
Next
Requests skipping to the next track.
public Button Next { get; }
Property Value
Pause
Requests playback pause.
public Button Pause { get; }
Property Value
Play
Requests playback start.
public Button Play { get; }
Property Value
PlaybackState
Reports playback state - publish one of the MediaPlayerPlaybackState constants.
public Sensor PlaybackState { get; }
Property Value
Power
Turns the player on/off.
public Switch Power { get; }
Property Value
Previous
Requests skipping to the previous track.
public Button Previous { get; }
Property Value
Source
Selects the active input source, or null if the config had no Sources.
public Select? Source { get; }
Property Value
Stop
Requests playback stop.
public Button Stop { get; }
Property Value
Title
Reports the title of the currently playing media.
public Sensor Title { get; }
Property Value
Volume
public Number Volume { get; }
Property Value
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
PublishArtistAsync(string, CancellationToken)
Publishes the artist of the currently playing media.
public Task PublishArtistAsync(string artist, CancellationToken cancellationToken = default)
Parameters
artiststringcancellationTokenCancellationToken
Returns
PublishDiscoveryAsync(CancellationToken)
Publishes the Home Assistant discovery payload for every sub-entity.
public Task PublishDiscoveryAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
PublishMuteAsync(bool, CancellationToken)
Publishes the player's mute state. Does nothing if Mute is null.
public Task PublishMuteAsync(bool isMuted, CancellationToken cancellationToken = default)
Parameters
isMutedboolcancellationTokenCancellationToken
Returns
PublishPlaybackStateAsync(string, CancellationToken)
Publishes the player's playback state - use one of the MediaPlayerPlaybackState constants.
public Task PublishPlaybackStateAsync(string state, CancellationToken cancellationToken = default)
Parameters
statestringcancellationTokenCancellationToken
Returns
PublishPowerAsync(bool, CancellationToken)
Publishes the player's power state.
public Task PublishPowerAsync(bool isOn, CancellationToken cancellationToken = default)
Parameters
isOnboolcancellationTokenCancellationToken
Returns
PublishSourceAsync(string, CancellationToken)
Publishes the player's active source. Does nothing if Source is null.
public Task PublishSourceAsync(string source, CancellationToken cancellationToken = default)
Parameters
sourcestringcancellationTokenCancellationToken
Returns
PublishStateAsync(MediaPlayerSnapshot, CancellationToken)
Publishes every non-null field of snapshot.
public Task PublishStateAsync(MediaPlayerSnapshot snapshot, CancellationToken cancellationToken = default)
Parameters
snapshotMediaPlayerSnapshotcancellationTokenCancellationToken
Returns
PublishTitleAsync(string, CancellationToken)
Publishes the title of the currently playing media.
public Task PublishTitleAsync(string title, CancellationToken cancellationToken = default)
Parameters
titlestringcancellationTokenCancellationToken
Returns
PublishVolumeAsync(double, CancellationToken)
Publishes the player's current volume level.
public Task PublishVolumeAsync(double volume, CancellationToken cancellationToken = default)
Parameters
volumedoublecancellationTokenCancellationToken
Returns
RemoveAsync(CancellationToken)
Removes every sub-entity from Home Assistant.
public Task RemoveAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
Events
MuteCommandReceived
Raised when Home Assistant sends a mute command. Never raised if Mute is null.
public event Func<bool, Task>? MuteCommandReceived
Event Type
NextTrackRequested
Raised when Home Assistant requests skipping to the next track.
public event Func<Task>? NextTrackRequested
Event Type
PauseRequested
Raised when Home Assistant requests playback pause.
public event Func<Task>? PauseRequested
Event Type
PlayRequested
Raised when Home Assistant requests playback start.
public event Func<Task>? PlayRequested
Event Type
PowerCommandReceived
Raised when Home Assistant sends a power command.
public event Func<bool, Task>? PowerCommandReceived
Event Type
PreviousTrackRequested
Raised when Home Assistant requests skipping to the previous track.
public event Func<Task>? PreviousTrackRequested
Event Type
SourceCommandReceived
Raised when Home Assistant sends a new source selection. Never raised if Source is null.
public event Func<string, Task>? SourceCommandReceived
Event Type
StopRequested
Raised when Home Assistant requests playback stop.
public event Func<Task>? StopRequested
Event Type
VolumeCommandReceived
Raised when Home Assistant sends a new volume level.
public event Func<double, Task>? VolumeCommandReceived