From 850609b1d00bfc37a99b6a779dff43f99d78a1b1 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Tue, 21 Feb 2023 00:36:18 -0800 Subject: [PATCH] Resync thermostat card setpoint values if service call fails (#15497) --- .../lovelace/cards/hui-thermostat-card.ts | 124 ++++++++++++++---- 1 file changed, 100 insertions(+), 24 deletions(-) diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts index 58983fecf5..f75d2e95d1 100644 --- a/src/panels/lovelace/cards/hui-thermostat-card.ts +++ b/src/panels/lovelace/cards/hui-thermostat-card.ts @@ -90,6 +90,8 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard { @query("ha-card") private _card?: HaCard; + @state() private resyncSetpoint = false; + public getCardSize(): number { return 7; } @@ -120,11 +122,23 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard { const name = this._config!.name || computeStateName(this.hass!.states[this._config!.entity]); - const targetTemp = - stateObj.attributes.temperature !== null && - Number.isFinite(Number(stateObj.attributes.temperature)) - ? stateObj.attributes.temperature - : stateObj.attributes.min_temp; + const targetTemp = this.resyncSetpoint + ? // If the user set position in the slider is out of sync with the entity + // value, then rerendering the slider with same $value a second time + // does not move the slider. Need to set it to a different dummy value + // for one update cycle to force it to rerender to the desired value. + stateObj.attributes.min_temp - 1 + : stateObj.attributes.temperature !== null && + Number.isFinite(Number(stateObj.attributes.temperature)) + ? stateObj.attributes.temperature + : stateObj.attributes.min_temp; + + const targetLow = this.resyncSetpoint + ? stateObj.attributes.min_temp - 1 + : stateObj.attributes.target_temp_low; + const targetHigh = this.resyncSetpoint + ? stateObj.attributes.min_temp - 1 + : stateObj.attributes.target_temp_high; const slider = stateObj.state === UNAVAILABLE @@ -132,8 +146,8 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard { : html` setTimeout(resolve, 2000)); + + const newState = this.hass!.states[this._config!.entity] as ClimateEntity; + delete data.entity_id; + + if ( + Object.entries(data).every( + ([key, value]) => newState.attributes[key] === value + ) + ) { + return; + } + + this.resyncSetpoint = true; + await this.updateComplete; + this.resyncSetpoint = false; + } + static get styles(): CSSResultGroup { return css` :host {