Skip to content

Commit 1c5c483

Browse files
committed
Use Tuya's OpenAPI
1 parent 58096fb commit 1c5c483

File tree

4 files changed

+252
-262
lines changed

4 files changed

+252
-262
lines changed

index.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const Cloud = require('@tuyapi/cloud');
1+
const Cloud = require('@tuyapi/openapi');
22
const debug = require('debug')('@tuyapi/link:wizard');
33
const TuyaLink = require('./lib/link.js');
44

55
/**
6-
* A wrapper that combines `@tuyapi/cloud` and
6+
* A wrapper that combines `@tuyapi/openapi` and
77
* `(@tuyapi/link).manual` (included in this package)
88
* to make registration Just Work™️. Exported as
99
* `(@tuyapi/link).wizard`.
@@ -38,10 +38,8 @@ function TuyaLinkWizard(options) {
3838
this.timezone = options.timezone ? options.timezone : '-05:00';
3939

4040
// Don't need to check key and secret for correct format as
41-
// tuyapi/cloud already does
42-
this.api = new Cloud({key: options.apiKey,
43-
secret: options.apiSecret,
44-
region: this.region});
41+
// tuyapi/openapi already does
42+
this.api = new Cloud({key: options.apiKey, secret: options.apiSecret, schema: options.schema});
4543

4644
// Construct instance of TuyaLink
4745
this.device = new TuyaLink();
@@ -53,9 +51,11 @@ function TuyaLinkWizard(options) {
5351
* register.init()
5452
* @returns {Promise<String>} A Promise that contains the session ID
5553
*/
56-
TuyaLinkWizard.prototype.init = function () {
54+
TuyaLinkWizard.prototype.init = async function () {
5755
// Register/login user
58-
return this.api.register({email: this.email, password: this.password});
56+
await this.api.getToken();
57+
58+
this.uid = await this.api.putUser({countryCode: '1', username: this.email, password: this.password, usernameType: 2});
5959
};
6060

6161
/**
@@ -77,16 +77,17 @@ TuyaLinkWizard.prototype.init = function () {
7777
* @returns {Promise<Object>} A Promise that contains data on device(s)
7878
*/
7979
TuyaLinkWizard.prototype.linkDevice = async function (options) {
80-
if (!options.ssid || !options.wifiPassword) {
81-
throw new Error('Both SSID and WiFI password must be provided');
80+
if (!options.ssid) {
81+
throw new Error('SSID must be provided');
8282
}
8383

8484
// Default for options.devices
8585
options.devices = options.devices ? options.devices : 1;
8686

8787
try {
88-
const token = await this.api.request({action: 'tuya.m.device.token.create',
89-
data: {timeZone: this.timezone}});
88+
const token = await this.api.getDeviceToken({uid: this.uid, timezone: this.timezone})
89+
90+
// {action: 'tuya.m.device.token.create', data: {timeZone: this.timezone}});
9091

9192
debug('Token: ', token);
9293

@@ -99,8 +100,16 @@ TuyaLinkWizard.prototype.linkDevice = async function (options) {
99100
// While UDP packets are being sent, start polling for device
100101
debug('Polling cloud for details on token...');
101102

102-
const devices = await this.api.waitForToken({token: token.token,
103-
devices: options.devices});
103+
const devices = [];
104+
105+
const waitingForDevices = true;
106+
107+
while (waitingForDevices) {
108+
const d = await this.api.getDevicesByToken(token.token)
109+
debug('Got response:', d)
110+
}
111+
112+
104113
debug('Found device(s)!', devices);
105114

106115
// Stop broadcasting setup data

lib/link.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function TuyaLink(options) {
5050
*/
5151
TuyaLink.prototype.registerSmartLink = function (options) {
5252
// Check arguments
53-
if (options.region.length !== 2) {
53+
if (options.region.length !== 2 || ['AZ', 'AY', 'EU'].indexOf(options.region) === -1) {
5454
throw new Error('Invalid region');
5555
}
5656
if (options.token.length !== 8) {
@@ -177,6 +177,7 @@ TuyaLink.prototype.smartLinkEncode = function (options) {
177177
const ssidBytes = Buffer.from(options.ssid);
178178

179179
// Calculate size of byte array
180+
// (must add 1 byte for lengths)
180181
const rawByteArray = Buffer.alloc(1 +
181182
wifiPasswordBytes.length +
182183
1 +
@@ -222,7 +223,7 @@ TuyaLink.prototype.smartLinkEncode = function (options) {
222223
// Length encoded into the first two bytes based at 16 and then 32
223224
encodedData[0] = (stringLength / 16) | 16;
224225
encodedData[1] = (stringLength % 16) | 32;
225-
// Length CRC encoded into the next two bytes based at 46 and 64
226+
// Length CRC encoded into the next two bytes based at 48 and 64
226227
encodedData[2] = (stringLengthCRC / 16) | 48;
227228
encodedData[3] = (stringLengthCRC % 16) | 64;
228229

0 commit comments

Comments
 (0)