Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrap around support in counter component #23207

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/data/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Counter {
restore?: boolean;
minimum?: number;
maximum?: number;
wrap_around?: boolean;
step?: number;
}

Expand All @@ -18,6 +19,7 @@ export interface CounterMutableParams {
restore: boolean;
minimum: number;
maximum: number;
wrap_around: boolean;
step: number;
}

Expand Down
38 changes: 30 additions & 8 deletions src/panels/config/helpers/forms/ha-counter-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "../../../../components/ha-icon-picker";
import "../../../../components/ha-switch";
import type { HaSwitch } from "../../../../components/ha-switch";
import "../../../../components/ha-textfield";
import "../../../../components/ha-selector/ha-selector-boolean";
import type { Counter } from "../../../../data/counter";
import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
Expand All @@ -26,6 +27,8 @@ class HaCounterForm extends LitElement {

@state() private _minimum?: number;

@state() private _wrap_around?: boolean;

@state() private _restore?: boolean;

@state() private _initial?: number;
Expand All @@ -41,6 +44,7 @@ class HaCounterForm extends LitElement {
this._minimum = item.minimum ?? undefined;
this._restore = item.restore ?? true;
this._step = item.step ?? 1;
this._wrap_around = item.wrap_around ?? false;
this._initial = item.initial ?? 0;
} else {
this._name = "";
Expand All @@ -49,6 +53,7 @@ class HaCounterForm extends LitElement {
this._minimum = undefined;
this._restore = true;
this._step = 1;
this._wrap_around = false;
this._initial = 0;
}
}
Expand Down Expand Up @@ -82,6 +87,17 @@ class HaCounterForm extends LitElement {
)}
dialogInitialFocus
></ha-textfield>
<ha-selector-boolean
.value=${this._wrap_around}
.configValue=${"wrap_around"}
@value-changed=${this._valueChanged}
.label=${this.hass!.localize(
"ui.dialogs.helper_settings.counter.wrap_around"
)}
.helper=${this.hass!.localize(
"ui.dialogs.helper_settings.counter.wrap_around_helper"
)}
></ha-selector-boolean>
<ha-icon-picker
.hass=${this.hass}
.value=${this._icon}
Expand All @@ -96,6 +112,7 @@ class HaCounterForm extends LitElement {
.configValue=${"minimum"}
type="number"
@input=${this._valueChanged}
.required=${this._wrap_around}
.label=${this.hass!.localize(
"ui.dialogs.helper_settings.counter.minimum"
)}
Expand All @@ -105,6 +122,7 @@ class HaCounterForm extends LitElement {
.configValue=${"maximum"}
type="number"
@input=${this._valueChanged}
.required=${this._wrap_around}
.label=${this.hass!.localize(
"ui.dialogs.helper_settings.counter.maximum"
)}
Expand Down Expand Up @@ -155,14 +173,18 @@ class HaCounterForm extends LitElement {
ev.stopPropagation();
const target = ev.target as any;
const configValue = target.configValue;
const value =
target.type === "number"
? target.value !== ""
? Number(target.value)
: undefined
: target.localName === "ha-switch"
? (ev.target as HaSwitch).checked
: ev.detail?.value || target.value;
let value = ev.detail?.value ?? target.value;

if (target.type === "number") {
if (value !== "") {
value = Number(value);
} else {
value = undefined;
}
} else if (target.localName === "ha-switch") {
value = (ev.target as HaSwitch).checked;
}

if (this[`_${configValue}`] === value) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,8 @@
"counter": {
"minimum": "Minimum value",
"maximum": "Maximum value",
"wrap_around": "Wrap around",
"wrap_around_helper": "Should wrap around once it reaches the maximum or minimum value",
"initial": "Initial value",
"restore": "Restore the last known value when Home Assistant starts",
"step": "Step size"
Expand Down
Loading