Add TTS support to more info media player

This commit is contained in:
Paulus Schoutsen 2016-12-13 22:30:42 -08:00
parent 336e974fe6
commit b8a7c6624d
2 changed files with 70 additions and 13 deletions

@ -1 +1 @@
Subproject commit 76f05f15fda7bcb15c43f9f529e3fdb8adc92d98 Subproject commit 299199f12f4642bdd2b80fbb23c5dcc155ae095d

View File

@ -12,7 +12,7 @@
<dom-module id='more-info-media_player'> <dom-module id='more-info-media_player'>
<template> <template>
<style is="custom-style" include="iron-flex"></style> <style is="custom-style" include="iron-flex iron-flex-alignment"></style>
<style> <style>
.media-state { .media-state {
text-transform: capitalize; text-transform: capitalize;
@ -66,6 +66,7 @@
</template> </template>
</div> </div>
</div> </div>
<!-- VOLUME -->
<div class='volume_buttons center horizontal layout' <div class='volume_buttons center horizontal layout'
hidden$='[[computeHideVolumeButtons(isOff, supportsVolumeButtons)]]'> hidden$='[[computeHideVolumeButtons(isOff, supportsVolumeButtons)]]'>
<paper-icon-button on-tap="handleVolumeTap" <paper-icon-button on-tap="handleVolumeTap"
@ -86,7 +87,9 @@
on-change='volumeSliderChanged' class='flex'> on-change='volumeSliderChanged' class='flex'>
</paper-slider> </paper-slider>
</div> </div>
<div class='controls layout horizontal justified' hidden$='[[!computeHideSelectSource(isOff, supportsSelectSource)]]'> <!-- SOURCE PICKER -->
<div class='controls layout horizontal justified'
hidden$='[[computeHideSelectSource(isOff, supportsSelectSource)]]'>
<iron-icon class="source-input" icon="mdi:login-variant"></iron-icon> <iron-icon class="source-input" icon="mdi:login-variant"></iron-icon>
<paper-dropdown-menu class="source-input" label-float label='Source'> <paper-dropdown-menu class="source-input" label-float label='Source'>
<paper-menu class="dropdown-content" selected="{{sourceIndex}}"> <paper-menu class="dropdown-content" selected="{{sourceIndex}}">
@ -96,6 +99,11 @@
</paper-menu> </paper-menu>
</paper-dropdown-menu> </paper-dropdown-menu>
</div> </div>
<!-- TTS -->
<div hidden$='[[computeHideTTS(ttsLoaded, supportsPlayMedia)]]' class='layout horizontal end'>
<paper-input label='Text to speak' class='flex' value='{{ttsMessage}}'></paper-input>
<paper-icon-button icon='mdi:send' on-tap='sendTTS'></paper-icon-button>
</div>
</div> </div>
</template> </template>
</dom-module> </dom-module>
@ -104,7 +112,16 @@
Polymer({ Polymer({
is: 'more-info-media_player', is: 'more-info-media_player',
behaviors: [window.hassBehavior],
properties: { properties: {
ttsLoaded: {
type: Boolean,
bindNuclear: function (hass) {
return hass.configGetters.isComponentLoaded('tts');
},
},
hass: { hass: {
type: Object, type: Object,
}, },
@ -145,6 +162,11 @@ Polymer({
value: 0, value: 0,
}, },
ttsMessage: {
type: String,
value: '',
},
supportsPause: { supportsPause: {
type: Boolean, type: Boolean,
value: false, value: false,
@ -180,6 +202,11 @@ Polymer({
value: false, value: false,
}, },
supportsPlayMedia: {
type: Boolean,
value: false,
},
supportsVolumeButtons: { supportsVolumeButtons: {
type: Boolean, type: Boolean,
value: false, value: false,
@ -214,6 +241,7 @@ Polymer({
this.supportsNextTrack = (newVal.attributes.supported_media_commands & 32) !== 0; this.supportsNextTrack = (newVal.attributes.supported_media_commands & 32) !== 0;
this.supportsTurnOn = (newVal.attributes.supported_media_commands & 128) !== 0; this.supportsTurnOn = (newVal.attributes.supported_media_commands & 128) !== 0;
this.supportsTurnOff = (newVal.attributes.supported_media_commands & 256) !== 0; this.supportsTurnOff = (newVal.attributes.supported_media_commands & 256) !== 0;
this.supportsPlayMedia = (newVal.attributes.supported_media_commands & 512) !== 0;
this.supportsVolumeButtons = (newVal.attributes.supported_media_commands & 1024) !== 0; this.supportsVolumeButtons = (newVal.attributes.supported_media_commands & 1024) !== 0;
this.supportsSelectSource = (newVal.attributes.supported_media_commands & 2048) !== 0; this.supportsSelectSource = (newVal.attributes.supported_media_commands & 2048) !== 0;
@ -259,13 +287,17 @@ Polymer({
}, },
computeHideSelectSource: function (isOff, supportsSelectSource) { computeHideSelectSource: function (isOff, supportsSelectSource) {
return !isOff && supportsSelectSource; return isOff || !supportsSelectSource;
}, },
computeSelectedSource: function (stateObj) { computeSelectedSource: function (stateObj) {
return stateObj.attributes.source_list.indexOf(stateObj.attributes.source); return stateObj.attributes.source_list.indexOf(stateObj.attributes.source);
}, },
computeHideTTS: function (ttsLoaded, supportsPlayMedia) {
return !ttsLoaded || !supportsPlayMedia;
},
handleTogglePower: function () { handleTogglePower: function () {
this.callService(this.isOff ? 'turn_on' : 'turn_off'); this.callService(this.isOff ? 'turn_on' : 'turn_off');
}, },
@ -334,6 +366,31 @@ Polymer({
this.callService('volume_set', { volume_level: vol }); this.callService('volume_set', { volume_level: vol });
}, },
sendTTS: function (ev) {
var services = hass.reactor.evaluate(
hass.serviceGetters.entityMap).get('tts').get('services').keySeq().toArray();
var service;
for (var i = 0; i < services.length; i++) {
if (services[i].indexOf('_say') !== -1) {
service = services[i];
break;
}
}
if (!service) {
alert('Unable to find TTS say service');
return;
}
this.hass.serviceActions.callService('tts', service, {
entity_id: this.stateObj.entityId,
message: this.ttsMessage,
});
this.ttsMessage = '';
document.activeElement.blur();
},
callService: function (service, data) { callService: function (service, data) {
var serviceData = data || {}; var serviceData = data || {};
serviceData.entity_id = this.stateObj.entityId; serviceData.entity_id = this.stateObj.entityId;