Skip to content

Commit d9654bf

Browse files
IHateToSelectAUsernameCopilot
authored andcommitted
Fix sync state dropdown rendering and add option labels (#276)
1 parent d842ae9 commit d9654bf

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/components/knx-sync-state-selector-row.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,39 @@ export class KnxSyncStateSelectorRow extends LitElement {
2121
key: string,
2222
) => key;
2323

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

2628
private _minutes = 60;
2729

28-
private get _options(): (boolean | "init" | "expire" | "every")[] {
30+
private get _options(): readonly string[] {
2931
return this.allowFalse
30-
? [true, "init", "expire", "every", false]
31-
: [true, "init", "expire", "every"];
32+
? ["true", "init", "expire", "every", "false"]
33+
: ["true", "init", "expire", "every"];
3234
}
3335

34-
protected _hasMinutes(strategy: boolean | string): boolean {
36+
protected _hasMinutes(strategy: string): boolean {
3537
return strategy === "expire" || strategy === "every";
3638
}
3739

3840
protected willUpdate() {
41+
// Convert incoming boolean values from backend to string representation
3942
if (typeof this.value === "boolean") {
40-
this._strategy = this.value;
43+
this._strategy = this.value ? "true" : "false";
4144
return;
4245
}
4346
const [strategy, minutes] = this.value.split(" ");
44-
this._strategy = strategy;
47+
48+
// Validate strategy value before assignment
49+
const validStrategies = ["true", "false", "init", "expire", "every"] as const;
50+
if (validStrategies.includes(strategy as any)) {
51+
this._strategy = strategy as typeof this._strategy;
52+
} else {
53+
// Fallback to default for invalid values
54+
this._strategy = "true";
55+
}
56+
4557
if (+minutes) {
4658
this._minutes = +minutes;
4759
}
@@ -58,7 +70,7 @@ export class KnxSyncStateSelectorRow extends LitElement {
5870
translation_key: this.key,
5971
multiple: false,
6072
custom_value: false,
61-
mode: "dropdown",
73+
mode: "dropdown" as const,
6274
options: this._options,
6375
},
6476
}}
@@ -88,7 +100,7 @@ export class KnxSyncStateSelectorRow extends LitElement {
88100

89101
private _handleChange(ev: CustomEvent): void {
90102
ev.stopPropagation();
91-
let strategy: boolean | string;
103+
let strategy: string;
92104
let minutes: number;
93105
if (ev.target.key === "strategy") {
94106
strategy = ev.detail.value;
@@ -97,7 +109,19 @@ export class KnxSyncStateSelectorRow extends LitElement {
97109
strategy = this._strategy;
98110
minutes = ev.detail.value;
99111
}
100-
const value = this._hasMinutes(strategy) ? `${strategy} ${minutes}` : strategy;
112+
113+
// Convert string "true"/"false" back to boolean for backend API compatibility
114+
let value: string | boolean;
115+
if (this._hasMinutes(strategy)) {
116+
value = `${strategy} ${minutes}`;
117+
} else if (strategy === "true") {
118+
value = true;
119+
} else if (strategy === "false") {
120+
value = false;
121+
} else {
122+
value = strategy;
123+
}
124+
101125
fireEvent(this, "value-changed", { value });
102126
}
103127

0 commit comments

Comments
 (0)