@switchbot/homebridge-switchbot 1.15.0 → 1.15.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +12 -5
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +10 -10
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +15 -1
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +140 -13
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +12 -9
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +13 -9
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +9 -8
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +2 -1
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +29 -18
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +6 -5
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +6 -5
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +7 -7
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +5 -5
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +7 -11
- package/dist/device/striplight.js.map +1 -1
- package/dist/platform.d.ts +7 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +75 -13
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +3 -1
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +1 -1
- package/dist/settings.js.map +1 -1
- package/package.json +81 -79
- package/src/device/bot.ts +10 -10
- package/src/device/colorbulb.ts +169 -18
- package/src/device/contact.ts +13 -10
- package/src/device/curtain.ts +13 -9
- package/src/device/humidifier.ts +9 -8
- package/src/device/lock.ts +4 -3
- package/src/device/meter.ts +46 -36
- package/src/device/meterplus.ts +6 -5
- package/src/device/motion.ts +6 -5
- package/src/device/plug.ts +13 -13
- package/src/device/striplight.ts +8 -14
- package/src/platform.ts +81 -13
- package/src/settings.ts +3 -1
package/package.json
CHANGED
|
@@ -1,83 +1,85 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
},
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/OpenWonderLabs/homebridge-switchbot/issues"
|
|
14
|
-
},
|
|
15
|
-
"engines": {
|
|
16
|
-
"homebridge": "^1.5.0",
|
|
17
|
-
"node": "^14.20.0 || ^16.17.0 || ^18.8.0"
|
|
18
|
-
},
|
|
19
|
-
"main": "dist/index.js",
|
|
20
|
-
"scripts": {
|
|
21
|
-
"check": "npm install && npm outdated",
|
|
22
|
-
"update": "ncu -u && npm update && npm install",
|
|
23
|
-
"lint": "eslint src/**.ts",
|
|
24
|
-
"watch": "npm run build && npm link && nodemon",
|
|
25
|
-
"build": "rimraf ./dist && tsc",
|
|
26
|
-
"prepublishOnly": "npm run lint && npm run build",
|
|
27
|
-
"postpublish": "npm run clean",
|
|
28
|
-
"clean": "rimraf ./dist",
|
|
29
|
-
"test": "eslint src/**.ts"
|
|
30
|
-
},
|
|
31
|
-
"funding": [
|
|
32
|
-
{
|
|
33
|
-
"type": "Paypal",
|
|
34
|
-
"url": "https://paypal.me/donavanbecker"
|
|
2
|
+
"displayName": "Homebridge SwitchBot",
|
|
3
|
+
"name": "@switchbot/homebridge-switchbot",
|
|
4
|
+
"version": "1.15.1-beta.1",
|
|
5
|
+
"description": "The [Homebridge](https://homebridge.io) SwitchBot plugin allows you to access your [SwitchBot](https://www.switch-bot.com) device(s) from HomeKit.",
|
|
6
|
+
"author": "SwitchBot <support@wondertechlabs.com> (https://github.com/SwitchBot)",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git://github.com/OpenWonderLabs/homebridge-switchbot.git"
|
|
35
11
|
},
|
|
36
|
-
{
|
|
37
|
-
|
|
38
|
-
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/OpenWonderLabs/homebridge-switchbot/issues"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"homebridge": "^1.5.0",
|
|
17
|
+
"node": "^14.20.0 || ^16.17.0 || ^18.8.0"
|
|
18
|
+
},
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"check": "npm install && npm outdated",
|
|
22
|
+
"update": "ncu -u && npm update && npm install",
|
|
23
|
+
"lint": "eslint src/**.ts",
|
|
24
|
+
"watch": "npm run build && npm link && nodemon",
|
|
25
|
+
"build": "rimraf ./dist && tsc",
|
|
26
|
+
"prepublishOnly": "npm run lint && npm run build",
|
|
27
|
+
"postpublish": "npm run clean",
|
|
28
|
+
"clean": "rimraf ./dist",
|
|
29
|
+
"test": "eslint src/**.ts"
|
|
30
|
+
},
|
|
31
|
+
"funding": [
|
|
32
|
+
{
|
|
33
|
+
"type": "Paypal",
|
|
34
|
+
"url": "https://paypal.me/donavanbecker"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"type": "GitHub",
|
|
38
|
+
"url": "https://github.com/sponsors/donavanbecker"
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"homebridge-plugin",
|
|
46
|
+
"switchbot",
|
|
47
|
+
"humidifier",
|
|
48
|
+
"humidity",
|
|
49
|
+
"temperature",
|
|
50
|
+
"bot",
|
|
51
|
+
"curtains",
|
|
52
|
+
"windows",
|
|
53
|
+
"ble",
|
|
54
|
+
"ir"
|
|
55
|
+
],
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@homebridge/plugin-ui-utils": "^0.0.19",
|
|
58
|
+
"async-mqtt": "^2.6.2",
|
|
59
|
+
"axios": "^0.27.2",
|
|
60
|
+
"crypto-js": "^4.1.1",
|
|
61
|
+
"fakegato-history": "^0.6.3",
|
|
62
|
+
"rxjs": "^7.5.6"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@types/crypto-js": "^4.1.1",
|
|
66
|
+
"@types/node": "^18.7.13",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^5.35.1",
|
|
68
|
+
"@typescript-eslint/parser": "^5.35.1",
|
|
69
|
+
"eslint": "^8.23.0",
|
|
70
|
+
"eslint-config-prettier": "8.5.0",
|
|
71
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
72
|
+
"homebridge": "^1.5.0",
|
|
73
|
+
"nodemon": "^2.0.19",
|
|
74
|
+
"npm-check-updates": "^16.0.6",
|
|
75
|
+
"prettier": "2.7.1",
|
|
76
|
+
"rimraf": "^3.0.2",
|
|
77
|
+
"ts-node": "^10.9.1",
|
|
78
|
+
"typescript": "^4.8.2",
|
|
79
|
+
"typescript-axios-wb": "^1.0.3"
|
|
80
|
+
},
|
|
81
|
+
"optionalDependencies": {
|
|
82
|
+
"@abandonware/noble": "^1.9.2-15",
|
|
83
|
+
"node-switchbot": "^1.4.2-beta.0"
|
|
39
84
|
}
|
|
40
|
-
],
|
|
41
|
-
"publishConfig": {
|
|
42
|
-
"access": "public"
|
|
43
|
-
},
|
|
44
|
-
"keywords": [
|
|
45
|
-
"homebridge-plugin",
|
|
46
|
-
"switchbot",
|
|
47
|
-
"humidifier",
|
|
48
|
-
"humidity",
|
|
49
|
-
"temperature",
|
|
50
|
-
"bot",
|
|
51
|
-
"curtains",
|
|
52
|
-
"windows",
|
|
53
|
-
"ble",
|
|
54
|
-
"ir"
|
|
55
|
-
],
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"@homebridge/plugin-ui-utils": "^0.0.19",
|
|
58
|
-
"async-mqtt": "^2.6.2",
|
|
59
|
-
"axios": "^0.27.2",
|
|
60
|
-
"fakegato-history": "^0.6.3",
|
|
61
|
-
"rxjs": "^7.5.6"
|
|
62
|
-
},
|
|
63
|
-
"devDependencies": {
|
|
64
|
-
"@types/node": "^18.7.13",
|
|
65
|
-
"@typescript-eslint/eslint-plugin": "^5.35.1",
|
|
66
|
-
"@typescript-eslint/parser": "^5.35.1",
|
|
67
|
-
"eslint": "^8.23.0",
|
|
68
|
-
"eslint-config-prettier": "8.5.0",
|
|
69
|
-
"eslint-plugin-prettier": "4.2.1",
|
|
70
|
-
"homebridge": "^1.5.0",
|
|
71
|
-
"nodemon": "^2.0.19",
|
|
72
|
-
"npm-check-updates": "^16.0.6",
|
|
73
|
-
"prettier": "2.7.1",
|
|
74
|
-
"rimraf": "^3.0.2",
|
|
75
|
-
"ts-node": "^10.9.1",
|
|
76
|
-
"typescript": "^4.8.2",
|
|
77
|
-
"typescript-axios-wb": "^1.0.3"
|
|
78
|
-
},
|
|
79
|
-
"optionalDependencies": {
|
|
80
|
-
"@abandonware/noble": "^1.9.2-15",
|
|
81
|
-
"node-switchbot": "^1.4.1"
|
|
82
|
-
}
|
|
83
85
|
}
|
package/src/device/bot.ts
CHANGED
|
@@ -96,7 +96,8 @@ export class Bot {
|
|
|
96
96
|
.setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-BOT-S1')
|
|
97
97
|
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
|
|
98
98
|
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision(accessory, device))
|
|
99
|
-
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
99
|
+
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
100
|
+
.updateValue(this.FirmwareRevision(accessory, device));
|
|
100
101
|
|
|
101
102
|
// get the service if it exists, otherwise create a new service
|
|
102
103
|
// you can create multiple services for each accessory
|
|
@@ -414,7 +415,7 @@ export class Bot {
|
|
|
414
415
|
if (this.device.ble) {
|
|
415
416
|
this.SwitchToOpenAPI = false;
|
|
416
417
|
}
|
|
417
|
-
if (this.platform.config.credentials?.
|
|
418
|
+
if (this.platform.config.credentials?.token) {
|
|
418
419
|
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI parseStatus`);
|
|
419
420
|
if (this.botMode === 'press') {
|
|
420
421
|
this.On = false;
|
|
@@ -509,7 +510,7 @@ export class Bot {
|
|
|
509
510
|
this.updateHomeKitCharacteristics();
|
|
510
511
|
} else {
|
|
511
512
|
this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
512
|
-
if (this.platform.config.credentials?.
|
|
513
|
+
if (this.platform.config.credentials?.token) {
|
|
513
514
|
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
514
515
|
this.SwitchToOpenAPI = true;
|
|
515
516
|
await this.openAPIRefreshStatus();
|
|
@@ -520,11 +521,10 @@ export class Bot {
|
|
|
520
521
|
this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
521
522
|
if (this.deviceLogging.includes('debug')) {
|
|
522
523
|
this.errorLog(
|
|
523
|
-
`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
|
|
524
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
524
|
+
`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
525
525
|
);
|
|
526
526
|
}
|
|
527
|
-
if (this.platform.config.credentials?.
|
|
527
|
+
if (this.platform.config.credentials?.token) {
|
|
528
528
|
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
529
529
|
this.SwitchToOpenAPI = true;
|
|
530
530
|
await this.openAPIRefreshStatus();
|
|
@@ -552,7 +552,7 @@ export class Bot {
|
|
|
552
552
|
}
|
|
553
553
|
|
|
554
554
|
async openAPIRefreshStatus(): Promise<void> {
|
|
555
|
-
if (this.platform.config.credentials?.
|
|
555
|
+
if (this.platform.config.credentials?.token) {
|
|
556
556
|
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI refreshStatus`);
|
|
557
557
|
try {
|
|
558
558
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
|
@@ -639,7 +639,7 @@ export class Bot {
|
|
|
639
639
|
if (this.platform.debugMode) {
|
|
640
640
|
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
641
641
|
}
|
|
642
|
-
if (this.platform.config.credentials?.
|
|
642
|
+
if (this.platform.config.credentials?.token) {
|
|
643
643
|
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
644
644
|
await this.openAPIpushChanges();
|
|
645
645
|
}
|
|
@@ -668,7 +668,7 @@ export class Bot {
|
|
|
668
668
|
if (this.platform.debugMode) {
|
|
669
669
|
this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
|
|
670
670
|
}
|
|
671
|
-
if (this.platform.config.credentials?.
|
|
671
|
+
if (this.platform.config.credentials?.token) {
|
|
672
672
|
this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
673
673
|
await this.openAPIpushChanges();
|
|
674
674
|
}
|
|
@@ -696,7 +696,7 @@ export class Bot {
|
|
|
696
696
|
}
|
|
697
697
|
|
|
698
698
|
async openAPIpushChanges(): Promise<void> {
|
|
699
|
-
if (this.platform.config.credentials?.
|
|
699
|
+
if (this.platform.config.credentials?.token) {
|
|
700
700
|
try {
|
|
701
701
|
if (this.On !== this.OnCached || this.allowPush) {
|
|
702
702
|
this.debugLog(`Bot: ${this.accessory.displayName} OpenAPI pushChanges`);
|
package/src/device/colorbulb.ts
CHANGED
|
@@ -3,7 +3,20 @@ import { interval, Subject } from 'rxjs';
|
|
|
3
3
|
import { SwitchBotPlatform } from '../platform';
|
|
4
4
|
import { debounceTime, skipWhile, tap } from 'rxjs/operators';
|
|
5
5
|
import { Service, PlatformAccessory, CharacteristicValue, ControllerConstructor, Controller, ControllerServiceMap } from 'homebridge';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
DeviceURL,
|
|
8
|
+
device,
|
|
9
|
+
devicesConfig,
|
|
10
|
+
switchbot,
|
|
11
|
+
deviceStatusResponse,
|
|
12
|
+
payload,
|
|
13
|
+
hs2rgb,
|
|
14
|
+
rgb2hs,
|
|
15
|
+
m2hs,
|
|
16
|
+
deviceStatus,
|
|
17
|
+
serviceData,
|
|
18
|
+
ad,
|
|
19
|
+
} from '../settings';
|
|
7
20
|
import { Context } from 'vm';
|
|
8
21
|
|
|
9
22
|
/**
|
|
@@ -35,10 +48,18 @@ export class ColorBulb {
|
|
|
35
48
|
deviceStatus!: deviceStatusResponse;
|
|
36
49
|
|
|
37
50
|
// BLE Others
|
|
51
|
+
connected?: boolean;
|
|
38
52
|
switchbot!: switchbot;
|
|
53
|
+
address!: ad['address'];
|
|
54
|
+
SwitchToOpenAPI?: boolean;
|
|
55
|
+
serviceData!: serviceData;
|
|
56
|
+
state: serviceData['state'];
|
|
57
|
+
delay: serviceData['delay'];
|
|
58
|
+
wifiRssi: serviceData['wifiRssi'];
|
|
39
59
|
|
|
40
60
|
// Config
|
|
41
61
|
set_minStep?: number;
|
|
62
|
+
scanDuration!: number;
|
|
42
63
|
deviceLogging!: string;
|
|
43
64
|
deviceRefreshRate!: number;
|
|
44
65
|
adaptiveLightingShift?: number;
|
|
@@ -86,7 +107,8 @@ export class ColorBulb {
|
|
|
86
107
|
.setCharacteristic(this.platform.Characteristic.Model, 'W1401400')
|
|
87
108
|
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
|
|
88
109
|
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision(accessory, device))
|
|
89
|
-
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
110
|
+
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
111
|
+
.updateValue(this.FirmwareRevision(accessory, device));
|
|
90
112
|
|
|
91
113
|
// get the Lightbulb service if it exists, otherwise create a new Lightbulb service
|
|
92
114
|
// you can create multiple services for each accessory
|
|
@@ -212,6 +234,27 @@ export class ColorBulb {
|
|
|
212
234
|
}
|
|
213
235
|
|
|
214
236
|
async parseStatus(): Promise<void> {
|
|
237
|
+
if (this.SwitchToOpenAPI || !this.device.ble) {
|
|
238
|
+
await this.openAPIparseStatus();
|
|
239
|
+
} else {
|
|
240
|
+
await this.BLEparseStatus();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async BLEparseStatus(): Promise<void> {
|
|
245
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE parseStatus`);
|
|
246
|
+
// State
|
|
247
|
+
switch (this.state) {
|
|
248
|
+
case 'on':
|
|
249
|
+
this.On = true;
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
this.On = false;
|
|
253
|
+
}
|
|
254
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} On: ${this.On}`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async openAPIparseStatus() {
|
|
215
258
|
switch (this.power) {
|
|
216
259
|
case 'on':
|
|
217
260
|
this.On = true;
|
|
@@ -234,9 +277,7 @@ export class ColorBulb {
|
|
|
234
277
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} blue: ${JSON.stringify(blue)}`);
|
|
235
278
|
|
|
236
279
|
const [hue, saturation] = rgb2hs(Number(red), Number(green), Number(blue));
|
|
237
|
-
this.debugLog(
|
|
238
|
-
`Color Bulb: ${this.accessory.displayName} hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`,
|
|
239
|
-
);
|
|
280
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} hs: ${JSON.stringify(rgb2hs(Number(red), Number(green), Number(blue)))}`);
|
|
240
281
|
|
|
241
282
|
// Hue
|
|
242
283
|
this.Hue = hue;
|
|
@@ -257,10 +298,126 @@ export class ColorBulb {
|
|
|
257
298
|
this.ColorTemperature = Math.max(Math.min(this.ColorTemperature, 500), 140);
|
|
258
299
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} ColorTemperature: ${this.ColorTemperature}`);
|
|
259
300
|
}
|
|
260
|
-
|
|
261
301
|
}
|
|
262
302
|
|
|
263
303
|
async refreshStatus(): Promise<void> {
|
|
304
|
+
if (this.device.ble) {
|
|
305
|
+
await this.BLERefreshStatus();
|
|
306
|
+
} else {
|
|
307
|
+
await this.openAPIRefreshStatus();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async BLERefreshStatus(): Promise<void> {
|
|
312
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE refreshStatus`);
|
|
313
|
+
const switchbot = await this.platform.connectBLE();
|
|
314
|
+
// Convert to BLE Address
|
|
315
|
+
this.device.bleMac = this.device
|
|
316
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
317
|
+
.join(':')
|
|
318
|
+
.toLowerCase();
|
|
319
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
320
|
+
this.getCustomBLEAddress(switchbot);
|
|
321
|
+
// Start to monitor advertisement packets
|
|
322
|
+
if (switchbot !== false) {
|
|
323
|
+
switchbot
|
|
324
|
+
.startScan({
|
|
325
|
+
model: 'u',
|
|
326
|
+
id: this.device.bleMac,
|
|
327
|
+
})
|
|
328
|
+
.then(() => {
|
|
329
|
+
// Set an event hander
|
|
330
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
331
|
+
this.address = ad.address;
|
|
332
|
+
if (this.deviceLogging.includes('debug')) {
|
|
333
|
+
this.infoLog(this.address);
|
|
334
|
+
this.infoLog(this.device.bleMac);
|
|
335
|
+
this.infoLog(`Color Bulb: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
|
|
336
|
+
this.infoLog(`Color Bulb: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
|
|
337
|
+
}
|
|
338
|
+
this.serviceData = ad.serviceData;
|
|
339
|
+
this.state = ad.serviceData.state;
|
|
340
|
+
this.delay = ad.serviceData.delay;
|
|
341
|
+
//this.timer = ad.serviceData.timer;
|
|
342
|
+
//this.syncUtcTime = ad.serviceData.syncUtcTime;
|
|
343
|
+
this.wifiRssi = ad.serviceData.wifiRssi;
|
|
344
|
+
//this.overload = ad.serviceData.overload;
|
|
345
|
+
//this.currentPower = ad.serviceData.currentPower;
|
|
346
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
347
|
+
this.debugLog(
|
|
348
|
+
`Color Bulb: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
|
|
349
|
+
`delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
|
|
350
|
+
`wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
if (this.serviceData) {
|
|
354
|
+
this.connected = true;
|
|
355
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
356
|
+
} else {
|
|
357
|
+
this.connected = false;
|
|
358
|
+
this.debugLog(`Color Bulb: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
// Wait 2 seconds
|
|
362
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
363
|
+
})
|
|
364
|
+
.then(async () => {
|
|
365
|
+
// Stop to monitor
|
|
366
|
+
switchbot.stopScan();
|
|
367
|
+
if (this.connected) {
|
|
368
|
+
this.parseStatus();
|
|
369
|
+
this.updateHomeKitCharacteristics();
|
|
370
|
+
} else {
|
|
371
|
+
await this.BLEconnection(switchbot);
|
|
372
|
+
}
|
|
373
|
+
})
|
|
374
|
+
.catch(async (e: any) => {
|
|
375
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
376
|
+
if (this.deviceLogging.includes('debug')) {
|
|
377
|
+
this.errorLog(
|
|
378
|
+
`Color Bulb: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
if (this.platform.config.credentials?.token) {
|
|
382
|
+
this.warnLog(`Color Bulb: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
383
|
+
this.SwitchToOpenAPI = true;
|
|
384
|
+
await this.openAPIRefreshStatus();
|
|
385
|
+
}
|
|
386
|
+
this.apiError(e);
|
|
387
|
+
});
|
|
388
|
+
} else {
|
|
389
|
+
await this.BLEconnection(switchbot);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async getCustomBLEAddress(switchbot: any) {
|
|
394
|
+
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
395
|
+
(async () => {
|
|
396
|
+
// Start to monitor advertisement packets
|
|
397
|
+
await switchbot.startScan({
|
|
398
|
+
model: 'u',
|
|
399
|
+
});
|
|
400
|
+
// Set an event handler
|
|
401
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
402
|
+
this.warnLog(JSON.stringify(ad, null, ' '));
|
|
403
|
+
};
|
|
404
|
+
await switchbot.wait(10000);
|
|
405
|
+
// Stop to monitor
|
|
406
|
+
switchbot.stopScan();
|
|
407
|
+
})();
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async BLEconnection(switchbot: any): Promise<void> {
|
|
412
|
+
this.errorLog(`Color Bulb: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
413
|
+
if (this.platform.config.credentials?.token) {
|
|
414
|
+
this.warnLog(`Color Bulb: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
415
|
+
this.SwitchToOpenAPI = true;
|
|
416
|
+
await this.openAPIRefreshStatus();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private async openAPIRefreshStatus() {
|
|
264
421
|
try {
|
|
265
422
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
|
266
423
|
this.debugLog(`Color Bulb: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
|
|
@@ -275,8 +432,7 @@ export class ColorBulb {
|
|
|
275
432
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
|
|
276
433
|
if (this.deviceLogging.includes('debug')) {
|
|
277
434
|
this.errorLog(
|
|
278
|
-
`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
|
|
279
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
435
|
+
`Color Bulb: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
280
436
|
);
|
|
281
437
|
}
|
|
282
438
|
this.apiError(e);
|
|
@@ -339,8 +495,7 @@ export class ColorBulb {
|
|
|
339
495
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
340
496
|
if (this.deviceLogging.includes('debug')) {
|
|
341
497
|
this.errorLog(
|
|
342
|
-
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
|
|
343
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
498
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
344
499
|
);
|
|
345
500
|
}
|
|
346
501
|
this.apiError(e);
|
|
@@ -383,8 +538,7 @@ export class ColorBulb {
|
|
|
383
538
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
384
539
|
if (this.deviceLogging.includes('debug')) {
|
|
385
540
|
this.errorLog(
|
|
386
|
-
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
|
|
387
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
541
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
388
542
|
);
|
|
389
543
|
}
|
|
390
544
|
this.apiError(e);
|
|
@@ -423,8 +577,7 @@ export class ColorBulb {
|
|
|
423
577
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
424
578
|
if (this.deviceLogging.includes('debug')) {
|
|
425
579
|
this.errorLog(
|
|
426
|
-
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
|
|
427
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
580
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
428
581
|
);
|
|
429
582
|
}
|
|
430
583
|
this.apiError(e);
|
|
@@ -452,16 +605,14 @@ export class ColorBulb {
|
|
|
452
605
|
this.BrightnessCached = this.Brightness;
|
|
453
606
|
} else {
|
|
454
607
|
this.debugLog(
|
|
455
|
-
`Color Bulb: ${this.accessory.displayName} No Changes.` +
|
|
456
|
-
`Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}`,
|
|
608
|
+
`Color Bulb: ${this.accessory.displayName} No Changes.` + `Brightness: ${this.Brightness}, BrightnessCached: ${this.BrightnessCached}`,
|
|
457
609
|
);
|
|
458
610
|
}
|
|
459
611
|
} catch (e: any) {
|
|
460
612
|
this.errorLog(`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
|
|
461
613
|
if (this.deviceLogging.includes('debug')) {
|
|
462
614
|
this.errorLog(
|
|
463
|
-
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` +
|
|
464
|
-
` Error Message: ${JSON.stringify(e.message)}`,
|
|
615
|
+
`Color Bulb: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
465
616
|
);
|
|
466
617
|
}
|
|
467
618
|
this.apiError(e);
|
package/src/device/contact.ts
CHANGED
|
@@ -74,7 +74,8 @@ export class Contact {
|
|
|
74
74
|
.setCharacteristic(this.platform.Characteristic.Model, 'W1201500')
|
|
75
75
|
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!)
|
|
76
76
|
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision(accessory, device))
|
|
77
|
-
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
77
|
+
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
78
|
+
.updateValue(this.FirmwareRevision(accessory, device));
|
|
78
79
|
|
|
79
80
|
// get the Contact service if it exists, otherwise create a new Contact service
|
|
80
81
|
// you can create multiple services for each accessory
|
|
@@ -216,7 +217,7 @@ export class Contact {
|
|
|
216
217
|
if (this.device.ble) {
|
|
217
218
|
this.SwitchToOpenAPI = false;
|
|
218
219
|
}
|
|
219
|
-
if (this.platform.config.credentials?.
|
|
220
|
+
if (this.platform.config.credentials?.token) {
|
|
220
221
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
|
|
221
222
|
// Contact State
|
|
222
223
|
if (this.openState === 'open') {
|
|
@@ -265,10 +266,12 @@ export class Contact {
|
|
|
265
266
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
|
|
266
267
|
const switchbot = await this.platform.connectBLE();
|
|
267
268
|
// Convert to BLE Address
|
|
268
|
-
this.device.bleMac =
|
|
269
|
-
.
|
|
270
|
-
.
|
|
271
|
-
|
|
269
|
+
this.device.bleMac =
|
|
270
|
+
this.device.customBLEaddress ||
|
|
271
|
+
this.device
|
|
272
|
+
.deviceId!.match(/.{1,2}/g)!
|
|
273
|
+
.join(':')
|
|
274
|
+
.toLowerCase();
|
|
272
275
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
273
276
|
this.getCustomBLEAddress(switchbot);
|
|
274
277
|
// Start to monitor advertisement packets
|
|
@@ -296,7 +299,7 @@ export class Contact {
|
|
|
296
299
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
297
300
|
this.debugLog(
|
|
298
301
|
`Contact Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, doorState: ` +
|
|
299
|
-
|
|
302
|
+
`${ad.serviceData.doorState}, lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
|
|
300
303
|
);
|
|
301
304
|
|
|
302
305
|
if (this.serviceData) {
|
|
@@ -328,7 +331,7 @@ export class Contact {
|
|
|
328
331
|
` Error Message: ${JSON.stringify(e.message)}`,
|
|
329
332
|
);
|
|
330
333
|
}
|
|
331
|
-
if (this.platform.config.credentials?.
|
|
334
|
+
if (this.platform.config.credentials?.token) {
|
|
332
335
|
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
333
336
|
this.SwitchToOpenAPI = true;
|
|
334
337
|
await this.openAPIRefreshStatus();
|
|
@@ -360,7 +363,7 @@ export class Contact {
|
|
|
360
363
|
|
|
361
364
|
async BLEconnection(switchbot: any): Promise<void> {
|
|
362
365
|
this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
363
|
-
if (this.platform.config.credentials?.
|
|
366
|
+
if (this.platform.config.credentials?.token) {
|
|
364
367
|
this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
365
368
|
this.SwitchToOpenAPI = true;
|
|
366
369
|
await this.openAPIRefreshStatus();
|
|
@@ -368,7 +371,7 @@ export class Contact {
|
|
|
368
371
|
}
|
|
369
372
|
|
|
370
373
|
async openAPIRefreshStatus(): Promise<void> {
|
|
371
|
-
if (this.platform.config.credentials?.
|
|
374
|
+
if (this.platform.config.credentials?.token) {
|
|
372
375
|
this.debugLog(`Contact Sensor: ${this.accessory.displayName} OpenAPI refreshStatus`);
|
|
373
376
|
try {
|
|
374
377
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
package/src/device/curtain.ts
CHANGED
|
@@ -90,7 +90,8 @@ export class Curtain {
|
|
|
90
90
|
.setCharacteristic(this.platform.Characteristic.Model, 'W0701600')
|
|
91
91
|
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId)
|
|
92
92
|
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.FirmwareRevision(accessory, device))
|
|
93
|
-
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
93
|
+
.getCharacteristic(this.platform.Characteristic.FirmwareRevision)
|
|
94
|
+
.updateValue(this.FirmwareRevision(accessory, device));
|
|
94
95
|
|
|
95
96
|
// get the WindowCovering service if it exists, otherwise create a new WindowCovering service
|
|
96
97
|
// you can create multiple services for each accessory
|
|
@@ -212,7 +213,10 @@ export class Curtain {
|
|
|
212
213
|
* 'homebridge-switchbot/curtain/xx:xx:xx:xx:xx:xx'
|
|
213
214
|
*/
|
|
214
215
|
mqttPublish(topic: string, message: any) {
|
|
215
|
-
const mac = this.device.deviceId
|
|
216
|
+
const mac = this.device.deviceId
|
|
217
|
+
?.toLowerCase()
|
|
218
|
+
.match(/[\s\S]{1,2}/g)
|
|
219
|
+
?.join(':');
|
|
216
220
|
const options = this.device.mqttPubOptions || {};
|
|
217
221
|
this.mqttClient?.publish(`homebridge-switchbot/curtain/${mac}/${topic}`, `${message}`, options);
|
|
218
222
|
this.debugLog(`Meter: ${this.accessory.displayName} MQTT message: ${topic}/${message} options:${JSON.stringify(options)}`);
|
|
@@ -355,7 +359,7 @@ export class Curtain {
|
|
|
355
359
|
if (this.device.ble) {
|
|
356
360
|
this.SwitchToOpenAPI = false;
|
|
357
361
|
}
|
|
358
|
-
if (this.platform.config.credentials?.
|
|
362
|
+
if (this.platform.config.credentials?.token) {
|
|
359
363
|
this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI parseStatus`);
|
|
360
364
|
// CurrentPosition
|
|
361
365
|
this.CurrentPosition = 100 - Number(this.slidePosition);
|
|
@@ -485,7 +489,7 @@ export class Curtain {
|
|
|
485
489
|
`Curtain: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
486
490
|
);
|
|
487
491
|
}
|
|
488
|
-
if (this.platform.config.credentials?.
|
|
492
|
+
if (this.platform.config.credentials?.token) {
|
|
489
493
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
490
494
|
this.SwitchToOpenAPI = true;
|
|
491
495
|
await this.openAPIRefreshStatus();
|
|
@@ -517,7 +521,7 @@ export class Curtain {
|
|
|
517
521
|
|
|
518
522
|
async BLEconnection(switchbot: any): Promise<void> {
|
|
519
523
|
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
520
|
-
if (this.platform.config.credentials?.
|
|
524
|
+
if (this.platform.config.credentials?.token) {
|
|
521
525
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
522
526
|
this.SwitchToOpenAPI = true;
|
|
523
527
|
await this.openAPIRefreshStatus();
|
|
@@ -525,7 +529,7 @@ export class Curtain {
|
|
|
525
529
|
}
|
|
526
530
|
|
|
527
531
|
async openAPIRefreshStatus(): Promise<void> {
|
|
528
|
-
if (this.platform.config.credentials?.
|
|
532
|
+
if (this.platform.config.credentials?.token) {
|
|
529
533
|
this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI refreshStatus`);
|
|
530
534
|
try {
|
|
531
535
|
this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
|
|
@@ -609,7 +613,7 @@ export class Curtain {
|
|
|
609
613
|
`Curtain: ${this.accessory.displayName} failed pushChanges with BLE Connection,` + ` Error Message: ${JSON.stringify(e.message)}`,
|
|
610
614
|
);
|
|
611
615
|
}
|
|
612
|
-
if (this.platform.config.credentials?.
|
|
616
|
+
if (this.platform.config.credentials?.token) {
|
|
613
617
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
614
618
|
await this.openAPIpushChanges();
|
|
615
619
|
}
|
|
@@ -617,7 +621,7 @@ export class Curtain {
|
|
|
617
621
|
});
|
|
618
622
|
} else {
|
|
619
623
|
this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
620
|
-
if (this.platform.config.credentials?.
|
|
624
|
+
if (this.platform.config.credentials?.token) {
|
|
621
625
|
this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
622
626
|
await this.openAPIpushChanges();
|
|
623
627
|
}
|
|
@@ -631,7 +635,7 @@ export class Curtain {
|
|
|
631
635
|
}
|
|
632
636
|
|
|
633
637
|
async openAPIpushChanges(): Promise<void> {
|
|
634
|
-
if (this.platform.config.credentials?.
|
|
638
|
+
if (this.platform.config.credentials?.token) {
|
|
635
639
|
try {
|
|
636
640
|
this.debugLog(`Curtain: ${this.accessory.displayName} OpenAPI pushChanges`);
|
|
637
641
|
if (this.TargetPosition !== this.CurrentPosition) {
|