Add support for media player assumed state (#11642)

This commit is contained in:
Shay Levy 2022-02-11 18:42:22 +02:00 committed by GitHub
parent db7cac5782
commit 35cc291118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 16 deletions

View File

@ -261,8 +261,10 @@ export const computeMediaControls = (
}); });
} }
const assumedState = stateObj.attributes.assumed_state === true;
if ( if (
(state === "playing" || state === "paused") && (state === "playing" || state === "paused" || assumedState) &&
supportsFeature(stateObj, SUPPORT_PREVIOUS_TRACK) supportsFeature(stateObj, SUPPORT_PREVIOUS_TRACK)
) { ) {
buttons.push({ buttons.push({
@ -272,14 +274,15 @@ export const computeMediaControls = (
} }
if ( if (
(state === "playing" && !assumedState &&
((state === "playing" &&
(supportsFeature(stateObj, SUPPORT_PAUSE) || (supportsFeature(stateObj, SUPPORT_PAUSE) ||
supportsFeature(stateObj, SUPPORT_STOP))) || supportsFeature(stateObj, SUPPORT_STOP))) ||
((state === "paused" || state === "idle") && ((state === "paused" || state === "idle") &&
supportsFeature(stateObj, SUPPORT_PLAY)) || supportsFeature(stateObj, SUPPORT_PLAY)) ||
(state === "on" && (state === "on" &&
(supportsFeature(stateObj, SUPPORT_PLAY) || (supportsFeature(stateObj, SUPPORT_PLAY) ||
supportsFeature(stateObj, SUPPORT_PAUSE))) supportsFeature(stateObj, SUPPORT_PAUSE))))
) { ) {
buttons.push({ buttons.push({
icon: icon:
@ -299,8 +302,29 @@ export const computeMediaControls = (
}); });
} }
if (assumedState && supportsFeature(stateObj, SUPPORT_PLAY)) {
buttons.push({
icon: mdiPlay,
action: "media_play",
});
}
if (assumedState && supportsFeature(stateObj, SUPPORT_PAUSE)) {
buttons.push({
icon: mdiPause,
action: "media_pause",
});
}
if (assumedState && supportsFeature(stateObj, SUPPORT_STOP)) {
buttons.push({
icon: mdiStop,
action: "media_stop",
});
}
if ( if (
(state === "playing" || state === "paused") && (state === "playing" || state === "paused" || assumedState) &&
supportsFeature(stateObj, SUPPORT_NEXT_TRACK) supportsFeature(stateObj, SUPPORT_NEXT_TRACK)
) { ) {
buttons.push({ buttons.push({

View File

@ -110,10 +110,11 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
const entityState = stateObj.state; const entityState = stateObj.state;
const controlButton = this._computeControlButton(stateObj); const controlButton = this._computeControlButton(stateObj);
const assumedState = stateObj.attributes.assumed_state === true;
const buttons = html` const buttons = html`
${!this._narrow && ${!this._narrow &&
entityState === "playing" && (entityState === "playing" || assumedState) &&
supportsFeature(stateObj, SUPPORT_PREVIOUS_TRACK) supportsFeature(stateObj, SUPPORT_PREVIOUS_TRACK)
? html` ? html`
<ha-icon-button <ha-icon-button
@ -125,14 +126,15 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
></ha-icon-button> ></ha-icon-button>
` `
: ""} : ""}
${(entityState === "playing" && ${!assumedState &&
((entityState === "playing" &&
(supportsFeature(stateObj, SUPPORT_PAUSE) || (supportsFeature(stateObj, SUPPORT_PAUSE) ||
supportsFeature(stateObj, SUPPORT_STOP))) || supportsFeature(stateObj, SUPPORT_STOP))) ||
((entityState === "paused" || entityState === "idle") && ((entityState === "paused" || entityState === "idle") &&
supportsFeature(stateObj, SUPPORT_PLAY)) || supportsFeature(stateObj, SUPPORT_PLAY)) ||
(entityState === "on" && (entityState === "on" &&
(supportsFeature(stateObj, SUPPORT_PLAY) || (supportsFeature(stateObj, SUPPORT_PLAY) ||
supportsFeature(stateObj, SUPPORT_PAUSE))) supportsFeature(stateObj, SUPPORT_PAUSE))))
? html` ? html`
<ha-icon-button <ha-icon-button
.path=${controlButton.icon} .path=${controlButton.icon}
@ -143,7 +145,34 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
></ha-icon-button> ></ha-icon-button>
` `
: ""} : ""}
${entityState === "playing" && ${assumedState && supportsFeature(stateObj, SUPPORT_PLAY)
? html`
<ha-icon-button
.path=${mdiPlay}
.label=${this.hass.localize(`ui.card.media_player.media_play`)}
@click=${this._play}
></ha-icon-button>
`
: ""}
${assumedState && supportsFeature(stateObj, SUPPORT_PAUSE)
? html`
<ha-icon-button
.path=${mdiPause}
.label=${this.hass.localize(`ui.card.media_player.media_pause`)}
@click=${this._pause}
></ha-icon-button>
`
: ""}
${assumedState && supportsFeature(stateObj, SUPPORT_STOP)
? html`
<ha-icon-button
.path=${mdiStop}
.label=${this.hass.localize(`ui.card.media_player.media_stop`)}
@click=${this._stop}
></ha-icon-button>
`
: ""}
${(entityState === "playing" || assumedState) &&
supportsFeature(stateObj, SUPPORT_NEXT_TRACK) supportsFeature(stateObj, SUPPORT_NEXT_TRACK)
? html` ? html`
<ha-icon-button <ha-icon-button
@ -312,6 +341,24 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
}); });
} }
private _play(): void {
this.hass!.callService("media_player", "media_play", {
entity_id: this._config!.entity,
});
}
private _pause(): void {
this.hass!.callService("media_player", "media_pause", {
entity_id: this._config!.entity,
});
}
private _stop(): void {
this.hass!.callService("media_player", "media_stop", {
entity_id: this._config!.entity,
});
}
private _previousTrack(): void { private _previousTrack(): void {
this.hass!.callService("media_player", "media_previous_track", { this.hass!.callService("media_player", "media_previous_track", {
entity_id: this._config!.entity, entity_id: this._config!.entity,