Conversation dialog tweaks (#14869)

This commit is contained in:
Paulus Schoutsen 2022-12-28 07:50:38 -05:00 committed by GitHub
parent 1d1ff410b2
commit 0e9a013549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 16 deletions

View File

@ -61,12 +61,14 @@ export const processConversationInput = (
hass: HomeAssistant, hass: HomeAssistant,
text: string, text: string,
// eslint-disable-next-line: variable-name // eslint-disable-next-line: variable-name
conversation_id: string conversation_id: string | null,
language: string
): Promise<ConversationResult> => ): Promise<ConversationResult> =>
hass.callWS({ hass.callWS({
type: "conversation/process", type: "conversation/process",
text, text,
conversation_id, conversation_id,
language,
}); });
export const getAgentInfo = (hass: HomeAssistant): Promise<AgentInfo> => export const getAgentInfo = (hass: HomeAssistant): Promise<AgentInfo> =>

View File

@ -13,7 +13,6 @@ import { customElement, property, query, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; import { classMap } from "lit/directives/class-map";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { SpeechRecognition } from "../../common/dom/speech-recognition"; import { SpeechRecognition } from "../../common/dom/speech-recognition";
import { uid } from "../../common/util/uid";
import "../../components/ha-dialog"; import "../../components/ha-dialog";
import type { HaDialog } from "../../components/ha-dialog"; import type { HaDialog } from "../../components/ha-dialog";
import "../../components/ha-icon-button"; import "../../components/ha-icon-button";
@ -60,7 +59,7 @@ export class HaVoiceCommandDialog extends LitElement {
private recognition!: SpeechRecognition; private recognition!: SpeechRecognition;
private _conversationId?: string; private _conversationId: string | null = null;
public async showDialog(): Promise<void> { public async showDialog(): Promise<void> {
this._opened = true; this._opened = true;
@ -175,7 +174,6 @@ export class HaVoiceCommandDialog extends LitElement {
protected firstUpdated(changedProps: PropertyValues) { protected firstUpdated(changedProps: PropertyValues) {
super.updated(changedProps); super.updated(changedProps);
this._conversationId = uid();
this._conversation = [ this._conversation = [
{ {
who: "hass", who: "hass",
@ -211,18 +209,29 @@ export class HaVoiceCommandDialog extends LitElement {
private _initRecognition() { private _initRecognition() {
this.recognition = new SpeechRecognition(); this.recognition = new SpeechRecognition();
this.recognition.interimResults = true; this.recognition.interimResults = true;
this.recognition.lang = "en-US"; this.recognition.lang = this.hass.language;
this.recognition.onstart = () => { this.recognition.addEventListener("start", () => {
this.results = { this.results = {
final: false, final: false,
transcript: "", 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(); this.recognition!.abort();
// @ts-ignore // @ts-ignore
if (event.error !== "aborted") { if (event.error !== "aborted" && event.error !== "no-speech") {
const text = const text =
this.results && this.results.transcript this.results && this.results.transcript
? this.results.transcript ? this.results.transcript
@ -232,8 +241,8 @@ export class HaVoiceCommandDialog extends LitElement {
this._addMessage({ who: "user", text, error: true }); this._addMessage({ who: "user", text, error: true });
} }
this.results = null; this.results = null;
}; });
this.recognition.onend = () => { this.recognition.addEventListener("end", () => {
// Already handled by onerror // Already handled by onerror
if (this.results == null) { if (this.results == null) {
return; return;
@ -251,15 +260,14 @@ export class HaVoiceCommandDialog extends LitElement {
error: true, error: true,
}); });
} }
}; });
this.recognition.addEventListener("result", (event) => {
this.recognition.onresult = (event) => {
const result = event.results[0]; const result = event.results[0];
this.results = { this.results = {
transcript: result[0].transcript, transcript: result[0].transcript,
final: result.isFinal, final: result.isFinal,
}; };
}; });
} }
private async _processText(text: string) { private async _processText(text: string) {
@ -277,8 +285,10 @@ export class HaVoiceCommandDialog extends LitElement {
const response = await processConversationInput( const response = await processConversationInput(
this.hass, this.hass,
text, text,
this._conversationId! this._conversationId,
this.hass.language
); );
this._conversationId = response.conversation_id;
const plain = response.response.speech?.plain; const plain = response.response.speech?.plain;
if (plain) { if (plain) {
message.text = plain.speech; message.text = plain.speech;

View File

@ -777,6 +777,7 @@
}, },
"voice_command": { "voice_command": {
"did_not_hear": "Home Assistant did not hear anything", "did_not_hear": "Home Assistant did not hear anything",
"did_not_understand": "Didn't quite get that",
"found": "I found the following for you:", "found": "I found the following for you:",
"error": "Oops, an error has occurred", "error": "Oops, an error has occurred",
"how_can_i_help": "How can I help?", "how_can_i_help": "How can I help?",