Skip to content

Commit

Permalink
Only retrieve details for 'other' device that is actually paired
Browse files Browse the repository at this point in the history
  • Loading branch information
JELoohuis committed Sep 16, 2024
1 parent 8d9e943 commit add5992
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
52 changes: 51 additions & 1 deletion drivers/other/device.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
import TuyaOAuth2Device from '../../lib/TuyaOAuth2Device';
import * as TuyaOAuth2Util from '../../lib/TuyaOAuth2Util';

module.exports = class TuyaOAuth2DeviceOther extends TuyaOAuth2Device {};
module.exports = class TuyaOAuth2DeviceOther extends TuyaOAuth2Device {
async onOAuth2Init(): Promise<void> {
await super.onOAuth2Init();

const tuyaCombinedSpecifications = this.store.tuya_combined_specifications;

// Initialize the details on first init
if (
tuyaCombinedSpecifications?.device !== undefined &&
(tuyaCombinedSpecifications?.specifications === undefined ||
tuyaCombinedSpecifications?.data_points === undefined)
) {
this.log('Initializing device details...');
const { deviceId } = this.data;

const deviceSpecs =
tuyaCombinedSpecifications.specifications ??
(await this.oAuth2Client
.getSpecification(deviceId)
.catch(e => this.log('Device specification retrieval failed', e))) ??
null;
const dataPoints =
tuyaCombinedSpecifications.data_points ??
(await this.oAuth2Client
.queryDataPoints(deviceId)
.catch(e => this.log('Device properties retrieval failed', e))) ??
null;

const combinedSpecification = {
device: TuyaOAuth2Util.redactFields(tuyaCombinedSpecifications.device),
specifications: deviceSpecs ?? '<not available>',
data_points: dataPoints?.properties ?? '<not available>',
};

await this.setSettings({
deviceSpecification: JSON.stringify(combinedSpecification, undefined, 2),
}).catch(this.error);

const newSpecifications = {
device: tuyaCombinedSpecifications.device,
specifications: deviceSpecs,
data_points: dataPoints,
};

await this.setStoreValue('tuya_combined_specifications', newSpecifications).catch(this.error);

this.log('Finished initializing device details...');
}
}
};
33 changes: 31 additions & 2 deletions drivers/other/driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
import TuyaOAuth2Driver from '../../lib/TuyaOAuth2Driver';
import TuyaOAuth2Client from '../../lib/TuyaOAuth2Client';
import { OAuth2DeviceResult } from 'homey-oauth2app';
import * as TuyaOAuth2Util from '../../lib/TuyaOAuth2Util';

module.exports = class TuyaOAuth2DriverOther extends TuyaOAuth2Driver {
onTuyaPairListDeviceFilter(): boolean {
return true; // Accept any device
async onPairListDevices({ oAuth2Client }: { oAuth2Client: TuyaOAuth2Client }): Promise<OAuth2DeviceResult[]> {
const devices = await oAuth2Client.getDevices();
const listDevices: OAuth2DeviceResult[] = [];

this.log('Listing devices to pair:');

for (const device of devices) {
this.log('Device:', JSON.stringify(TuyaOAuth2Util.redactFields(device)));

// GitHub #178: Some device do not have the status property at all.
// Make sure to populate it with an empty array instead.
if (!Array.isArray(device.status)) {
device.status = [];
}

const deviceProperties = super.onTuyaPairListDeviceProperties({ ...device }, undefined, undefined);

listDevices.push({
...deviceProperties,
name: device.name,
data: {
deviceId: device.id,
productId: device.product_id,
},
});
}

return listDevices;
}
};
5 changes: 5 additions & 0 deletions lib/TuyaOAuth2Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export default class TuyaOAuth2Driver extends OAuth2Driver<TuyaOAuth2Client> {
return {
capabilities: [],
store: {
tuya_combined_specifications: {
device,
specifications,
data_points: dataPoints,
},
tuya_capabilities: [],
tuya_category: device.category,
},
Expand Down

0 comments on commit add5992

Please sign in to comment.