ha-frontend/src/util/media-player-model.html

195 lines
4.9 KiB
HTML

<script>
(function () {
window.MediaPlayerEntity = function (hass, stateObj) {
this.hass = hass;
this.stateObj = stateObj;
};
function addGetter(name, getter) {
Object.defineProperty(window.MediaPlayerEntity.prototype, name,
{ get: getter });
}
addGetter('isOff', function () {
return this.stateObj.state === 'off';
});
addGetter('isIdle', function () {
return this.stateObj.state === 'idle';
});
addGetter('isMuted', function () {
return this.stateObj.attributes.is_volume_muted;
});
addGetter('isPaused', function () {
return this.stateObj.state === 'paused';
});
addGetter('isPlaying', function () {
return this.stateObj.state === 'playing';
});
addGetter('isMusic', function () {
return this.stateObj.attributes.media_content_type === 'music';
});
addGetter('isTVShow', function () {
return this.stateObj.attributes.media_content_type === 'tvshow';
});
addGetter('hasMediaControl', function () {
return ['playing', 'paused', 'unknown'].indexOf(
this.stateObj.state) !== -1;
});
addGetter('volumeSliderValue', function () {
return this.stateObj.attributes.volume_level * 100;
});
addGetter('showProgress', function () {
return (
(this.isPlaying || this.isPaused) &&
'media_position' in this.stateObj.attributes &&
'media_position_updated_at' in this.stateObj.attributes);
});
addGetter('currentProgress', function () {
return (
this.stateObj.attributes.media_position +
((Date.now() -
new Date(this.stateObj.attributes.media_position_updated_at)) / 1000));
});
/* eslint-disable no-bitwise */
addGetter('supportsPause', function () {
return (this.stateObj.attributes.supported_media_commands & 1) !== 0;
});
addGetter('supportsVolumeSet', function () {
return (this.stateObj.attributes.supported_media_commands & 4) !== 0;
});
addGetter('supportsVolumeMute', function () {
return (this.stateObj.attributes.supported_media_commands & 8) !== 0;
});
addGetter('supportsPreviousTrack', function () {
return (this.stateObj.attributes.supported_media_commands & 16) !== 0;
});
addGetter('supportsNextTrack', function () {
return (this.stateObj.attributes.supported_media_commands & 32) !== 0;
});
addGetter('supportsTurnOn', function () {
return (this.stateObj.attributes.supported_media_commands & 128) !== 0;
});
addGetter('supportsTurnOff', function () {
return (this.stateObj.attributes.supported_media_commands & 256) !== 0;
});
addGetter('supportsPlayMedia', function () {
return (this.stateObj.attributes.supported_media_commands & 512) !== 0;
});
addGetter('supportsVolumeButtons', function () {
return (this.stateObj.attributes.supported_media_commands & 1024) !== 0;
});
addGetter('supportsPlay', function () {
return (this.stateObj.attributes.supported_media_commands & 16384) !== 0;
});
/* eslint-enable no-bitwise */
addGetter('primaryText', function () {
return this.stateObj.attributes.media_title ||
window.hassUtil.computeStateState(this.stateObj);
});
addGetter('secondaryText', function () {
if (this.isMusic) {
return this.stateObj.attributes.media_artist;
} else if (this.isTVShow) {
var text = this.stateObj.attributes.media_series_title;
if (this.stateObj.attributes.media_season) {
text += ' S' + this.stateObj.attributes.media_season;
if (this.stateObj.attributes.media_episode) {
text += 'E' + this.stateObj.attributes.media_episode;
}
}
return text;
} else if (this.stateObj.attributes.app_name) {
return this.stateObj.attributes.app_name;
}
return '';
});
Object.assign(window.MediaPlayerEntity.prototype, {
mediaPlayPause() {
this.callService('media_play_pause');
},
nextTrack() {
this.callService('media_next_track');
},
playbackControl() {
this.callService('media_play_pause');
},
previousTrack() {
this.callService('media_previous_track');
},
setVolume(volume) {
this.callService('volume_set', { volume_level: volume });
},
togglePower() {
if (this.isOff) {
this.turnOn();
} else {
this.turnOff();
}
},
turnOff() {
this.callService('turn_off');
},
turnOn() {
this.callService('turn_on');
},
volumeDown() {
this.callService('volume_down');
},
volumeMute(mute) {
if (!this.supportsVolumeMute) {
throw new Error('Muting volume not supported');
}
this.callService('volume_mute', { is_volume_muted: mute });
},
volumeUp() {
this.callService('volume_down');
},
// helper method
callService(service, data) {
var serviceData = data || {};
serviceData.entity_id = this.stateObj.entity_id;
this.hass.callService('media_player', service, serviceData);
},
});
}());
</script>