@tuyapi/link
Advanced tools
Comparing version 0.3.3 to 0.3.4
73
index.js
@@ -1,2 +0,3 @@ | ||
const Cloud = require('@tuyapi/openapi'); | ||
const {TuyaContext} = require('@tuya/tuya-connector-nodejs'); | ||
const delay = require('delay'); | ||
const debug = require('debug')('@tuyapi/link:wizard'); | ||
@@ -6,3 +7,3 @@ const TuyaLink = require('./lib/link.js'); | ||
/** | ||
* A wrapper that combines `@tuyapi/openapi` and | ||
* A wrapper that combines `@tuya/tuya-connector-nodejs` and | ||
* `(@tuyapi/link).manual` (included in this package) | ||
@@ -38,5 +39,8 @@ * to make registration Just Work™️. Exported as | ||
// Don't need to check key and secret for correct format as | ||
// tuyapi/openapi already does | ||
this.api = new Cloud({key: apiKey, secret: apiSecret, region, schema}); | ||
this.api = new TuyaContext({ | ||
baseUrl: `https://openapi.tuya${region}.com`, | ||
accessKey: apiKey, | ||
secretKey: apiSecret | ||
}); | ||
this.schema = schema; | ||
@@ -55,5 +59,20 @@ // Construct instance of TuyaLink | ||
// Register/login user | ||
await this.api.getToken(); | ||
const result = await this.api.request({ | ||
path: `/v1.0/apps/${this.schema}/user`, | ||
method: 'POST', | ||
body: { | ||
schema: this.schema, | ||
country_code: '1', | ||
username: this.email, | ||
password: this.password, | ||
username_type: 2, | ||
nick_name: this.email | ||
} | ||
}); | ||
this.uid = await this.api.putUser({countryCode: '1', username: this.email, password: this.password, usernameType: 2}); | ||
if (!result.success) { | ||
throw new Error(result.msg); | ||
} | ||
this.uid = result.result.uid; | ||
} | ||
@@ -65,3 +84,3 @@ | ||
* options | ||
* @param {Number} [options.timeout=60] | ||
* @param {Number} [options.timeout=100] | ||
* how long we should wait for devices to | ||
@@ -83,3 +102,3 @@ * connect before throwing an error, in seconds | ||
*/ | ||
async linkDevice({timeout = 60, ssid, wifiPassword = '', devices = 1} = {}) { | ||
async linkDevice({timeout = 100, ssid, wifiPassword = '', devices = 1} = {}) { | ||
if (!ssid) { | ||
@@ -90,4 +109,18 @@ throw new Error('SSID must be provided'); | ||
try { | ||
const token = await this.api.getDeviceToken({uid: this.uid, timezone: this.timezone}); | ||
const response = await this.api.request({ | ||
path: '/v1.0/device/paring/token', | ||
method: 'POST', | ||
body: { | ||
uid: this.uid, | ||
timeZoneId: this.timezone, | ||
paring_type: 'EZ' | ||
} | ||
}); | ||
if (!response.success) { | ||
throw new Error(response.msg); | ||
} | ||
const token = response.result; | ||
debug('Token: ', token); | ||
@@ -111,7 +144,16 @@ | ||
// eslint-disable-next-line no-await-in-loop | ||
lastAPIResponse = await this.api.getDevicesByToken(token.token); | ||
lastAPIResponse = await this.api.request({ | ||
path: `/v1.0/device/paring/tokens/${token.token}`, | ||
method: 'GET' | ||
}); | ||
debug(`${lastAPIResponse.successDevices.length} devices returned by API.`); | ||
if (!lastAPIResponse.success) { | ||
throw new Error(lastAPIResponse.msg); | ||
} | ||
if (lastAPIResponse.successDevices.length >= devices) { | ||
const {result} = lastAPIResponse; | ||
debug(`${result.success.length} devices returned by API.`); | ||
if (result.success.length >= devices) { | ||
waitingForDevices = false; | ||
@@ -126,5 +168,8 @@ } | ||
} | ||
// eslint-disable-next-line no-await-in-loop | ||
await delay(1000); | ||
} | ||
const returnedDevices = lastAPIResponse.successDevices; | ||
const returnedDevices = lastAPIResponse.result.success; | ||
@@ -131,0 +176,0 @@ debug('Found device(s)!', returnedDevices); |
{ | ||
"name": "@tuyapi/link", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "📡 Effortlessly connect devices to WiFi and the cloud", | ||
@@ -23,2 +23,3 @@ "main": "index.js", | ||
"rules": { | ||
"camelcase": "off", | ||
"indent": [ | ||
@@ -53,3 +54,3 @@ "error", | ||
"dependencies": { | ||
"@tuyapi/openapi": "^0.2.0", | ||
"@tuya/tuya-connector-nodejs": "^2.0.3", | ||
"debug": "^4.1.1", | ||
@@ -56,0 +57,0 @@ "delay": "^4.3.0" |
@@ -6,3 +6,3 @@ tuyapi/link [![Build Status](https://travis-ci.org/TuyaAPI/link.svg?branch=master)](https://travis-ci.org/TuyaAPI/link) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) | ||
- **`(@tuyapi/link).manual`**: a low-level interface for linking devices. | ||
- **`(@tuyapi/link).wizard`**: a wrapper that combines `(@tuyapi/link).manual` and `@tuyapi/cloud`, making it super easy to link devices. It Just Works™️. | ||
- **`(@tuyapi/link).wizard`**: a wrapper that combines `(@tuyapi/link).manual` and `@tuya/tuya-connector-nodejs`, making it super easy to link devices. It Just Works™️. | ||
@@ -9,0 +9,0 @@ ## Installation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20006
477
+ Added@tuya/tuya-connector-nodejs@2.1.2(transitive)
+ Addedaxios@0.21.4(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedside-channel@1.0.6(transitive)
- Removed@tuyapi/openapi@^0.2.0
- Removed@sindresorhus/is@2.1.1(transitive)
- Removed@szmarczak/http-timer@4.0.6(transitive)
- Removed@tuyapi/openapi@0.2.0(transitive)
- Removed@types/cacheable-request@6.0.3(transitive)
- Removed@types/http-cache-semantics@4.0.4(transitive)
- Removed@types/keyv@3.1.4(transitive)
- Removed@types/node@22.9.0(transitive)
- Removed@types/responselike@1.0.3(transitive)
- Removedcacheable-lookup@2.0.1(transitive)
- Removedcacheable-request@7.0.4(transitive)
- Removedclone-response@1.0.3(transitive)
- Removeddecompress-response@5.0.0(transitive)
- Removeddefer-to-connect@2.0.1(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedget-stream@5.2.0(transitive)
- Removedgot@10.7.0(transitive)
- Removedhttp-cache-semantics@4.1.1(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlowercase-keys@2.0.0(transitive)
- Removedmimic-response@1.0.12.1.0(transitive)
- Removednormalize-url@6.1.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-cancelable@2.1.1(transitive)
- Removedp-event@4.2.0(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedp-timeout@3.2.0(transitive)
- Removedpump@3.0.2(transitive)
- Removedresponselike@2.0.1(transitive)
- Removedto-readable-stream@2.1.0(transitive)
- Removedtype-fest@0.10.0(transitive)
- Removedundici-types@6.19.8(transitive)
- Removedwrappy@1.0.2(transitive)