From 0e9a013549e0a7bdebc7abd680ca128cc7ba928e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 28 Dec 2022 07:50:38 -0500 Subject: [PATCH] Conversation dialog tweaks (#14869) --- src/data/conversation.ts | 4 +- .../ha-voice-command-dialog.ts | 40 ++++++++++++------- src/translations/en.json | 1 + 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/data/conversation.ts b/src/data/conversation.ts index 4e7a504f11..b448876dfd 100644 --- a/src/data/conversation.ts +++ b/src/data/conversation.ts @@ -61,12 +61,14 @@ export const processConversationInput = ( hass: HomeAssistant, text: string, // eslint-disable-next-line: variable-name - conversation_id: string + conversation_id: string | null, + language: string ): Promise => hass.callWS({ type: "conversation/process", text, conversation_id, + language, }); export const getAgentInfo = (hass: HomeAssistant): Promise => diff --git a/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts b/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts index 49c998bcb4..91577f65e8 100644 --- a/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts +++ b/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts @@ -13,7 +13,6 @@ import { customElement, property, query, state } from "lit/decorators"; import { classMap } from "lit/directives/class-map"; import { fireEvent } from "../../common/dom/fire_event"; import { SpeechRecognition } from "../../common/dom/speech-recognition"; -import { uid } from "../../common/util/uid"; import "../../components/ha-dialog"; import type { HaDialog } from "../../components/ha-dialog"; import "../../components/ha-icon-button"; @@ -60,7 +59,7 @@ export class HaVoiceCommandDialog extends LitElement { private recognition!: SpeechRecognition; - private _conversationId?: string; + private _conversationId: string | null = null; public async showDialog(): Promise { this._opened = true; @@ -175,7 +174,6 @@ export class HaVoiceCommandDialog extends LitElement { protected firstUpdated(changedProps: PropertyValues) { super.updated(changedProps); - this._conversationId = uid(); this._conversation = [ { who: "hass", @@ -211,18 +209,29 @@ export class HaVoiceCommandDialog extends LitElement { private _initRecognition() { this.recognition = new SpeechRecognition(); this.recognition.interimResults = true; - this.recognition.lang = "en-US"; + this.recognition.lang = this.hass.language; - this.recognition.onstart = () => { + this.recognition.addEventListener("start", () => { this.results = { final: false, transcript: "", }; - }; - this.recognition.onerror = (event) => { + }); + this.recognition.addEventListener("nomatch", () => { + this._addMessage({ + who: "user", + text: `<${this.hass.localize( + "ui.dialogs.voice_command.did_not_understand" + )}>`, + error: true, + }); + }); + this.recognition.addEventListener("error", (event) => { + // eslint-disable-next-line + console.error("Error recognizing text", event); this.recognition!.abort(); // @ts-ignore - if (event.error !== "aborted") { + if (event.error !== "aborted" && event.error !== "no-speech") { const text = this.results && this.results.transcript ? this.results.transcript @@ -232,8 +241,8 @@ export class HaVoiceCommandDialog extends LitElement { this._addMessage({ who: "user", text, error: true }); } this.results = null; - }; - this.recognition.onend = () => { + }); + this.recognition.addEventListener("end", () => { // Already handled by onerror if (this.results == null) { return; @@ -251,15 +260,14 @@ export class HaVoiceCommandDialog extends LitElement { error: true, }); } - }; - - this.recognition.onresult = (event) => { + }); + this.recognition.addEventListener("result", (event) => { const result = event.results[0]; this.results = { transcript: result[0].transcript, final: result.isFinal, }; - }; + }); } private async _processText(text: string) { @@ -277,8 +285,10 @@ export class HaVoiceCommandDialog extends LitElement { const response = await processConversationInput( this.hass, text, - this._conversationId! + this._conversationId, + this.hass.language ); + this._conversationId = response.conversation_id; const plain = response.response.speech?.plain; if (plain) { message.text = plain.speech; diff --git a/src/translations/en.json b/src/translations/en.json index 92c455f6bc..a6329fb139 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -777,6 +777,7 @@ }, "voice_command": { "did_not_hear": "Home Assistant did not hear anything", + "did_not_understand": "Didn't quite get that", "found": "I found the following for you:", "error": "Oops, an error has occurred", "how_can_i_help": "How can I help?",