Skip to content

Commit

Permalink
Neaten scaling calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
JELoohuis committed Aug 16, 2024
1 parent e372459 commit 2bd2698
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
24 changes: 8 additions & 16 deletions drivers/heater/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,22 @@ module.exports = class TuyaOAuth2DeviceHeater extends TuyaOAuth2Device {
}

if (typeof status['temp_current'] === 'number') {
const scaleExp = Number.parseInt(this.getSetting('temp_current_scaling') ?? '0', 10);
const scale = 10 ** scaleExp;
const temp_current = status['temp_current'] / scale;
this.setCapabilityValue('measure_temperature', temp_current).catch(this.error);
const scaling = 10.0 ** Number.parseInt(this.getSetting('temp_current_scaling') ?? '0', 10);
this.setCapabilityValue('measure_temperature', status['temp_current'] / scaling).catch(this.error);
}

if (typeof status['temp_set'] === 'number') {
const scaleExp = Number.parseInt(this.getSetting('temp_set_scaling') ?? '0', 10);
const scale = 10 ** scaleExp;
const temp_set = status['temp_set'] / scale;
this.setCapabilityValue('target_temperature', temp_set).catch(this.error);
const scaling = 10.0 ** Number.parseInt(this.getSetting('temp_set_scaling') ?? '0', 10);
this.setCapabilityValue('target_temperature', status['temp_set'] / scaling).catch(this.error);
}

if (typeof status['lock'] === 'boolean') {
this.setCapabilityValue('child_lock', status['lock']).catch(this.error);
}

if (typeof status['work_power'] === 'number') {
const scaleExp = Number.parseInt(this.getSetting('work_power_scaling') ?? '0', 10);
const scale = 10 ** scaleExp;
const cur_power = status['work_power'] / scale;
this.setCapabilityValue('measure_power', cur_power).catch(this.error);
const scaling = 10.0 ** Number.parseInt(this.getSetting('work_power_scaling') ?? '0', 10);
this.setCapabilityValue('measure_power', status['work_power'] / scaling).catch(this.error);
}

if (typeof status['mode_eco'] === 'boolean') {
Expand All @@ -67,12 +61,10 @@ module.exports = class TuyaOAuth2DeviceHeater extends TuyaOAuth2Device {
}

async targetTemperatureCapabilityListener(value: number): Promise<void> {
const scaleExp = Number.parseInt(this.getSetting('temp_set_scaling') ?? '0', 10);
const scale = 10 ** scaleExp;
const temp_set = value * scale;
const scaling = 10.0 ** Number.parseInt(this.getSetting('temp_set_scaling') ?? '0', 10);
await this.sendCommand({
code: 'temp_set',
value: temp_set,
value: value * scaling,
});
}

Expand Down
14 changes: 6 additions & 8 deletions drivers/socket/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,19 @@ export default class TuyaOAuth2DeviceSocket extends TuyaOAuth2Device {
this.safeSetCapabilityValue('onoff', anySwitchOn).catch(this.error);

if (typeof status['cur_power'] === 'number') {
const powerScaling = 10.0 ** parseInt(this.getSetting('power_scaling') ?? '0');
const cur_power = status['cur_power'] / powerScaling;
this.setCapabilityValue('measure_power', cur_power).catch(this.error);
const scaling = 10.0 ** parseInt(this.getSetting('power_scaling') ?? '0');
this.setCapabilityValue('measure_power', status['cur_power'] / scaling).catch(this.error);
}

if (typeof status['cur_voltage'] === 'number') {
const scaling = 10.0 ** parseInt(this.getSetting('cur_voltage_scaling') ?? '0');
const cur_voltage = status['cur_voltage'] / scaling;
this.setCapabilityValue('measure_voltage', cur_voltage).catch(this.error);
this.setCapabilityValue('measure_voltage', status['cur_voltage'] / scaling).catch(this.error);
}

if (typeof status['cur_current'] === 'number') {
const scaling = 10.0 ** parseInt(this.getSetting('cur_current_scaling') ?? '0');
const cur_current = status['cur_current'] / scaling / 1000.0; // Additionally convert mA
this.setCapabilityValue('measure_current', cur_current).catch(this.error);
// Additionally convert mA
const scaling = 1000.0 * 10.0 ** parseInt(this.getSetting('cur_current_scaling') ?? '0');
this.setCapabilityValue('measure_current', status['cur_current'] / scaling).catch(this.error);
}

if (status['child_lock'] !== undefined) {
Expand Down

0 comments on commit 2bd2698

Please sign in to comment.