Skip to content

Commit

Permalink
Optional boolean selector
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Jan 28, 2025
1 parent df8ad51 commit b60a324
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
40 changes: 26 additions & 14 deletions src/components/knx-selector-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,38 @@ export class KnxSelectorRow extends LitElement {

private _haSelectorValue: any = null;

private _inlineSelector = false;

private _optionalBooleanSelector = false;

public connectedCallback() {
super.connectedCallback();
this._disabled = !!this.selector.optional && this.value === undefined;
// apply default value if available or no value is set yet
this._haSelectorValue = this.value ?? this.selector.default ?? null;

const booleanSelector = "boolean" in this.selector.selector;
const possibleInlineSelector = booleanSelector || "number" in this.selector.selector;
this._inlineSelector = !this.selector.optional && possibleInlineSelector;
// optional boolean should not show as 2 switches (one for optional and one for value)
this._optionalBooleanSelector = !!this.selector.optional && booleanSelector;
if (this._optionalBooleanSelector) {
// either true or the key will be unset (via this._disabled)
this._haSelectorValue = true;
}
}

protected render(): TemplateResult {
const possibleInlineSelector =
"boolean" in this.selector.selector || "number" in this.selector.selector;
const inlineSelector = !this.selector.optional && possibleInlineSelector;

const haSelector = html`<ha-selector
class=${classMap({ "newline-selector": !inlineSelector })}
.hass=${this.hass}
.selector=${this.selector.selector}
.disabled=${this._disabled}
.value=${this._haSelectorValue}
@value-changed=${this._valueChange}
></ha-selector>`;
const haSelector = this._optionalBooleanSelector
? nothing
: html`<ha-selector
class=${classMap({ "newline-selector": !this._inlineSelector })}
.hass=${this.hass}
.selector=${this.selector.selector}
.disabled=${this._disabled}
.value=${this._haSelectorValue}
@value-changed=${this._valueChange}
></ha-selector>`;

return html`
<div class="body">
Expand All @@ -58,11 +70,11 @@ export class KnxSelectorRow extends LitElement {
.value=${!this._disabled}
@value-changed=${this._toggleDisabled}
></ha-selector>`
: inlineSelector
: this._inlineSelector
? haSelector
: nothing}
</div>
${inlineSelector ? nothing : haSelector}
${this._inlineSelector ? nothing : haSelector}
`;
}

Expand Down
21 changes: 8 additions & 13 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type KnxHaSelector = {
name: string;
type: "selector";
default?: any;
optional?: boolean;
optional?: boolean; // for optional boolean selectors, there shall be no default value (can't get applied)
selector: Selector;
label: string;
helper?: string;
Expand Down Expand Up @@ -66,7 +66,7 @@ export const binarySensorSchema: SettingsGroup[] = [
description: "DPT 1 group addresses representing binary states.",
selectors: [
{
name: "ga_state",
name: "ga_sensor",
type: "group_address",
options: {
state: { required: true },
Expand All @@ -80,7 +80,8 @@ export const binarySensorSchema: SettingsGroup[] = [
selector: { boolean: null },
label: "Invert",
helper: "Invert payload before processing.",
// default: false, // does this work?
optional: true,
// default: false, // does this work? - no, it doesn't - isn't forwarded to data when loading
},
],
},
Expand All @@ -96,6 +97,7 @@ export const binarySensorSchema: SettingsGroup[] = [
selector: { boolean: null },
label: "Force update",
helper: "Write each update to the state machine, even if the data is the same.",
optional: true,
},
{
name: "context_timeout",
Expand All @@ -104,23 +106,16 @@ export const binarySensorSchema: SettingsGroup[] = [
label: "Context timeout",
helper:
"The time in seconds between multiple identical telegram payloads would count towards an internal counter. This can be used to automate on mulit-clicks of a button. `0` to disable this feature.",
default: 0,
default: 0.8, // not forwarded to data when loading - fine when optional: true
optional: true,
},
// {
// name: "reset_after",
// type: "selector",
// selector: { boolean: null },
// label: "Reset after",
// helper: "Automatically reset state back to “off”.",
// },
{
// TODO: this has to be `None` to diable - being `0` will reset immediately
name: "reset_after",
type: "selector",
selector: { number: { min: 0, max: 10, step: 0.1, unit_of_measurement: "s" } },
label: "Reset after",
helper: "Reset back to “off” state after specified seconds.",
default: 0.8,
default: 0.8, // not forwarded to data when loading - fine when optional: true
optional: true,
},
],
Expand Down

0 comments on commit b60a324

Please sign in to comment.