Fix backup custom retention save issue (#25368)

This commit is contained in:
Wendelin 2025-05-08 16:50:31 +02:00 committed by Bram Kragten
parent 3fe907f388
commit 1508c7c905
2 changed files with 28 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { css, html, LitElement, nothing, type PropertyValues } from "lit"; import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
import { fireEvent } from "../../../../../common/dom/fire_event"; import { fireEvent } from "../../../../../common/dom/fire_event";
import { clamp } from "../../../../../common/number/clamp"; import { clamp } from "../../../../../common/number/clamp";
import "../../../../../components/ha-expansion-panel"; import "../../../../../components/ha-expansion-panel";
@ -8,6 +8,7 @@ import "../../../../../components/ha-md-select";
import type { HaMdSelect } from "../../../../../components/ha-md-select"; import type { HaMdSelect } from "../../../../../components/ha-md-select";
import "../../../../../components/ha-md-select-option"; import "../../../../../components/ha-md-select-option";
import "../../../../../components/ha-md-textfield"; import "../../../../../components/ha-md-textfield";
import type { HaMdTextfield } from "../../../../../components/ha-md-textfield";
import type { BackupConfig, Retention } from "../../../../../data/backup"; import type { BackupConfig, Retention } from "../../../../../data/backup";
import type { HomeAssistant } from "../../../../../types"; import type { HomeAssistant } from "../../../../../types";
@ -54,16 +55,21 @@ class HaBackupConfigRetention extends LitElement {
@state() private _value = 3; @state() private _value = 3;
@query("#value") private _customValueField?: HaMdTextfield;
@query("#type") private _customTypeField?: HaMdSelect;
private _configLoaded = false;
private presetOptions = [ private presetOptions = [
RetentionPreset.COPIES_3, RetentionPreset.COPIES_3,
RetentionPreset.FOREVER, RetentionPreset.FOREVER,
RetentionPreset.CUSTOM, RetentionPreset.CUSTOM,
]; ];
public willUpdate(properties: PropertyValues) { public willUpdate() {
super.willUpdate(properties); if (!this._configLoaded && this.retention !== undefined) {
this._configLoaded = true;
if (!this.hasUpdated) {
if (!this.retention) { if (!this.retention) {
this._preset = RetentionPreset.GLOBAL; this._preset = RetentionPreset.GLOBAL;
} else if ( } else if (
@ -94,6 +100,10 @@ class HaBackupConfigRetention extends LitElement {
} }
protected render() { protected render() {
if (!this._configLoaded) {
return nothing;
}
return html` return html`
<ha-md-list-item> <ha-md-list-item>
<span slot="headline"> <span slot="headline">
@ -206,10 +216,12 @@ class HaBackupConfigRetention extends LitElement {
const clamped = clamp(value, MIN_VALUE, MAX_VALUE); const clamped = clamp(value, MIN_VALUE, MAX_VALUE);
target.value = clamped.toString(); target.value = clamped.toString();
const type = this._customTypeField?.value;
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: { value: {
copies: this._type === "copies" ? clamped : null, copies: type === "copies" ? clamped : null,
days: this._type === "days" ? clamped : null, days: type === "days" ? clamped : null,
}, },
}); });
} }
@ -219,10 +231,12 @@ class HaBackupConfigRetention extends LitElement {
const target = ev.currentTarget as HaMdSelect; const target = ev.currentTarget as HaMdSelect;
const type = target.value as "copies" | "days"; const type = target.value as "copies" | "days";
const value = this._customValueField?.value;
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: { value: {
copies: type === "copies" ? this._value : null, copies: type === "copies" ? Number(value) : null,
days: type === "days" ? this._value : null, days: type === "days" ? Number(value) : null,
}, },
}); });
} }

View File

@ -125,8 +125,10 @@ class HaConfigBackupDetails extends LitElement {
{ location: agentName } { location: agentName }
)} )}
.hass=${this.hass} .hass=${this.hass}
.retention=${this.config?.agents[this.agentId] .retention=${!this.config
?.retention} ? undefined
: this.config.agents[this.agentId]?.retention ||
null}
@value-changed=${this._retentionChanged} @value-changed=${this._retentionChanged}
></ha-backup-config-retention>`} ></ha-backup-config-retention>`}
</ha-card> </ha-card>