-
-
+
-
- {{results.final}}
- [[results.interim]]
- …
-
-
- An error occurred. Unable to fulfill request.
+
+
+
+ {{results.final}}
+ [[results.interim]]
+ …
+
+
+
+
@@ -87,28 +143,15 @@ Polymer({
results: {
type: Object,
+ value: null,
+ observer: '_scrollMessagesBottom',
},
- isTransmitting: {
- type: Boolean,
- value: false,
- },
-
- isListening: {
- type: Boolean,
- value: false,
- },
-
- hasError: {
- type: Boolean,
- value: false,
- },
-
- showListenInterface: {
- type: Boolean,
- computed: 'computeShowListenInterface(isListening, isTransmitting)',
- observer: 'showListenInterfaceChanged',
- },
+ _conversation: {
+ type: Array,
+ value: function () { return []; },
+ observer: '_scrollMessagesBottom',
+ }
},
initRecognition: function () {
@@ -117,9 +160,6 @@ Polymer({
/* eslint-enable new-cap */
this.recognition.onstart = function () {
- this.isListening = true;
- this.isTransmitting = false;
- this.hasError = false;
this.results = {
final: '',
interim: '',
@@ -127,19 +167,24 @@ Polymer({
}.bind(this);
this.recognition.onerror = function () {
this.recognition.abort();
- this.hasError = true;
+ var text = this.results.final || this.results.interim;
+ this.results = null;
+ this.push('_conversation', { who: 'user', text: text, error: true });
}.bind(this);
this.recognition.onend = function () {
- this.isListening = false;
- this.isTransmitting = true;
var text = this.results.final || this.results.interim;
+ this.results = null;
- var listeningDone = function () {
- this.isTransmitting = false;
- }.bind(this);
+ if (text === '') return;
- this.hass.callService('conversation', 'process', { text: text })
- .then(listeningDone, listeningDone);
+ this.push('_conversation', { who: 'user', text: text });
+
+ this.hass.callApi('post', 'conversation/process', { text: text })
+ .then(function (response) {
+ this.push('_conversation', { who: 'hass', text: response.speech.plain.speech });
+ }.bind(this), function () {
+ this.set(['_conversation', this._conversation.length - 1, 'error'], true);
+ }.bind(this));
}.bind(this);
this.recognition.onresult = function (event) {
@@ -167,25 +212,33 @@ Polymer({
this.initRecognition();
}
+ this.results = {
+ interim: '',
+ final: '',
+ };
this.recognition.start();
},
- computeShowListenInterface: function (isListening, isTransmitting) {
- return isListening || isTransmitting;
+ _scrollMessagesBottom: function () {
+ this.async(function () {
+ this.$.messages.scrollTop = this.$.messages.scrollHeight;
+
+ if (this.$.messages.scrollTop !== 0) {
+ this.$.dialog.fire('iron-resize');
+ }
+ }.bind(this), 10);
},
dialogOpenChanged: function (newVal) {
- if (!newVal && this.isListening) {
+ if (newVal) {
+ this.startListening();
+ } else if (!newVal && this.results) {
this.recognition.abort();
}
},
- showListenInterfaceChanged: function (newVal) {
- if (!newVal && this.dialogOpen) {
- this.dialogOpen = false;
- } else if (newVal) {
- this.dialogOpen = true;
- }
- },
+ _computeMessageClasses: function (message) {
+ return 'message ' + message.who + (message.error ? ' error' : '');
+ }
});
diff --git a/src/layouts/home-assistant-main.html b/src/layouts/home-assistant-main.html
index 795aa66ce1..0fce5c646b 100644
--- a/src/layouts/home-assistant-main.html
+++ b/src/layouts/home-assistant-main.html
@@ -133,7 +133,7 @@
handleStartVoice: function (ev) {
ev.stopPropagation();
- this.$.voiceDialog.startListening();
+ this.$.voiceDialog.dialogOpen = true;
},
handleOpenMenu: function () {