From 1db93a4f7bbbf0754c5982bc8abe2e13d5a577c5 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 28 Feb 2019 17:41:20 -0800 Subject: [PATCH] Fix ha-entity-toggle restoring old state (#2868) --- src/components/entity/ha-entity-toggle.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/components/entity/ha-entity-toggle.ts b/src/components/entity/ha-entity-toggle.ts index 42a622a9e2..18caf31ea9 100644 --- a/src/components/entity/ha-entity-toggle.ts +++ b/src/components/entity/ha-entity-toggle.ts @@ -110,11 +110,21 @@ class HaEntityToggle extends LitElement { entity_id: this.stateObj.entity_id, }); - setTimeout(() => { + setTimeout(async () => { // If after 2 seconds we have not received a state update // reset the switch to it's original state. - if (this.stateObj === currentState) { - this.requestUpdate(); + if (this.stateObj !== currentState) { + return; + } + // Force a re-render. It's not good enough to just call this.requestUpdate() + // because the value has changed outside of Lit's render function, and so Lit + // won't update the property again, because it's the same as last update. + // So we just temporarily unset the stateObj and set it again. + this.stateObj = undefined; + await this.updateComplete; + // Make sure that a stateObj was not set in between. + if (this.stateObj === undefined) { + this.stateObj = currentState; } }, 2000); }