Skip to content

Commit 80a8abd

Browse files
committed
WoHumi
1 parent a032891 commit 80a8abd

File tree

7 files changed

+183
-48
lines changed

7 files changed

+183
-48
lines changed

lib/switchbot-advertising.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ class SwitchbotAdvertising {
8585

8686
if (model === 'H') { // WoHand
8787
sd = this._parseServiceDataForWoHand(buf);
88+
} else if (model === 'h') { // WoHumi
89+
sd = this._parseServiceDataForWoHumi(buf);
8890
} else if (model === 'T') { // WoSensorTH
8991
sd = this._parseServiceDataForWoSensorTH(buf);
9092
} else if (model === 'c') { // WoCurtain
9193
sd = this._parseServiceDataForWoCurtain(buf);
94+
} else if (model === 'P') { // WoPresence
95+
sd = this._parseServiceDataForWoPresence(buf);
96+
} else if (model === 'C') { // WoContact
97+
sd = this._parseServiceDataForWoContact(buf);
9298
} else {
9399
return null;
94100
}
@@ -142,6 +148,26 @@ class SwitchbotAdvertising {
142148
return data;
143149
}
144150

151+
_parseServiceDataForWoHumi(buf) {
152+
if (buf.length !== 3) {
153+
return null;
154+
}
155+
let byte1 = buf.readUInt8(1);
156+
let byte2 = buf.readUInt8(2);
157+
158+
let mode = (byte1 & 0b10000000) ? true : false; // Whether the light switch Add-on is used or not
159+
let state = (byte1 & 0b01000000) ? true : false; // Whether the switch status is ON or OFF
160+
161+
let data = {
162+
model: 'h',
163+
modelName: 'WoHumi',
164+
mode: mode,
165+
state: state
166+
};
167+
168+
return data;
169+
}
170+
145171
_parseServiceDataForWoSensorTH(buf) {
146172
if (buf.length !== 6) {
147173
return null;
@@ -171,28 +197,28 @@ class SwitchbotAdvertising {
171197
return data;
172198
}
173199

174-
_parseServiceDataForWoSensorMS(buf) {
200+
_parseServiceDataForWoPresence(buf) {
175201
if (buf.length !== 6) {
176202
return null;
177203
}
178204

179205
let data = {
180-
model: 'M',
181-
modelName: 'WoSensorMS',
206+
model: 'P',
207+
modelName: 'WoPresence',
182208
battery: (byte2 & 0b01111111)
183209
};
184210

185211
return data;
186212
}
187213

188-
_parseServiceDataForWoSensorCS(buf) {
214+
_parseServiceDataForWoContact(buf) {
189215
if (buf.length !== 6) {
190216
return null;
191217
}
192218

193219
let data = {
194-
model: 'CS',
195-
modelName: 'WoSensorCS',
220+
model: 'C',
221+
modelName: 'WoContact',
196222
battery: (byte2 & 0b01111111)
197223
};
198224

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* ------------------------------------------------------------------
2-
* node-linking - switchbot-device-wosensorth.js
2+
* node-linking - switchbot-device-wocontact.js
33
*
44
* Copyright (c) 2019, Futomi Hatano, All rights reserved.
55
* Released under the MIT license
@@ -8,9 +8,9 @@
88
'use strict';
99
const SwitchbotDevice = require('./switchbot-device.js');
1010

11-
class SwitchbotDeviceWoSensorCS extends SwitchbotDevice {
11+
class SwitchbotDeviceWoContact extends SwitchbotDevice {
1212

1313

1414
}
1515

16-
module.exports = SwitchbotDeviceWoSensorCS;
16+
module.exports = SwitchbotDeviceWoContact;

lib/switchbot-device-wohumi.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* ------------------------------------------------------------------
2+
* node-linking - switchbot-device-wohumi.js
3+
*
4+
* Copyright (c) 2019-2020, Futomi Hatano, All rights reserved.
5+
* Released under the MIT license
6+
* Date: 2020-02-10
7+
* ---------------------------------------------------------------- */
8+
'use strict';
9+
const SwitchbotDevice = require('./switchbot-device.js');
10+
11+
class SwitchbotDeviceWoHumi extends SwitchbotDevice {
12+
13+
/* ------------------------------------------------------------------
14+
* press()
15+
* - Press
16+
*
17+
* [Arguments]
18+
* - none
19+
*
20+
* [Returen value]
21+
* - Promise object
22+
* Nothing will be passed to the `resolve()`.
23+
* ---------------------------------------------------------------- */
24+
press() {
25+
return this._operateBot([0x57, 0x01, 0x00]);
26+
}
27+
28+
/* ------------------------------------------------------------------
29+
* turnOn()
30+
* - Turn on
31+
*
32+
* [Arguments]
33+
* - none
34+
*
35+
* [Returen value]
36+
* - Promise object
37+
* Nothing will be passed to the `resolve()`.
38+
* ---------------------------------------------------------------- */
39+
turnOn() {
40+
return this._operateBot([0x57, 0x01, 0x01]);
41+
}
42+
43+
/* ------------------------------------------------------------------
44+
* turnOff()
45+
* - Turn off
46+
*
47+
* [Arguments]
48+
* - none
49+
*
50+
* [Returen value]
51+
* - Promise object
52+
* Nothing will be passed to the `resolve()`.
53+
* ---------------------------------------------------------------- */
54+
turnOff() {
55+
return this._operateBot([0x57, 0x01, 0x02]);
56+
}
57+
58+
/* ------------------------------------------------------------------
59+
* down()
60+
* - Down
61+
*
62+
* [Arguments]
63+
* - none
64+
*
65+
* [Returen value]
66+
* - Promise object
67+
* Nothing will be passed to the `resolve()`.
68+
* ---------------------------------------------------------------- */
69+
down() {
70+
return this._operateBot([0x57, 0x01, 0x03]);
71+
}
72+
73+
/* ------------------------------------------------------------------
74+
* up()
75+
* - Up
76+
*
77+
* [Arguments]
78+
* - none
79+
*
80+
* [Returen value]
81+
* - Promise object
82+
* Nothing will be passed to the `resolve()`.
83+
* ---------------------------------------------------------------- */
84+
up() {
85+
return this._operateBot([0x57, 0x01, 0x04]);
86+
}
87+
88+
_operateBot(bytes) {
89+
return new Promise((resolve, reject) => {
90+
let req_buf = Buffer.from(bytes);
91+
this._command(req_buf).then((res_buf) => {
92+
let code = res_buf.readUInt8(0);
93+
if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
94+
resolve();
95+
} else {
96+
reject(new Error('The device returned an error: 0x' + res_buf.toString('hex')));
97+
}
98+
}).catch((error) => {
99+
reject(error);
100+
});
101+
});
102+
}
103+
104+
}
105+
106+
module.exports = SwitchbotDeviceWoHumi;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* ------------------------------------------------------------------
2-
* node-linking - switchbot-device-wosensorth.js
2+
* node-linking - switchbot-device-wopresence.js
33
*
44
* Copyright (c) 2019, Futomi Hatano, All rights reserved.
55
* Released under the MIT license
@@ -8,9 +8,9 @@
88
'use strict';
99
const SwitchbotDevice = require('./switchbot-device.js');
1010

11-
class SwitchbotDeviceWoSensorMS extends SwitchbotDevice {
11+
class SwitchbotDeviceWoPresence extends SwitchbotDevice {
1212

1313

1414
}
1515

16-
module.exports = SwitchbotDeviceWoSensorMS;
16+
module.exports = SwitchbotDeviceWoPresence;

lib/switchbot.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ const switchbotAdvertising = require('./switchbot-advertising.js');
1212
const SwitchbotDevice = require('./switchbot-device.js');
1313
const SwitchbotDeviceWoHand = require('./switchbot-device-wohand.js');
1414
const SwitchbotDeviceWoCurtain = require('./switchbot-device-wocurtain.js');
15-
const SwitchbotDeviceWoSensorMS = require('./switchbot-device-wosensorms.js');
16-
const SwitchbotDeviceWoSensorCS = require('./switchbot-device-wosensorcs.js');
15+
const SwitchbotDeviceWoPresence = require('./switchbot-device-wopresence.js');
16+
const SwitchbotDeviceWoContact = require('./switchbot-device-wocontact.js');
1717
const SwitchbotDeviceWoSensorTH = require('./switchbot-device-wosensorth.js');
18+
const SwitchbotDeviceWoHumi = require('./switchbot-device-wohumi.js');
1819

1920
class Switchbot {
2021
/* ------------------------------------------------------------------
@@ -83,7 +84,7 @@ class Switchbot {
8384
// Check the parameters
8485
let valid = parameterChecker.check(params, {
8586
duration: { required: false, type: 'integer', min: 1, max: 60000 },
86-
model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] },
87+
model: { required: false, type: 'string', enum: ['H', 'h','T', 'P', 'C', 'c'] },
8788
id: { required: false, type: 'string', min: 12, max: 17 },
8889
quick: { required: false, type: 'boolean' }
8990
}, false);
@@ -190,14 +191,17 @@ class Switchbot {
190191
case 'H':
191192
device = new SwitchbotDeviceWoHand(peripheral, this.noble);
192193
break;
194+
case 'Hu':
195+
device = new SwitchbotDeviceWoHumi(peripheral, this.noble);
196+
break;
193197
case 'T':
194198
device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble);
195199
break;
196200
case 'M':
197-
device = new SwitchbotDeviceWoSensorMS(peripheral, this.noble);
201+
device = new SwitchbotDeviceWoPresence(peripheral, this.noble);
198202
break;
199203
case 'CS':
200-
device = new SwitchbotDeviceWoSensorCS(peripheral, this.noble);
204+
device = new SwitchbotDeviceWoContact(peripheral, this.noble);
201205
break;
202206
case 'c':
203207
device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
@@ -218,7 +222,7 @@ class Switchbot {
218222
if (id) {
219223
id = id.toLowerCase().replace(/\:/g, '');
220224
ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, '');
221-
if (ad_id !== id) {
225+
if (ad_id !== id) {
222226
return false;
223227
}
224228
}
@@ -268,7 +272,7 @@ class Switchbot {
268272
let promise = new Promise((resolve, reject) => {
269273
// Check the parameters
270274
let valid = parameterChecker.check(params, {
271-
model: { required: false, type: 'string', enum: ['H', 'T', 'M', 'CS', 'c'] },
275+
model: { required: false, type: 'string', enum: ['H', 'h', 'T', 'P', 'C', 'c'] },
272276
id: { required: false, type: 'string', min: 12, max: 17 },
273277
}, false);
274278

0 commit comments

Comments
 (0)