Skip to content

Commit 5eca9d0

Browse files
committed
Add 'wizard' device listing
1 parent e53d7f0 commit 5eca9d0

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cli.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const link = require('./lib/link');
1111
const listApp = require('./lib/list-app.js');
1212
const mock = require('./lib/mock.js');
1313
const control = require('./lib/control');
14+
const wizard = require('./lib/wizard');
1415
const pkg = require('./package.json');
1516

1617
// Set up config store
@@ -107,6 +108,11 @@ program
107108
mock(conf, options);
108109
});
109110

111+
program
112+
.command('wizard')
113+
.description('list devices from an offical app')
114+
.action(options => wizard(conf, options));
115+
110116
// Get help
111117
program
112118
.command('help')

lib/wizard.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const OpenAPI = require('@tuyapi/openapi');
2+
const inquirer = require('inquirer');
3+
const colors = require('colors');
4+
5+
const list = async conf => {
6+
let questions = [
7+
{
8+
name: 'deviceId',
9+
message: 'Provide the ID of a device currently registered in the app:'
10+
}
11+
];
12+
13+
const apiCredentialQuestions = [
14+
{
15+
name: 'apiKey',
16+
message: 'The API key from tuya.com:'
17+
},
18+
{
19+
name: 'apiSecret',
20+
message: 'The API secret from tuya.com'
21+
},
22+
{
23+
name: 'region',
24+
message: 'Select the region closest to you:',
25+
type: 'list',
26+
choices: [
27+
{name: 'Americas', value: 'us'},
28+
{name: 'Europe', value: 'eu'},
29+
{name: 'Asia', value: 'cn'}
30+
]
31+
}
32+
];
33+
34+
const savedAPIKey = conf.get('apiKey');
35+
const savedAPISecret = conf.get('apiSecret');
36+
const savedAPIRegion = conf.get('apiRegion');
37+
38+
let useExistingKeys = false;
39+
if (savedAPIKey && savedAPISecret && savedAPIRegion) {
40+
const answers = await inquirer.prompt([
41+
{
42+
type: 'confirm',
43+
name: 'useExistingKeys',
44+
message: `Do you want to use these saved API credentials? ${savedAPIKey} ${savedAPISecret} ${savedAPIRegion}`
45+
}
46+
]);
47+
48+
({useExistingKeys} = answers);
49+
50+
if (!useExistingKeys) {
51+
questions = [...apiCredentialQuestions, ...questions];
52+
}
53+
} else {
54+
questions = [...apiCredentialQuestions, ...questions];
55+
}
56+
57+
const answers = await inquirer.prompt(questions);
58+
59+
if (useExistingKeys) {
60+
answers.apiKey = savedAPIKey;
61+
answers.apiSecret = savedAPISecret;
62+
answers.region = savedAPIRegion;
63+
}
64+
65+
// Create API instance
66+
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: answers.region});
67+
68+
await api.getToken();
69+
70+
// Get seed device
71+
let userId = null;
72+
73+
try {
74+
const device = await api.getDevice(answers.deviceId);
75+
76+
userId = device.uid;
77+
} catch {
78+
console.error(colors.red('There was an issue fetching that device. Make sure your account is linked and the ID is correct.'));
79+
80+
// eslint-disable-next-line unicorn/no-process-exit
81+
process.exit(1);
82+
}
83+
84+
// Get user devices
85+
const devices = await api.getDevicesByUser(userId);
86+
87+
// Output devices
88+
console.log(devices.map(device => ({name: device.name, id: device.id, key: device.local_key})));
89+
90+
// Save API key and secret
91+
conf.set('apiKey', answers.apiKey);
92+
conf.set('apiSecret', answers.apiSecret);
93+
conf.set('apiRegion', answers.region);
94+
};
95+
96+
module.exports = list;

0 commit comments

Comments
 (0)