Skip to content

Commit 42281d0

Browse files
committed
feat(config-panel): add reset functionality for helper categories to default values
1 parent a1c2dbe commit 42281d0

File tree

2 files changed

+119
-9
lines changed

2 files changed

+119
-9
lines changed

src/core/helpers/lcards-helper-manager.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ export class LCARdSHelperManager extends BaseService {
319319

320320
for (const helper of allHelpers) {
321321
try {
322-
const result = await ensureHelper(this.hass, helper);
322+
// Delegate to this.ensureHelper so default values are set on creation
323+
const result = await this.ensureHelper(helper.key);
323324

324325
if (result.exists) {
325326
results.existing++;
@@ -358,7 +359,76 @@ export class LCARdSHelperManager extends BaseService {
358359
throw new Error(`[HelperManager] Helper not found in registry: ${key}`);
359360
}
360361

361-
return await ensureHelper(this.hass, definition);
362+
// Helpers with ws_create_params === null cannot be created via the API
363+
// (e.g. template sensors — must be configured manually in configuration.yaml)
364+
if (definition.ws_create_params === null) {
365+
lcardsLog.debug(`[HelperManager] Skipping non-creatable helper: ${key} (${definition.entity_id})`);
366+
return {
367+
exists: apiHelperExists(this.hass, definition.entity_id),
368+
created: false,
369+
skipped: true,
370+
entity_id: definition.entity_id
371+
};
372+
}
373+
374+
const result = await ensureHelper(this.hass, definition);
375+
376+
// Set the registry default_value whenever a helper is freshly created
377+
if (result.created && definition.default_value !== undefined && definition.default_value !== null) {
378+
try {
379+
// Small delay to let HA register the entity before we write to it
380+
await new Promise(resolve => setTimeout(resolve, 300));
381+
await apiSetHelperValue(this.hass, definition.entity_id, definition.default_value);
382+
lcardsLog.info(`[HelperManager] Set default value for new helper ${key}: ${definition.default_value}`);
383+
} catch (error) {
384+
lcardsLog.warn(`[HelperManager] Could not set default value for ${key}:`, error);
385+
}
386+
}
387+
388+
return result;
389+
}
390+
391+
/**
392+
* Reset all helpers in a category (or all helpers) to their registry default values.
393+
* Only affects helpers that already exist in Home Assistant.
394+
*
395+
* @param {string|null} category - Category name, or null to reset all
396+
* @returns {Promise<{success: number, failed: number, skipped: number}>}
397+
*/
398+
async resetCategoryToDefaults(category = null) {
399+
if (!this.hass) {
400+
throw new Error('[HelperManager] Cannot reset helpers - HASS not available');
401+
}
402+
403+
const helpers = category ? getHelpersByCategory(category) : getAllHelpers();
404+
const results = { success: 0, failed: 0, skipped: 0 };
405+
406+
for (const helper of helpers) {
407+
// Skip helpers that don't exist yet
408+
if (!apiHelperExists(this.hass, helper.entity_id)) {
409+
results.skipped++;
410+
continue;
411+
}
412+
413+
// Skip helpers with no defined default
414+
if (helper.default_value === undefined || helper.default_value === null) {
415+
results.skipped++;
416+
continue;
417+
}
418+
419+
try {
420+
await apiSetHelperValue(this.hass, helper.entity_id, helper.default_value);
421+
this._valueCache.set(helper.key, helper.default_value);
422+
lcardsLog.debug(`[HelperManager] Reset ${helper.key} -> ${helper.default_value}`);
423+
results.success++;
424+
} catch (error) {
425+
lcardsLog.error(`[HelperManager] Failed to reset ${helper.key}:`, error);
426+
results.failed++;
427+
}
428+
}
429+
430+
lcardsLog.info(`[HelperManager] Reset complete: ${results.success} reset, ${results.skipped} skipped, ${results.failed} failed`);
431+
return results;
362432
}
363433

364434
// ===== PUBLIC API: STATE ACCESS =====

src/panels/lcards-config-panel.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,36 @@ export class LCARdSConfigPanel extends LitElement {
623623
}
624624
}
625625

626+
async _resetCategoryToDefaults(category) {
627+
if (!window.lcards?.core?.helperManager) {
628+
this._showError('Helper Manager not available');
629+
return;
630+
}
631+
632+
const helperManager = window.lcards.core.helperManager;
633+
634+
try {
635+
const results = await helperManager.resetCategoryToDefaults(category);
636+
637+
if (results.success > 0) {
638+
this._showSuccess(`Reset ${results.success} helper(s) to defaults`);
639+
} else if (results.skipped > 0 && results.failed === 0) {
640+
this._showSuccess('All helpers already at defaults (or none exist yet)');
641+
}
642+
643+
if (results.failed > 0) {
644+
this._showError(`Failed to reset ${results.failed} helper(s)`);
645+
}
646+
647+
// Reload helper status to reflect new values
648+
await this._loadHelperStatus();
649+
this.requestUpdate();
650+
} catch (error) {
651+
lcardsLog.error('[ConfigPanel] Failed to reset category to defaults:', error);
652+
this._showError(`Error: ${error.message}`);
653+
}
654+
}
655+
626656
async _setHelperValue(key, value) {
627657
if (!window.lcards?.core?.helperManager) {
628658
return;
@@ -895,6 +925,14 @@ export class LCARdSConfigPanel extends LitElement {
895925
.totalCount=${totalCount}
896926
?expanded=${isExpanded}
897927
@toggle=${() => this._toggleCategory(category)}>
928+
<div class="category-actions" style="display: flex; justify-content: flex-end; padding: 4px 0 8px;">
929+
<ha-button
930+
@click=${(e) => { e.stopPropagation(); this._resetCategoryToDefaults(category); }}
931+
title="Reset all helpers in this section to their registry default values">
932+
<ha-icon slot="start" icon="mdi:restore"></ha-icon>
933+
Reset to Defaults
934+
</ha-button>
935+
</div>
898936
<table class="helper-table">
899937
<thead>
900938
<tr>
@@ -1033,20 +1071,22 @@ export class LCARdSConfigPanel extends LitElement {
10331071
${helper.exists ? this._renderValueControl(helper) : '-'}
10341072
</td>
10351073
<td>
1036-
${!helper.exists ? html`
1074+
${helper.exists ? html`
1075+
<ha-button
1076+
@click=${() => this._showMoreInfo(helper.entity_id)}
1077+
>
1078+
<ha-icon slot="start" icon="mdi:magnify"></ha-icon>
1079+
Inspect
1080+
</ha-button>
1081+
` : helper.ws_create_params !== null ? html`
10371082
<ha-button
10381083
@click=${() => this._createHelper(helper.key)}
10391084
>
10401085
<ha-icon slot="start" icon="mdi:plus"></ha-icon>
10411086
Create
10421087
</ha-button>
10431088
` : html`
1044-
<ha-button
1045-
@click=${() => this._showMoreInfo(helper.entity_id)}
1046-
>
1047-
<ha-icon slot="start" icon="mdi:magnify"></ha-icon>
1048-
Inspect
1049-
</ha-button>
1089+
<span style="font-size: 12px; color: var(--secondary-text-color); font-style: italic;">Manual setup required</span>
10501090
`}
10511091
</td>
10521092
</tr>

0 commit comments

Comments
 (0)