1- const Cloud = require ( '@tuyapi/cloud ' ) ;
1+ const Cloud = require ( '@tuyapi/openapi ' ) ;
22const debug = require ( 'debug' ) ( '@tuyapi/link:wizard' ) ;
33const 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*/
7979TuyaLinkWizard . 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
0 commit comments