Skip to content

Commit

Permalink
Fix temperature cards not syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolger committed Oct 29, 2023
1 parent 113eb5b commit 3ba8788
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class HaMoreInfoClimateTemperature extends LitElement {
return this.stateObj.attributes.max_temp;
}

private _valueChanged(ev: CustomEvent) {
private async _valueChanged(ev: CustomEvent) {
const value = (ev.detail as any).value;
if (isNaN(value)) return;
const target = ev.type.replace("-changed", "");
Expand All @@ -88,7 +88,7 @@ export class HaMoreInfoClimateTemperature extends LitElement {
[target]: value,
};
this._selectTargetTemperature = target as Target;
this._callService(target);
await this._callService(target);
}

private _valueChanging(ev: CustomEvent) {
Expand All @@ -107,19 +107,40 @@ export class HaMoreInfoClimateTemperature extends LitElement {
1000
);

private _callService(type: string) {
if (type === "high" || type === "low") {
this.hass.callService("climate", "set_temperature", {
entity_id: this.stateObj!.entity_id,
target_temp_low: this._targetTemperature.low,
target_temp_high: this._targetTemperature.high,
});
return;
private async _callService(type: string) {
try {
await this.hass.callService(
"climate",
"set_temperature",
this.buildServiceData(type)
);
} catch (err) {
this.resetTemperatureValues(type);
}
this.hass.callService("climate", "set_temperature", {
}

private async resetTemperatureValues(type: string) {
if (type === "high") {
this._targetTemperature.high = this.stateObj.attributes.target_temp_high;
} else if (type === "low") {
this._targetTemperature.low = this.stateObj.attributes.target_temp_low;
} else {
this._targetTemperature.value = this.stateObj.attributes.temperature;
}
this.requestUpdate();
}

private buildServiceData(type: string) {
const data: any = {
entity_id: this.stateObj!.entity_id,
temperature: this._targetTemperature.value,
});
};
if (type === "low" || type === "high") {
data.target_temp_low = this._targetTemperature.low;
data.target_temp_high = this._targetTemperature.high;
} else {
data.temperature = this._targetTemperature.value;
}
return data;
}

private _handleButton(ev) {
Expand Down
10 changes: 7 additions & 3 deletions src/panels/lovelace/cards/hui-thermostat-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {

@state() private resyncSetpoint = false;

@state() private _error?: string;

Check failure on line 94 in src/panels/lovelace/cards/hui-thermostat-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

'_error' is declared but its value is never read.

public getCardSize(): number {
return 7;
}
Expand Down Expand Up @@ -487,9 +489,11 @@ export class HuiThermostatCard extends LitElement implements LovelaceCard {
}

data.entity_id = this._config!.entity;

await this.hass!.callService("climate", service, data);

try {
await this.hass!.callService("climate", service, data);
} catch (err: any) {
this._error = err.message;
}
// After updating temperature, wait 2s and check if the values
// from call service are reflected in the entity. If not, resync
// the slider to the entity values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,41 @@ class HuiTargetTemperatureTileFeature
1000
);

private _callService(type: string) {
private async _callService(type: string) {
const domain = computeStateDomain(this.stateObj!);
if (type === "high" || type === "low") {
this.hass!.callService(domain, "set_temperature", {
entity_id: this.stateObj!.entity_id,
target_temp_low: this._targetTemperature.low,
target_temp_high: this._targetTemperature.high,
});
return;
try {
await this.hass!.callService(
domain,
"set_temperature",
this.buildServiceData(type)
);
} catch (err) {
this.resetTemperatureValues(type);
}
this.hass!.callService(domain, "set_temperature", {
}

private async resetTemperatureValues(type: string) {
if (type === "high") {
this._targetTemperature.high = this.stateObj!.attributes.target_temp_high;

Check failure on line 131 in src/panels/lovelace/tile-features/hui-target-temperature-tile-feature.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Property 'target_temp_high' does not exist on type '(HassEntityAttributeBase & { hvac_mode: "auto" | "heat" | "off" | "heat_cool" | "cool" | "dry" | "fan_only"; hvac_modes: ("auto" | "heat" | "off" | "heat_cool" | "cool" | "dry" | "fan_only")[]; ... 20 more ...; aux_heat?: "on" | ... 1 more ... | undefined; }) | (HassEntityAttributeBase & { ...; })'.
} else if (type === "low") {
this._targetTemperature.low = this.stateObj!.attributes.target_temp_low;

Check failure on line 133 in src/panels/lovelace/tile-features/hui-target-temperature-tile-feature.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Property 'target_temp_low' does not exist on type '(HassEntityAttributeBase & { hvac_mode: "auto" | "heat" | "off" | "heat_cool" | "cool" | "dry" | "fan_only"; hvac_modes: ("auto" | "heat" | "off" | "heat_cool" | "cool" | "dry" | "fan_only")[]; ... 20 more ...; aux_heat?: "on" | ... 1 more ... | undefined; }) | (HassEntityAttributeBase & { ...; })'.
} else {
this._targetTemperature.value = this.stateObj!.attributes.temperature;
}
this.requestUpdate();
}

private buildServiceData(type: string) {
const data: any = {
entity_id: this.stateObj!.entity_id,
temperature: this._targetTemperature.value,
});
};
if (type === "low" || type === "high") {
data.target_temp_low = this._targetTemperature.low;
data.target_temp_high = this._targetTemperature.high;
} else {
data.temperature = this._targetTemperature.value;
}
return data;
}

private _supportsTarget() {
Expand Down

0 comments on commit 3ba8788

Please sign in to comment.