-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from athombv/sensor-updates
Sensor device update - battery percentage & tamper alarm
- Loading branch information
Showing
7 changed files
with
142 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type TuyaOAuth2Device from '../TuyaOAuth2Device'; | ||
|
||
const storeKey = '_migrations'; | ||
|
||
/** Execute a migration when not already done, based on the supplied migration id. */ | ||
export async function executeMigration( | ||
device: TuyaOAuth2Device, | ||
migrationId: string, | ||
migration: () => Promise<void>, | ||
): Promise<void> { | ||
let migrations: string[] | undefined = device.getStoreValue(storeKey); | ||
if (!Array.isArray(migrations)) { | ||
migrations = []; | ||
} | ||
|
||
if (migrations.includes(migrationId)) { | ||
return; | ||
} | ||
|
||
await migration(); | ||
|
||
migrations.push(migrationId); | ||
await device.setStoreValue(storeKey, migrations).catch(device.error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import TuyaOAuth2DeviceSensor from '../TuyaOAuth2DeviceSensor'; | ||
import { executeMigration } from './MigrationStore'; | ||
|
||
export async function performMigrations(device: TuyaOAuth2DeviceSensor): Promise<void> { | ||
await addBatteryPercentageMigration(device).catch(device.error); | ||
await addTemperAlarmMigration(device).catch(device.error); | ||
} | ||
|
||
async function addBatteryPercentageMigration(device: TuyaOAuth2DeviceSensor): Promise<void> { | ||
await executeMigration(device, 'sensor_battery_percentage', async () => { | ||
if (device.hasCapability('measure_battery') || device.hasCapability('alarm_battery')) { | ||
// Don't touch existing devices that already have a battery related capability | ||
device.log('Battery percentage migration skipped'); | ||
|
||
return; | ||
} | ||
|
||
device.log('Migrating battery percentage...'); | ||
|
||
const status = await device.getStatus(); | ||
const batteryPercentage = status.find(s => s.code === 'battery_percentage'); | ||
if (!batteryPercentage) { | ||
device.log('Battery percentage not supported'); | ||
return; | ||
} | ||
|
||
await device.addCapability('measure_battery'); | ||
await device.safeSetCapabilityValue('measure_battery', batteryPercentage.value); | ||
|
||
device.log('Battery percentage added'); | ||
}); | ||
} | ||
|
||
async function addTemperAlarmMigration(device: TuyaOAuth2DeviceSensor): Promise<void> { | ||
await executeMigration(device, 'sensor_temper_alarm', async () => { | ||
if (device.hasCapability('alarm_tamper')) { | ||
return; | ||
} | ||
|
||
device.log('Migrating tamper alarm...'); | ||
|
||
const status = await device.getStatus(); | ||
const temperAlarm = status.find(s => s.code === 'temper_alarm'); | ||
if (!temperAlarm) { | ||
device.log('Tamper alarm not supported'); | ||
return; | ||
} | ||
|
||
await device.addCapability('alarm_tamper'); | ||
await device.safeSetCapabilityValue('alarm_tamper', temperAlarm.value); | ||
|
||
device.log('Tamper alarm added'); | ||
}); | ||
} |