Fetch media player image from websocket (#1137)

This commit is contained in:
Paulus Schoutsen 2018-05-03 16:03:48 -04:00 committed by Pascal Vizeli
parent 3e90db5fa3
commit 9fc3d9d019

View File

@ -238,37 +238,25 @@ class HaMediaPlayerCard extends
}; };
} }
playerObjChanged(playerObj) { playerObjChanged(playerObj, oldPlayerObj) {
var picture = playerObj.stateObj.attributes.entity_picture; const picture = playerObj.stateObj.attributes.entity_picture;
var dummy; const oldPicture = oldPlayerObj && oldPlayerObj.stateObj.attributes.entity_picture;
if (picture) {
dummy = document.createElement('IMG'); if (picture !== oldPicture && !picture) {
dummy.onload = () => {
this.$.cover.style.backgroundImage = 'url(' + picture + ')';
dummy.onerror = dummy.onload = null;
dummy.src = '';
dummy = null;
};
dummy.onerror = () => {
this.$.cover.style.backgroundImage = ''; this.$.cover.style.backgroundImage = '';
this.$.cover.parentElement.classList.add('no-cover'); } else if (picture !== oldPicture) {
dummy.onerror = dummy.onload = null; // We have a new picture url
dummy.src = ''; this.hass.connection.sendMessagePromise({
dummy = null; type: 'media_player_thumbnail',
}; entity_id: playerObj.stateObj.entity_id,
if (this._timeout_id) { }).then((resp) => {
clearTimeout(this._timeout_id); if (resp.success) {
} this.$.cover.style.backgroundImage = `url(data:${resp.result.content_type};base64,${resp.result.content})`;
this._timeout_id = setTimeout(() => {
if (dummy) {
// Background load still inflight. Clear real image.
this.$.cover.style.backgroundImage = '';
}
this._timeout_id = null;
}, 5000);
dummy.src = picture;
} else { } else {
this.$.cover.style.backgroundImage = ''; this.$.cover.style.backgroundImage = '';
this.$.cover.parentElement.classList.add('no-cover');
}
});
} }
if (playerObj.isPlaying && playerObj.showProgress) { if (playerObj.isPlaying && playerObj.showProgress) {