Skip to content

Commit

Permalink
Fix an infinite loop in automation numeric_state (#22429)
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts authored Oct 29, 2024
1 parent 5f6396b commit 9fff3ad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ export default class HaNumericStateCondition extends LitElement {
return true;
}

private _data = memoizeOne(
(
inputAboveIsEntity: boolean,
inputBelowIsEntity: boolean,
condition: NumericStateCondition
) => ({
mode_above: inputAboveIsEntity ? "input" : "value",
mode_below: inputBelowIsEntity ? "input" : "value",
...condition,
})
);

private _schema = memoizeOne(
(
localize: LocalizeFunc,
Expand Down Expand Up @@ -233,31 +245,33 @@ export default class HaNumericStateCondition extends LitElement {
] as const
);

public render() {
const inputAboveIsEntity =
public willUpdate() {
this._inputAboveIsEntity =
this._inputAboveIsEntity ??
(typeof this.condition.above === "string" &&
((this.condition.above as string).startsWith("input_number.") ||
(this.condition.above as string).startsWith("number.") ||
(this.condition.above as string).startsWith("sensor.")));
const inputBelowIsEntity =
this._inputBelowIsEntity =
this._inputBelowIsEntity ??
(typeof this.condition.below === "string" &&
((this.condition.below as string).startsWith("input_number.") ||
(this.condition.below as string).startsWith("number.") ||
(this.condition.below as string).startsWith("sensor.")));
}

public render() {
const schema = this._schema(
this.hass.localize,
inputAboveIsEntity,
inputBelowIsEntity
this._inputAboveIsEntity,
this._inputBelowIsEntity
);

const data = {
mode_above: inputAboveIsEntity ? "input" : "value",
mode_below: inputBelowIsEntity ? "input" : "value",
...this.condition,
};
const data = this._data(
this._inputAboveIsEntity!,
this._inputBelowIsEntity!,
this.condition
);

return html`
<ha-form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ export class HaNumericStateTrigger extends LitElement {
);

public willUpdate(changedProperties: PropertyValues) {
this._inputAboveIsEntity =
this._inputAboveIsEntity ??
(typeof this.trigger.above === "string" &&
((this.trigger.above as string).startsWith("input_number.") ||
(this.trigger.above as string).startsWith("number.") ||
(this.trigger.above as string).startsWith("sensor.")));
this._inputBelowIsEntity =
this._inputBelowIsEntity ??
(typeof this.trigger.below === "string" &&
((this.trigger.below as string).startsWith("input_number.") ||
(this.trigger.below as string).startsWith("number.") ||
(this.trigger.below as string).startsWith("sensor.")));

if (!changedProperties.has("trigger")) {
return;
}
Expand All @@ -244,36 +257,33 @@ export class HaNumericStateTrigger extends LitElement {
};
}

public render() {
const trgFor = createDurationData(this.trigger.for);

const inputAboveIsEntity =
this._inputAboveIsEntity ??
(typeof this.trigger.above === "string" &&
((this.trigger.above as string).startsWith("input_number.") ||
(this.trigger.above as string).startsWith("number.") ||
(this.trigger.above as string).startsWith("sensor.")));
const inputBelowIsEntity =
this._inputBelowIsEntity ??
(typeof this.trigger.below === "string" &&
((this.trigger.below as string).startsWith("input_number.") ||
(this.trigger.below as string).startsWith("number.") ||
(this.trigger.below as string).startsWith("sensor.")));
private _data = memoizeOne(
(
inputAboveIsEntity: boolean,
inputBelowIsEntity: boolean,
trigger: NumericStateTrigger
) => ({
mode_above: inputAboveIsEntity ? "input" : "value",
mode_below: inputBelowIsEntity ? "input" : "value",
...trigger,
entity_id: ensureArray(trigger.entity_id),
for: createDurationData(trigger.for),
})
);

public render() {
const schema = this._schema(
this.hass.localize,
this.trigger.entity_id,
inputAboveIsEntity,
inputBelowIsEntity
this._inputAboveIsEntity,
this._inputBelowIsEntity
);

const data = {
mode_above: inputAboveIsEntity ? "input" : "value",
mode_below: inputBelowIsEntity ? "input" : "value",
...this.trigger,
entity_id: ensureArray(this.trigger.entity_id),
for: trgFor,
};
const data = this._data(
this._inputAboveIsEntity!,
this._inputBelowIsEntity!,
this.trigger
);

return html`
<ha-form
Expand All @@ -289,7 +299,7 @@ export class HaNumericStateTrigger extends LitElement {

private _valueChanged(ev: CustomEvent): void {
ev.stopPropagation();
const newTrigger = ev.detail.value;
const newTrigger = { ...ev.detail.value };

this._inputAboveIsEntity = newTrigger.mode_above === "input";
this._inputBelowIsEntity = newTrigger.mode_below === "input";
Expand Down

0 comments on commit 9fff3ad

Please sign in to comment.