Skip to content

Commit

Permalink
Update lib/TuyaOAuth2Util.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Sep 5, 2024
1 parent f90c729 commit a70c07e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/TuyaOAuth2Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,26 @@ export function hasJsonStructure(str: unknown): boolean {
}
}

/**
* @returns {boolean} - Whether the scaling was successfully changed
*/
export async function handleScaleSetting<T extends string, S extends Record<T, string>>(
device: TuyaOAuth2Device,
event: SettingsEvent<S>,
settingKey: T,
homeyCapability: string | undefined,
): Promise<boolean> {
if (!event.changedKeys.includes(settingKey)) return true;
if (!homeyCapability || !device.hasCapability(homeyCapability)) return false;
): Promise<void> {
if (!event.changedKeys.includes(settingKey)) {
return;
}

if (!homeyCapability || !device.hasCapability(homeyCapability)) {
return;
}

const oldScaling = 10.0 ** Number.parseInt(event.oldSettings[settingKey] ?? '0', 10);
const newScaling = 10.0 ** Number.parseInt(event.newSettings[settingKey] ?? '0', 10);
const oldValue = device.getCapabilityValue(homeyCapability);
const newValue = (oldValue * oldScaling) / newScaling;

await device.setCapabilityValue(homeyCapability, newValue);
return true;
}

/**
Expand Down

0 comments on commit a70c07e

Please sign in to comment.