From 37def6d3e41ab7d80cfb5a211224fff9833f4d21 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 6 Aug 2025 14:27:58 +0200 Subject: [PATCH] add save button to AI suggestions settings (#26407) --- src/panels/config/core/ai-task-pref.ts | 53 +++++++++++++++++++------- src/translations/en.json | 4 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/panels/config/core/ai-task-pref.ts b/src/panels/config/core/ai-task-pref.ts index ec99cfa907..51fcabe8d6 100644 --- a/src/panels/config/core/ai-task-pref.ts +++ b/src/panels/config/core/ai-task-pref.ts @@ -2,19 +2,20 @@ import "@material/mwc-button"; import { mdiHelpCircle, mdiStarFourPoints } from "@mdi/js"; import { css, html, LitElement } from "lit"; import { customElement, property, state } from "lit/decorators"; -import "../../../components/ha-card"; -import "../../../components/ha-settings-row"; +import { isComponentLoaded } from "../../../common/config/is_component_loaded"; +import type { HaProgressButton } from "../../../components/buttons/ha-progress-button"; import "../../../components/entity/ha-entity-picker"; import type { HaEntityPicker } from "../../../components/entity/ha-entity-picker"; -import type { HomeAssistant } from "../../../types"; -import { brandsUrl } from "../../../util/brands-url"; +import "../../../components/ha-card"; +import "../../../components/ha-settings-row"; import { fetchAITaskPreferences, saveAITaskPreferences, type AITaskPreferences, } from "../../../data/ai_task"; +import type { HomeAssistant } from "../../../types"; +import { brandsUrl } from "../../../util/brands-url"; import { documentationUrl } from "../../../util/documentation-url"; -import { isComponentLoaded } from "../../../common/config/is_component_loaded"; @customElement("ai-task-pref") export class AITaskPref extends LitElement { @@ -24,6 +25,8 @@ export class AITaskPref extends LitElement { @state() private _prefs?: AITaskPreferences; + private _gen_data_entity_id?: string | null; + protected firstUpdated(changedProps) { super.firstUpdated(changedProps); if (!this.hass || !isComponentLoaded(this.hass, "ai_task")) { @@ -86,30 +89,51 @@ export class AITaskPref extends LitElement { .hass=${this.hass} .disabled=${this._prefs === undefined && isComponentLoaded(this.hass, "ai_task")} - .value=${this._prefs?.gen_data_entity_id} + .value=${this._gen_data_entity_id || + this._prefs?.gen_data_entity_id} .includeDomains=${["ai_task"]} @value-changed=${this._handlePrefChange} > +
+ + ${this.hass!.localize("ui.common.save")} + +
`; } - private async _handlePrefChange( - ev: CustomEvent<{ value: string | undefined }> - ) { + private _handlePrefChange(ev: CustomEvent<{ value: string | undefined }>) { const input = ev.target as HaEntityPicker; - const key = input.getAttribute("data-name") as keyof AITaskPreferences; - const entityId = ev.detail.value || null; + const key = input.dataset.name as keyof AITaskPreferences; + const value = ev.detail.value || null; + this[`_${key}`] = value; + } + + private async _update(ev) { + const button = ev.target as HaProgressButton; + if (button.progress) { + return; + } + button.progress = true; + const oldPrefs = this._prefs; - this._prefs = { ...this._prefs!, [key]: entityId }; + const update: Partial = { + gen_data_entity_id: this._gen_data_entity_id, + }; + this._prefs = { ...this._prefs!, ...update }; try { this._prefs = await saveAITaskPreferences(this.hass, { - [key]: entityId, + ...update, }); + button.actionSuccess(); } catch (_err: any) { + button.actionError(); this._prefs = oldPrefs; + } finally { + button.progress = false; } } @@ -145,6 +169,9 @@ export class AITaskPref extends LitElement { direction: var(--direction); color: var(--secondary-text-color); } + .card-actions { + text-align: right; + } ha-entity-picker { flex: 1; margin-left: 16px; diff --git a/src/translations/en.json b/src/translations/en.json index ebb4c29f3e..5cd2b2624f 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2275,9 +2275,9 @@ }, "ai_task": { "header": "AI suggestions", - "description": "Home Assistant can use generative AI to help you with tasks like writing automations, creating scripts, and more. Look for the button with the {button} icon throughout Home Assistant to get suggestions.", + "description": "Home Assistant can use generative AI to help you with tasks. Look for the button with the {button} icon throughout Home Assistant to get suggestions. Select an AI task entity to use this feature.", "gen_data_header": "Data generation tasks", - "gen_data_description": "Suggest automation names or dashboards." + "gen_data_description": "Suggest automation names." }, "category": { "caption": "Categories",