From 9fff3adbfb0ce9775e34b2e25b1a08b1975acf62 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Tue, 29 Oct 2024 07:21:41 -0700 Subject: [PATCH] Fix an infinite loop in automation numeric_state (#22429) --- .../ha-automation-condition-numeric_state.ts | 34 +++++++---- .../ha-automation-trigger-numeric_state.ts | 60 +++++++++++-------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts b/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts index c816bc6f8d..0f6e3977f0 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-numeric_state.ts @@ -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, @@ -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` ({ + mode_above: inputAboveIsEntity ? "input" : "value", + mode_below: inputBelowIsEntity ? "input" : "value", + ...trigger, + entity_id: ensureArray(trigger.entity_id), + for: createDurationData(trigger.for), + }) + ); + 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."))); - 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`