Add options flow button to conversation agent in assist pipeline dialog (#16595)

This commit is contained in:
Bram Kragten 2023-05-23 12:32:18 +02:00 committed by GitHub
parent 5a36f100a9
commit 4ccfd6a3fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import { mdiCog } from "@mdi/js";
import {
css,
CSSResultGroup,
@ -10,7 +11,10 @@ import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event";
import { stopPropagation } from "../common/dom/stop_propagation";
import { debounce } from "../common/util/debounce";
import { ConfigEntry, getConfigEntry } from "../data/config_entries";
import { Agent, listAgents } from "../data/conversation";
import { fetchIntegrationManifest } from "../data/integration";
import { showOptionsFlowDialog } from "../dialogs/config-flow/show-dialog-options-flow";
import { HomeAssistant } from "../types";
import "./ha-list-item";
import "./ha-select";
@ -34,6 +38,8 @@ export class HaConversationAgentPicker extends LitElement {
@state() _agents?: Agent[];
@state() private _configEntry?: ConfigEntry;
protected render() {
if (!this._agents) {
return nothing;
@ -77,8 +83,13 @@ export class HaConversationAgentPicker extends LitElement {
>
${agent.name}
</ha-list-item>`
)}
</ha-select>
)}</ha-select
>${this._configEntry?.supports_options
? html`<ha-icon-button
.path=${mdiCog}
@click=${this._openOptionsFlow}
></ha-icon-button>`
: ""}
`;
}
@ -89,6 +100,24 @@ export class HaConversationAgentPicker extends LitElement {
} else if (changedProperties.has("language")) {
this._debouncedUpdateAgents();
}
if (changedProperties.has("value")) {
this._maybeFetchConfigEntry();
}
}
private async _maybeFetchConfigEntry() {
if (!this.value || this.value === "homeassistant") {
this._configEntry = undefined;
return;
}
try {
this._configEntry = (
await getConfigEntry(this.hass, this.value)
).config_entry;
} catch (err) {
this._configEntry = undefined;
}
}
private _debouncedUpdateAgents = debounce(() => this._updateAgents(), 500);
@ -122,11 +151,29 @@ export class HaConversationAgentPicker extends LitElement {
}
}
private async _openOptionsFlow() {
if (!this._configEntry) {
return;
}
showOptionsFlowDialog(
this,
this._configEntry,
await fetchIntegrationManifest(this.hass, this._configEntry.domain)
);
}
static get styles(): CSSResultGroup {
return css`
:host {
display: flex;
align-items: center;
}
ha-select {
width: 100%;
}
ha-icon-button {
color: var(--secondary-text-color);
}
`;
}