Skip to content

Commit

Permalink
Improve temperature readings
Browse files Browse the repository at this point in the history
  • Loading branch information
bimusiek committed Sep 2, 2024
1 parent ab80de2 commit 2742de5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
37 changes: 17 additions & 20 deletions src/accessories/thermostatAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {
private ecoModeService: Service | undefined;
private comfortModeService: Service | undefined;
private readonly isCoolModeEnabled: boolean;
private readonly zoneSensor: 'Internal' | 'Water temperature';
private readonly hasWaterTank: boolean;

constructor(
Expand All @@ -22,7 +21,6 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {
super(platform, accessory, panasonicApi);
this.isCoolModeEnabled = this.accessory.context.device.isCoolModeEnabled;
this.hasWaterTank = this.accessory.context.device.hasWaterTank;
this.zoneSensor = this.accessory.context.device.zoneSensor;
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Panasonic')
Expand Down Expand Up @@ -128,15 +126,16 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {
if(!readings) {
return;
}
const {targetTempMin, tempType} = readings;
const temperatureDelta = this.getTemperatureDelta({targetTempMin});
const parsedTemp = parseInt(temp as string);
const adjustedTemp = this.zoneSensor === 'Internal' ? parsedTemp : parsedTemp - readings.temperatureNow;
const adjustedTemp = parsedTemp + temperatureDelta;

try {
this.panasonicApi.setZoneTemp(this.accessory.context.device.uniqueId,
adjustedTemp, readings.tempType);
this.panasonicApi.setZoneTemp(this.accessory.context.device.uniqueId, adjustedTemp, tempType);
} catch (e) {
this.platform.log.error(
`Could not set zone temp[${this.accessory.context.device.uniqueId}][${adjustedTemp}][${readings.tempType}]: ${e}`,
`Could not set zone temp[${this.accessory.context.device.uniqueId}][${adjustedTemp}][${tempType}]: ${e}`,
);
}
}
Expand Down Expand Up @@ -233,7 +232,6 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {

const currentTemp = this.updateTargetTemperaturePropsAndReturnCurrentTemp(readings);
this.service.getCharacteristic(this.platform.Characteristic.TargetTemperature).updateValue(currentTemp);
// As heat pumps take -5 up to +5 target temp and HomeKit does not support it, we have to adjust by tempNow

if (this.tankService) {
this.tankService.getCharacteristic(this.platform.Characteristic.TargetTemperature).setProps({
Expand All @@ -251,25 +249,24 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {
this.comfortModeService?.getCharacteristic(this.platform.Characteristic.On).updateValue(comfortModeIsActive);
}

private getTemperatureDelta({ targetTempMin }: Pick<DeviceDetails, 'targetTempMin'>) {
// As heat pumps take -5 up to +5 target temp and HomeKit does not support it, we have to adjust by the min temp
return targetTempMin < 0 ? -targetTempMin : 0;
}

private updateTargetTemperaturePropsAndReturnCurrentTemp({ targetTempMin, targetTempMax, targetTempSet, temperatureNow }: DeviceDetails) {
if(this.zoneSensor === 'Internal') {
this.service.getCharacteristic(this.platform.Characteristic.TargetTemperature).setProps({
minValue: targetTempMin,
maxValue: targetTempMax,
minStep: 1,
});
return targetTempSet;
}
if(targetTempMin === undefined || temperatureNow === undefined || targetTempMax === undefined || targetTempSet === undefined) {
if(targetTempMin === undefined || targetTempMax === undefined || targetTempSet === undefined) {
this.platform.log.error(
`updateTargetTemperaturePropsAndReturnCurrentTemp got wrong readings:
${JSON.stringify({ targetTempMin, targetTempMax, targetTempSet, temperatureNow })}
`);
return 100; // fake big value to indicate the issue in homekit
}
const tempMin = Math.floor(targetTempMin) + Math.floor(temperatureNow);
const tempMax = Math.ceil(targetTempMax) + Math.ceil(temperatureNow);
const tempCurrent = Math.round(targetTempSet + temperatureNow);

const temperatureDelta = this.getTemperatureDelta({targetTempMin});
const tempMin = targetTempMin + temperatureDelta;
const tempMax = targetTempMax + temperatureDelta;
const tempCurrent = targetTempSet + temperatureDelta;
this.platform.log.debug(`Updating TargetTemperature of Floor Heater: ${JSON.stringify({
minValue: tempMin,
maxValue: tempMax,
Expand All @@ -280,7 +277,7 @@ export class ThermostatAccessory extends Accessory<DeviceContext> {
maxValue: tempMax,
minStep: 1,
});
return Math.max(tempMin, Math.min(tempMax, tempCurrent));
return tempCurrent;
}


Expand Down
1 change: 0 additions & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class PanasonicHeatPumpHomebridgePlatform implements DynamicPlatformPlugi
displayName: selectedDeviceName,
isCoolModeEnabled: deviceConf.configration[0].zoneInfo[0].coolMode === 'enable',
hasWaterTank: deviceConf.configration[0].tankInfo[0].tank === 'Yes',
zoneSensor: deviceConf.configration[0].zoneInfo[0].zoneSensor,
},
];
} catch (e) {
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface Device {
displayName: string;
isCoolModeEnabled: boolean;
hasWaterTank: boolean;
zoneSensor: 'Internal' | 'Water temperature';
}

export interface DeviceContext {
Expand Down

0 comments on commit 2742de5

Please sign in to comment.