-
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.
Only retrieve details for 'other' device that is actually paired
- Loading branch information
Showing
3 changed files
with
87 additions
and
3 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
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...'); | ||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -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; | ||
} | ||
}; |
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