Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last_used option for pipeline and explicit default #17329

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/components/ha-assist-pipeline-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import "./ha-list-item";
import "./ha-select";
import type { HaSelect } from "./ha-select";

const PREFERRED = "__PREFERRED_PIPELINE_OPTION__";
const PREFERRED = "preferred";
const LAST_USED = "last_used";

@customElement("ha-assist-pipeline-picker")
export class HaAssistPipelinePicker extends LitElement {
Expand All @@ -30,15 +31,21 @@ export class HaAssistPipelinePicker extends LitElement {

@property({ type: Boolean }) public required = false;

@property() public includeLastUsed = false;

@state() _pipelines?: AssistPipeline[];

@state() _preferredPipeline: string | null = null;

private get _default() {
return this.includeLastUsed ? LAST_USED : PREFERRED;
}

protected render() {
if (!this._pipelines) {
return nothing;
}
const value = this.value ?? PREFERRED;
const value = this.value ?? this._default;
return html`
<ha-select
.label=${this.label ||
Expand All @@ -51,6 +58,15 @@ export class HaAssistPipelinePicker extends LitElement {
fixedMenuPosition
naturalMenuWidth
>
${this.includeLastUsed
? html`
<ha-list-item .value=${LAST_USED}>
${this.hass!.localize(
"ui.components.pipeline-picker.last_used"
)}
</ha-list-item>
`
: null}
<ha-list-item .value=${PREFERRED}>
${this.hass!.localize("ui.components.pipeline-picker.preferred", {
preferred: this._pipelines.find(
Expand Down Expand Up @@ -93,11 +109,11 @@ export class HaAssistPipelinePicker extends LitElement {
!this.hass ||
target.value === "" ||
target.value === this.value ||
(this.value === undefined && target.value === PREFERRED)
(this.value === undefined && target.value === this._default)
) {
return;
}
this.value = target.value === PREFERRED ? undefined : target.value;
this.value = target.value === this._default ? undefined : target.value;
fireEvent(this, "value-changed", { value: this.value });
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/components/ha-selector/ha-selector-assist-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ export class HaAssistPipelineSelector extends LitElement {
@property({ type: Boolean }) public required = true;

protected render() {
return html`<ha-assist-pipeline-picker
.hass=${this.hass}
.value=${this.value}
.label=${this.label}
.helper=${this.helper}
.disabled=${this.disabled}
.required=${this.required}
></ha-assist-pipeline-picker>`;
return html`
<ha-assist-pipeline-picker
.hass=${this.hass}
.value=${this.value}
.label=${this.label}
.helper=${this.helper}
.disabled=${this.disabled}
.required=${this.required}
.includeLastUsed=${Boolean(
this.selector.assist_pipeline?.include_last_used
)}
></ha-assist-pipeline-picker>
`;
}

static styles = css`
Expand Down
4 changes: 3 additions & 1 deletion src/data/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ export interface ObjectSelector {

export interface AssistPipelineSelector {
// eslint-disable-next-line @typescript-eslint/ban-types
assist_pipeline: {} | null;
assist_pipeline: {
include_last_used?: boolean;
} | null;
}

export interface SelectOption {
Expand Down
19 changes: 15 additions & 4 deletions src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ export class HaVoiceCommandDialog extends LitElement {

private _pipelinePromise?: Promise<AssistPipeline>;

public async showDialog(params?: VoiceCommandDialogParams): Promise<void> {
if (params?.pipeline_id) {
this._pipelineId = params?.pipeline_id;
public async showDialog(
params: Required<VoiceCommandDialogParams>
): Promise<void> {
if (params.pipeline_id === "last_used") {
// Do not set pipeline id (retrieve from storage)
} else if (params.pipeline_id === "preferred") {
await this._loadPipelines();
this._pipelineId = this._preferredPipeline;
} else {
this._pipelineId = params.pipeline_id;
}

this._conversation = [
Expand All @@ -103,7 +110,11 @@ export class HaVoiceCommandDialog extends LitElement {
this._scrollMessagesBottom();

await this._pipelinePromise;
if (params?.start_listening && this._pipeline?.stt_engine) {
if (
params?.start_listening &&
this._pipeline?.stt_engine &&
AudioRecorder.isSupported
) {
this._toggleListening();
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/dialogs/voice-command-dialog/show-ha-voice-command-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ import { HomeAssistant } from "../../types";
const loadVoiceCommandDialog = () => import("./ha-voice-command-dialog");

export interface VoiceCommandDialogParams {
pipeline_id?: string;
pipeline_id: "last_used" | "preferred" | string;
start_listening?: boolean;
}

export const showVoiceCommandDialog = (
element: HTMLElement,
hass: HomeAssistant,
dialogParams?: VoiceCommandDialogParams
dialogParams: VoiceCommandDialogParams
): void => {
if (hass.auth.external?.config.hasAssist) {
hass.auth.external!.fireMessage({
type: "assist/show",
payload: {
pipeline_id: dialogParams?.pipeline_id,
start_listening: dialogParams?.start_listening,
pipeline_id: dialogParams.pipeline_id,
// Start listening by default for app
start_listening: dialogParams.start_listening ?? true,
},
});
return;
}
fireEvent(element, "show-dialog", {
dialogTag: "ha-voice-command-dialog",
dialogImport: loadVoiceCommandDialog,
dialogParams,
dialogParams: {
pipeline_id: dialogParams.pipeline_id,
// Don't start listening by default for web
start_listening: dialogParams.start_listening ?? false,
},
});
};
4 changes: 2 additions & 2 deletions src/external_app/external_messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ interface EMOutgoingMessageSidebarShow extends EMMessage {
interface EMOutgoingMessageAssistShow extends EMMessage {
type: "assist/show";
payload?: {
pipeline_id?: string;
start_listening?: boolean;
pipeline_id: "preferred" | "last_used" | string;
start_listening: boolean;
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/common/handle-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ export const handleAction = async (
}
case "assist": {
showVoiceCommandDialog(node, hass, {
start_listening: actionConfig.start_listening,
pipeline_id: actionConfig.pipeline_id,
start_listening: actionConfig.start_listening ?? false,
pipeline_id: actionConfig.pipeline_id ?? "last_used",
});
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/panels/lovelace/components/hui-action-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const ASSIST_SCHEMA = [
{
name: "pipeline_id",
selector: {
assist_pipeline: {},
assist_pipeline: {
include_last_used: true,
},
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/panels/lovelace/hui-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class HUIRoot extends LitElement {
if (searchParams.edit === "1") {
this.lovelace!.setEditMode(true);
} else if (searchParams.conversation === "1") {
showVoiceCommandDialog(this, this.hass);
this._showVoiceCommandDialog();
window.history.replaceState(
null,
"",
Expand Down Expand Up @@ -793,7 +793,7 @@ class HUIRoot extends LitElement {
}

private _showVoiceCommandDialog(): void {
showVoiceCommandDialog(this, this.hass);
showVoiceCommandDialog(this, this.hass, { pipeline_id: "last_used" });
}

private _handleEnableEditMode(ev: CustomEvent<RequestSelectedDetail>): void {
Expand Down
2 changes: 1 addition & 1 deletion src/panels/shopping-list/ha-panel-shopping-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PanelShoppingList extends LitElement {
}

private _showVoiceCommandDialog(): void {
showVoiceCommandDialog(this, this.hass);
showVoiceCommandDialog(this, this.hass, { pipeline_id: "last_used" });
}

static get styles(): CSSResultGroup {
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@
},
"pipeline-picker": {
"pipeline": "Assistant",
"preferred": "Preferred assistant ({preferred})"
"preferred": "Preferred assistant ({preferred})",
"last_used": "Last used assistant"
},
"theme-picker": {
"theme": "Theme",
Expand Down