Add language to conversation agent picker (#16244)

This commit is contained in:
Bram Kragten 2023-04-20 11:26:42 +02:00 committed by GitHub
parent aac28efd32
commit be005b4c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 31 deletions

View File

@ -4,11 +4,12 @@ import {
html, html,
LitElement, LitElement,
nothing, nothing,
PropertyValueMap, PropertyValues,
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import { stopPropagation } from "../common/dom/stop_propagation"; import { stopPropagation } from "../common/dom/stop_propagation";
import { debounce } from "../common/util/debounce";
import { Agent, listAgents } from "../data/conversation"; import { Agent, listAgents } from "../data/conversation";
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
import "./ha-list-item"; import "./ha-list-item";
@ -20,6 +21,8 @@ const NONE = "__NONE_OPTION__";
export class HaConversationAgentPicker extends LitElement { export class HaConversationAgentPicker extends LitElement {
@property() public value?: string; @property() public value?: string;
@property() public language?: string;
@property() public label?: string; @property() public label?: string;
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@ -30,13 +33,11 @@ export class HaConversationAgentPicker extends LitElement {
@state() _agents?: Agent[]; @state() _agents?: Agent[];
@state() _defaultAgent: string | null = null;
protected render() { protected render() {
if (!this._agents) { if (!this._agents) {
return nothing; return nothing;
} }
const value = this.value ?? (this.required ? this._defaultAgent : NONE); const value = this.value ?? (this.required ? "homeassistant" : NONE);
return html` return html`
<ha-select <ha-select
.label=${this.label || .label=${this.label ||
@ -60,20 +61,41 @@ export class HaConversationAgentPicker extends LitElement {
: nothing} : nothing}
${this._agents.map( ${this._agents.map(
(agent) => (agent) =>
html`<ha-list-item .value=${agent.id}>${agent.name}</ha-list-item>` html`<ha-list-item
.value=${agent.id}
.disabled=${agent.language_supported === false}
>
${agent.name}
</ha-list-item>`
)} )}
</ha-select> </ha-select>
`; `;
} }
protected firstUpdated( protected willUpdate(changedProperties: PropertyValues<this>): void {
changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown> super.willUpdate(changedProperties);
): void { if (!this.hasUpdated) {
super.firstUpdated(changedProperties); this._updateAgents();
listAgents(this.hass).then((agents) => { } else if (changedProperties.has("language")) {
this._agents = agents.agents; this._debouncedUpdateAgents();
this._defaultAgent = agents.default_agent; }
}); }
private _debouncedUpdateAgents = debounce(() => this._updateAgents(), 500);
private async _updateAgents() {
const { agents } = await listAgents(this.hass, this.language);
this._agents = agents;
if (
this.value &&
agents.find((agent) => agent.id === this.value)?.language_supported ===
false
) {
this.value = undefined;
fireEvent(this, "value-changed", { value: this.value });
}
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {

View File

@ -20,10 +20,16 @@ export class HaConversationAgentSelector extends LitElement {
@property({ type: Boolean }) public required = true; @property({ type: Boolean }) public required = true;
@property({ attribute: false }) public context?: {
language?: string;
};
protected render() { protected render() {
return html`<ha-conversation-agent-picker return html`<ha-conversation-agent-picker
.hass=${this.hass} .hass=${this.hass}
.value=${this.value} .value=${this.value}
.language=${this.selector.conversation_agent?.language ||
this.context?.language}
.label=${this.label} .label=${this.label}
.helper=${this.helper} .helper=${this.helper}
.disabled=${this.disabled} .disabled=${this.disabled}

View File

@ -90,8 +90,8 @@ export class HaSTTPicker extends LitElement {
if ( if (
this.value && this.value &&
!this._engines.find((engine) => engine.engine_id === this.value) this._engines.find((engine) => engine.engine_id === this.value)
?.language_supported ?.language_supported === false
) { ) {
this.value = undefined; this.value = undefined;
fireEvent(this, "value-changed", { value: this.value }); fireEvent(this, "value-changed", { value: this.value });

View File

@ -1,4 +1,3 @@
import { debounce } from "chart.js/helpers";
import { import {
css, css,
CSSResultGroup, CSSResultGroup,
@ -11,6 +10,7 @@ import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event"; import { fireEvent } from "../common/dom/fire_event";
import { stopPropagation } from "../common/dom/stop_propagation"; import { stopPropagation } from "../common/dom/stop_propagation";
import { computeStateName } from "../common/entity/compute_state_name"; import { computeStateName } from "../common/entity/compute_state_name";
import { debounce } from "../common/util/debounce";
import { listTTSEngines, TTSEngine } from "../data/tts"; import { listTTSEngines, TTSEngine } from "../data/tts";
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
import "./ha-list-item"; import "./ha-list-item";
@ -90,8 +90,8 @@ export class HaTTSPicker extends LitElement {
if ( if (
this.value && this.value &&
!this._engines.find((engine) => engine.engine_id === this.value) this._engines.find((engine) => engine.engine_id === this.value)
?.language_supported ?.language_supported === false
) { ) {
this.value = undefined; this.value = undefined;
fireEvent(this, "value-changed", { value: this.value }); fireEvent(this, "value-changed", { value: this.value });

View File

@ -59,6 +59,7 @@ export interface AgentInfo {
export interface Agent { export interface Agent {
id: string; id: string;
name: string; name: string;
language_supported: boolean;
} }
export const processConversationInput = ( export const processConversationInput = (
@ -76,10 +77,12 @@ export const processConversationInput = (
}); });
export const listAgents = ( export const listAgents = (
hass: HomeAssistant hass: HomeAssistant,
): Promise<{ agents: Agent[]; default_agent: string | null }> => language?: string
): Promise<{ agents: Agent[] }> =>
hass.callWS({ hass.callWS({
type: "conversation/agent/list", type: "conversation/agent/list",
language,
}); });
export const getAgentInfo = ( export const getAgentInfo = (

View File

@ -89,8 +89,7 @@ export interface ColorTempSelector {
} }
export interface ConversationAgentSelector { export interface ConversationAgentSelector {
// eslint-disable-next-line @typescript-eslint/ban-types conversation_agent: { language?: string } | null;
conversation_agent: {} | null;
} }
export interface ConfigEntrySelector { export interface ConfigEntrySelector {

View File

@ -129,13 +129,6 @@ export class DialogVoiceAssistantPipelineDetail extends LitElement {
text: {}, text: {},
}, },
}, },
{
name: "conversation_engine",
required: true,
selector: {
conversation_agent: {},
},
},
{ {
name: "language", name: "language",
required: true, required: true,
@ -143,6 +136,14 @@ export class DialogVoiceAssistantPipelineDetail extends LitElement {
text: {}, text: {},
}, },
}, },
{
name: "conversation_engine",
required: true,
selector: {
conversation_agent: {},
},
context: { language: "language" },
},
{ {
name: "stt_engine", name: "stt_engine",
selector: { selector: {

View File

@ -1,5 +1,5 @@
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
import { css, html, LitElement } from "lit"; import { css, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { isComponentLoaded } from "../../../common/config/is_component_loaded"; import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { computeRTLDirection } from "../../../common/util/compute_rtl"; import { computeRTLDirection } from "../../../common/util/compute_rtl";
@ -37,7 +37,9 @@ export class HaConfigVoiceAssistantsAssistants extends LitElement {
.tabs=${voiceAssistantTabs} .tabs=${voiceAssistantTabs}
> >
<div class="content"> <div class="content">
<assist-pref .hass=${this.hass}> </assist-pref> ${isComponentLoaded(this.hass, "assist_pipeline")
? html`<assist-pref .hass=${this.hass}></assist-pref>`
: nothing}
${this.cloudStatus?.logged_in ${this.cloudStatus?.logged_in
? html`<cloud-alexa-pref ? html`<cloud-alexa-pref
.hass=${this.hass} .hass=${this.hass}