Fix ha-entity-toggle restoring old state (#2868)

This commit is contained in:
Paulus Schoutsen 2019-02-28 17:41:20 -08:00 committed by GitHub
parent 8f4d24b6da
commit 1db93a4f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}