Skip to content

Commit 0b87342

Browse files
committed
Merge branch 'master' of github.com:TuyaAPI/cli
2 parents b0131c0 + 10bc381 commit 0b87342

File tree

5 files changed

+1851
-3975
lines changed

5 files changed

+1851
-3975
lines changed

cli.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const Configstore = require('configstore');
88
// Import local files
99
const cloud = require('./lib/cloud');
1010
const link = require('./lib/link');
11-
const listApp = require('./lib/list-app.js');
12-
const mock = require('./lib/mock.js');
11+
const listApp = require('./lib/list-app');
12+
const mock = require('./lib/mock');
1313
const control = require('./lib/control');
1414
const wizard = require('./lib/wizard');
1515
const pkg = require('./package.json');
@@ -54,8 +54,9 @@ program
5454
.option('--ip <ip_addr>', 'ip address of device')
5555
.option('--id <id>', 'id of device')
5656
.option('--key [key]', 'key of device')
57-
.option('--dps [dps]', 'property index to get', 1)
58-
.option('-a, --all', 'get all properties of a device', false)
57+
.option('--cid [cid_zigbee_device]', 'if specified, use device id of zigbee gateway and cid of subdevice to get its status')
58+
.option('--dps [dps]', 'property index to get')
59+
.option('--full', 'get full response payload', false)
5960
.option('--protocol-version [version]', 'tuya protocol version', parseFloat, 3.1)
6061
.action(options => {
6162
control.get(conf, options);
@@ -69,6 +70,7 @@ program
6970
.option('--ip <ip_addr>', 'ip address of device')
7071
.option('--id <id>', 'id of device')
7172
.option('--key [key]', 'key of device')
73+
.option('--cid [cid_zigbee_device]', 'if specified, use device id of zigbee gateway and cid of subdevice to set its property')
7274
.option('--set <set>', 'value to set')
7375
.option('--raw-value', 'pass the raw set value without attempting to parse it from a string')
7476
.option('--dps [dps]', 'DPS index to set', 1)

lib/control.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ const TuyAPI = require('tuyapi');
33
const c = require('./common');
44

55
function parseConfig(config, options) {
6-
if (options.id === undefined) {
6+
const apiOptions = {
7+
issueGetOnConnect: false,
8+
version: options.protocolVersion,
9+
id: options.id,
10+
ip: options.ip,
11+
key: options.key || config.get(options.id)
12+
};
13+
14+
// If key is not given and does not exit in config
15+
if (!apiOptions.key) {
716
c.badArgument(options);
817
}
918

10-
if (Number.isNaN(Number(options.protocolVersion))) {
19+
if (!apiOptions.id) {
20+
c.badArgument(options);
21+
}
22+
23+
if (Number.isNaN(Number(apiOptions.version))) {
1124
c.badArgument(options);
1225
}
1326

@@ -16,26 +29,7 @@ function parseConfig(config, options) {
1629
config.set(options.id, options.key);
1730
}
1831

19-
// If key is not given but exists in config
20-
if (options.key === undefined && config.get(options.id) !== undefined) {
21-
if (options.ip === undefined) {
22-
return new TuyAPI({version: options.protocolVersion, id: options.id, key: config.get(options.id)});
23-
}
24-
25-
return new TuyAPI({version: options.protocolVersion, ip: options.ip, id: options.id, key: config.get(options.id)});
26-
}
27-
28-
// If key is not given and does not exit in config
29-
if (options.key === undefined && config.get(options.id) === undefined) {
30-
c.badArgument(options);
31-
} else {
32-
// If both arguments are given
33-
if (options.ip === undefined) {
34-
return new TuyAPI({version: options.protocolVersion, id: options.id, key: options.key});
35-
}
36-
37-
return new TuyAPI({version: options.protocolVersion, ip: options.ip, id: options.id, key: options.key});
38-
}
32+
return new TuyAPI(apiOptions);
3933
}
4034

4135
async function get(config, options) {
@@ -46,14 +40,20 @@ async function get(config, options) {
4640

4741
await tuya.connect();
4842

49-
const properties = await tuya.get({schema: true});
43+
const properties = await tuya.get({schema: true, cid: options.cid, dps: options.dps});
5044

5145
tuya.disconnect();
5246

53-
if (options.all) {
54-
console.log(JSON.stringify(properties));
47+
if (properties) {
48+
if (options.dps) {
49+
console.log(properties.dps[options.dps]);
50+
} else if (options.full) {
51+
console.log(properties);
52+
} else {
53+
console.log(properties.dps);
54+
}
5555
} else {
56-
console.log(properties.dps[options.dps]);
56+
throw new Error('No response from device');
5757
}
5858
} catch (error) {
5959
console.log(error);
@@ -82,7 +82,7 @@ async function set(config, options) {
8282

8383
await tuya.connect();
8484

85-
await tuya.set({set: options.set, dps: options.dps});
85+
await tuya.set({set: options.set, dps: options.dps, cid: options.cid});
8686

8787
tuya.disconnect();
8888

lib/wizard.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,47 @@ const list = async (conf, options) => {
9696
// Get user devices
9797
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: foundAPIRegion});
9898
await api.getToken();
99+
99100
const devices = await api.getDevicesByUser(userId);
100101

102+
const groupedDevices = {};
103+
for (const device of devices) {
104+
if (device.node_id) {
105+
if (!groupedDevices[device.local_key] || !groupedDevices[device.local_key].subDevices) {
106+
groupedDevices[device.local_key] = {...groupedDevices[device.local_key], subDevices: []};
107+
}
108+
109+
groupedDevices[device.local_key].subDevices.push(device);
110+
} else {
111+
groupedDevices[device.local_key] = {...device, ...groupedDevices[device.local_key]};
112+
}
113+
}
114+
101115
// Output devices
102-
const prettyDevices = devices.map(device => ({name: device.name, id: device.id, key: device.local_key}));
116+
const prettyDevices = Object.values(groupedDevices).map(device => {
117+
const pretty = {
118+
name: device.name,
119+
id: device.id,
120+
key: device.local_key
121+
};
122+
123+
if (device.subDevices) {
124+
const prettySubDevices = device.subDevices.map(subDevice => ({
125+
name: subDevice.name,
126+
id: subDevice.id,
127+
cid: subDevice.node_id
128+
}));
129+
130+
pretty.subDevices = prettySubDevices;
131+
}
132+
133+
return pretty;
134+
});
103135

104136
if (options.stringify) {
105137
console.log(JSON.stringify(prettyDevices));
106138
} else {
107-
console.log(prettyDevices);
139+
console.dir(prettyDevices, {depth: 3});
108140
}
109141

110142
// Save API key and secret

0 commit comments

Comments
 (0)