Skip to content
Merged
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
44 changes: 34 additions & 10 deletions src/components/knx-sync-state-selector-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@ export class KnxSyncStateSelectorRow extends LitElement {
key: string,
) => key;

private _strategy: boolean | "init" | "expire" | "every" = true;
// Note: Strategy values are strings to maintain compatibility with ha-selector-select.
// Backend API expects booleans for true/false, conversion happens in _handleChange().
private _strategy: "true" | "false" | "init" | "expire" | "every" = "true";

private _minutes = 60;

private get _options(): (boolean | "init" | "expire" | "every")[] {
private get _options(): readonly string[] {
return this.allowFalse
? [true, "init", "expire", "every", false]
: [true, "init", "expire", "every"];
? ["true", "init", "expire", "every", "false"]
: ["true", "init", "expire", "every"];
}

protected _hasMinutes(strategy: boolean | string): boolean {
protected _hasMinutes(strategy: string): boolean {
return strategy === "expire" || strategy === "every";
}

protected willUpdate() {
// Convert incoming boolean values from backend to string representation
if (typeof this.value === "boolean") {
this._strategy = this.value;
this._strategy = this.value ? "true" : "false";
return;
}
const [strategy, minutes] = this.value.split(" ");
this._strategy = strategy;

// Validate strategy value before assignment
const validStrategies = ["true", "false", "init", "expire", "every"] as const;
if (validStrategies.includes(strategy as any)) {
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using as any defeats TypeScript's type safety. Use a type predicate or assertion function instead: if (validStrategies.includes(strategy as (typeof validStrategies)[number]))

Copilot uses AI. Check for mistakes.
this._strategy = strategy as typeof this._strategy;
} else {
// Fallback to default for invalid values
this._strategy = "true";
}

if (+minutes) {
this._minutes = +minutes;
}
Expand All @@ -58,7 +70,7 @@ export class KnxSyncStateSelectorRow extends LitElement {
translation_key: this.key,
multiple: false,
custom_value: false,
mode: "dropdown",
mode: "dropdown" as const,
options: this._options,
},
}}
Expand Down Expand Up @@ -88,7 +100,7 @@ export class KnxSyncStateSelectorRow extends LitElement {

private _handleChange(ev) {
ev.stopPropagation();
let strategy: boolean | string;
let strategy: string;
let minutes: number;
if (ev.target.key === "strategy") {
strategy = ev.detail.value;
Expand All @@ -97,7 +109,19 @@ export class KnxSyncStateSelectorRow extends LitElement {
strategy = this._strategy;
minutes = ev.detail.value;
}
const value = this._hasMinutes(strategy) ? `${strategy} ${minutes}` : strategy;

// Convert string "true"/"false" back to boolean for backend API compatibility
let value: string | boolean;
if (this._hasMinutes(strategy)) {
value = `${strategy} ${minutes}`;
} else if (strategy === "true") {
value = true;
} else if (strategy === "false") {
value = false;
} else {
value = strategy;
}

fireEvent(this, "value-changed", { value });
}

Expand Down