Skip to content

Commit

Permalink
Fix Assist pipeline defaults (#21796)
Browse files Browse the repository at this point in the history
* Fix Assist pipeline defaults

* Always set to NONE if nothing

* Rewrite for readability
  • Loading branch information
balloob authored Aug 26, 2024
1 parent c416dae commit 11ace60
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 31 deletions.
38 changes: 29 additions & 9 deletions src/components/ha-conversation-agent-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,35 @@ export class HaConversationAgentPicker extends LitElement {
if (!this._agents) {
return nothing;
}
const value =
this.value ??
(this.required &&
(!this.language ||
this._agents
.find((agent) => agent.id === "homeassistant")
?.supported_languages.includes(this.language))
? "homeassistant"
: NONE);
let value = this.value;
if (!value && this.required) {
// Select Home Assistant conversation agent if it supports the language
for (const agent of this._agents) {
if (
agent.id === "conversation.home_assistant" &&
agent.supported_languages.includes(this.language!)
) {
value = agent.id;
break;
}
}
if (!value) {
// Select the first agent that supports the language
for (const agent of this._agents) {
if (
agent.supported_languages === "*" &&
agent.supported_languages.includes(this.language!)
) {
value = agent.id;
break;
}
}
}
}
if (!value) {
value = NONE;
}

return html`
<ha-select
.label=${this.label ||
Expand Down
34 changes: 27 additions & 7 deletions src/components/ha-stt-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HomeAssistant } from "../types";
import "./ha-list-item";
import "./ha-select";
import type { HaSelect } from "./ha-select";
import { computeDomain } from "../common/entity/compute_domain";

const NONE = "__NONE_OPTION__";

Expand All @@ -41,13 +42,32 @@ export class HaSTTPicker extends LitElement {
if (!this._engines) {
return nothing;
}
const value =
this.value ??
(this.required
? this._engines.find(
(engine) => engine.supported_languages?.length !== 0
)
: NONE);

let value = this.value;
if (!value && this.required) {
for (const entity of Object.values(this.hass.entities)) {
if (
entity.platform === "cloud" &&
computeDomain(entity.entity_id) === "stt"
) {
value = entity.entity_id;
break;
}
}

if (!value) {
for (const sttEngine of this._engines) {
if (sttEngine?.supported_languages?.length !== 0) {
value = sttEngine.engine_id;
break;
}
}
}
}
if (!value) {
value = NONE;
}

return html`
<ha-select
.label=${this.label ||
Expand Down
34 changes: 27 additions & 7 deletions src/components/ha-tts-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HomeAssistant } from "../types";
import "./ha-list-item";
import "./ha-select";
import type { HaSelect } from "./ha-select";
import { computeDomain } from "../common/entity/compute_domain";

const NONE = "__NONE_OPTION__";

Expand Down Expand Up @@ -44,13 +45,32 @@ export class HaTTSPicker extends LitElement {
if (!this._engines) {
return nothing;
}
const value =
this.value ??
(this.required
? this._engines.find(
(engine) => engine.supported_languages?.length !== 0
)
: NONE);

let value = this.value;
if (!value && this.required) {
for (const entity of Object.values(this.hass.entities)) {
if (
entity.platform === "cloud" &&
computeDomain(entity.entity_id) === "tts"
) {
value = entity.entity_id;
break;
}
}

if (!value) {
for (const ttsEngine of this._engines) {
if (ttsEngine?.supported_languages?.length !== 0) {
value = ttsEngine.engine_id;
break;
}
}
}
}
if (!value) {
value = NONE;
}

return html`
<ha-select
.label=${this.label ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import "./assist-pipeline-detail/assist-pipeline-detail-tts";
import "./assist-pipeline-detail/assist-pipeline-detail-wakeword";
import "./debug/assist-render-pipeline-events";
import { VoiceAssistantPipelineDetailsDialogParams } from "./show-dialog-voice-assistant-pipeline-detail";
import { computeDomain } from "../../../common/entity/compute_domain";

@customElement("dialog-voice-assistant-pipeline-detail")
export class DialogVoiceAssistantPipelineDetail extends LitElement {
Expand All @@ -54,15 +55,36 @@ export class DialogVoiceAssistantPipelineDetail extends LitElement {
if (this._params.pipeline) {
this._data = this._params.pipeline;
this._preferred = this._params.preferred;
} else {
this._data = {
language: (
this.hass.config.language || this.hass.locale.language
).substring(0, 2),
stt_engine: this._cloudActive ? "cloud" : undefined,
tts_engine: this._cloudActive ? "cloud" : undefined,
};
return;
}

let sstDefault: string | undefined;
let ttsDefault: string | undefined;
if (this._cloudActive) {
for (const entity of Object.values(this.hass.entities)) {
if (entity.platform !== "cloud") {
continue;
}
if (computeDomain(entity.entity_id) === "stt") {
sstDefault = entity.entity_id;
if (ttsDefault) {
break;
}
} else if (computeDomain(entity.entity_id) === "tts") {
ttsDefault = entity.entity_id;
if (sstDefault) {
break;
}
}
}
}
this._data = {
language: (
this.hass.config.language || this.hass.locale.language
).substring(0, 2),
stt_engine: sstDefault,
tts_engine: ttsDefault,
};
}

public closeDialog(): void {
Expand Down

0 comments on commit 11ace60

Please sign in to comment.