@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.3
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/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +203 -255
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +9 -4
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +196 -218
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +9 -4
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +202 -247
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +10 -5
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +329 -378
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +9 -4
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +99 -142
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +19 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +203 -264
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +28 -32
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +186 -225
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +9 -4
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +198 -227
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +9 -5
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +106 -231
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +12 -4
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +192 -232
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +9 -4
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +91 -157
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +10 -5
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +287 -367
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -5
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +171 -216
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +9 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +93 -158
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +9 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +94 -155
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +9 -4
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +84 -142
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -4
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +99 -124
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +9 -6
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +170 -253
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +12 -4
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +102 -128
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +2 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +2 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +2 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +2 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +2 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +1 -1
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +2 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +2 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +2 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +2 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +2 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +5 -7
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +51 -34
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -191
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +19 -0
- package/dist/settings.js.map +1 -1
- package/dist/types/bledevicestatus.d.ts +279 -0
- package/dist/types/bledevicestatus.d.ts.map +1 -0
- package/dist/types/bledevicestatus.js +2 -0
- package/dist/types/bledevicestatus.js.map +1 -0
- package/dist/types/devicelist.d.ts +89 -0
- package/dist/types/devicelist.d.ts.map +1 -0
- package/dist/types/devicelist.js +6 -0
- package/dist/types/devicelist.js.map +1 -0
- package/dist/types/deviceresponse.d.ts +13 -0
- package/dist/types/deviceresponse.d.ts.map +1 -0
- package/dist/types/deviceresponse.js +2 -0
- package/dist/types/deviceresponse.js.map +1 -0
- package/dist/types/devicestatus.d.ts +149 -0
- package/dist/types/devicestatus.d.ts.map +1 -0
- package/dist/types/devicestatus.js +3 -0
- package/dist/types/devicestatus.js.map +1 -0
- package/dist/types/devicewebhookstatus.d.ts +156 -0
- package/dist/types/devicewebhookstatus.d.ts.map +1 -0
- package/dist/types/devicewebhookstatus.js +2 -0
- package/dist/types/devicewebhookstatus.js.map +1 -0
- package/dist/types/irdevicelist.d.ts +10 -0
- package/dist/types/irdevicelist.d.ts.map +1 -0
- package/dist/types/irdevicelist.js +2 -0
- package/dist/types/irdevicelist.js.map +1 -0
- package/dist/types/pushbody.d.ts +6 -0
- package/dist/types/pushbody.d.ts.map +1 -0
- package/dist/types/pushbody.js +2 -0
- package/dist/types/pushbody.js.map +1 -0
- package/dist/utils.d.ts +25 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/device/blindtilt.ts +223 -263
- package/src/device/bot.ts +208 -221
- package/src/device/ceilinglight.ts +210 -257
- package/src/device/colorbulb.ts +334 -397
- package/src/device/contact.ts +111 -146
- package/src/device/curtain.ts +220 -283
- package/src/device/device.ts +194 -245
- package/src/device/fan.ts +214 -230
- package/src/device/hub.ts +126 -246
- package/src/device/humidifier.ts +216 -243
- package/src/device/iosensor.ts +114 -166
- package/src/device/lightstrip.ts +302 -377
- package/src/device/lock.ts +187 -221
- package/src/device/meter.ts +116 -177
- package/src/device/meterplus.ts +117 -174
- package/src/device/motion.ts +99 -144
- package/src/device/plug.ts +110 -128
- package/src/device/robotvacuumcleaner.ts +189 -252
- package/src/device/waterdetector.ts +120 -138
- package/src/irdevice/airconditioner.ts +2 -1
- package/src/irdevice/airpurifier.ts +2 -1
- package/src/irdevice/camera.ts +2 -1
- package/src/irdevice/fan.ts +2 -1
- package/src/irdevice/irdevice.ts +3 -2
- package/src/irdevice/light.ts +2 -1
- package/src/irdevice/other.ts +2 -1
- package/src/irdevice/tv.ts +2 -1
- package/src/irdevice/vacuumcleaner.ts +2 -1
- package/src/irdevice/waterheater.ts +2 -1
- package/src/platform.ts +52 -34
- package/src/settings.ts +30 -239
- package/src/types/bledevicestatus.ts +305 -0
- package/src/types/devicelist.ts +124 -0
- package/src/types/deviceresponse.ts +15 -0
- package/src/types/devicestatus.ts +178 -0
- package/src/types/devicewebhookstatus.ts +196 -0
- package/src/types/irdevicelist.ts +15 -0
- package/src/types/pushbody.ts +9 -0
- package/src/utils.ts +33 -0
package/dist/device/device.js
CHANGED
|
@@ -6,7 +6,8 @@ import { hostname } from 'os';
|
|
|
6
6
|
import { request } from 'undici';
|
|
7
7
|
import asyncmqtt from 'async-mqtt';
|
|
8
8
|
import { Devices } from '../settings.js';
|
|
9
|
-
import { BlindTiltMappingMode,
|
|
9
|
+
import { BlindTiltMappingMode, sleep } from '../utils.js';
|
|
10
|
+
import { SwitchBotModel, SwitchBotBLEModel, SwitchBotBLEModelName, SwitchBotBLEModelFriendlyName } from 'node-switchbot';
|
|
10
11
|
export class deviceBase {
|
|
11
12
|
platform;
|
|
12
13
|
accessory;
|
|
@@ -47,11 +48,11 @@ export class deviceBase {
|
|
|
47
48
|
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
48
49
|
this.getDeviceLogSettings(accessory, device);
|
|
49
50
|
this.getDeviceRateSettings(accessory, device);
|
|
50
|
-
this.getDeviceRetry(
|
|
51
|
+
this.getDeviceRetry(device);
|
|
51
52
|
this.getDeviceConfigSettings(accessory, device);
|
|
52
53
|
this.getDeviceContext(accessory, device);
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
54
|
+
this.getDeviceScanDuration(accessory, device);
|
|
55
|
+
this.setupMqtt(device);
|
|
55
56
|
// Set accessory information
|
|
56
57
|
accessory
|
|
57
58
|
.getService(this.hap.Service.AccessoryInformation)
|
|
@@ -66,82 +67,81 @@ export class deviceBase {
|
|
|
66
67
|
async getDeviceLogSettings(accessory, device) {
|
|
67
68
|
if (this.platform.debugMode) {
|
|
68
69
|
this.deviceLogging = accessory.context.logging = 'debugMode';
|
|
69
|
-
this.debugWarnLog(
|
|
70
|
+
this.debugWarnLog(`Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
70
71
|
}
|
|
71
72
|
else if (device.logging) {
|
|
72
73
|
this.deviceLogging = accessory.context.logging = device.logging;
|
|
73
|
-
this.debugWarnLog(
|
|
74
|
+
this.debugWarnLog(`Using Device Config Logging: ${this.deviceLogging}`);
|
|
74
75
|
}
|
|
75
76
|
else if (this.config.logging) {
|
|
76
77
|
this.deviceLogging = accessory.context.logging = this.config.logging;
|
|
77
|
-
this.debugWarnLog(
|
|
78
|
+
this.debugWarnLog(`Using Platform Config Logging: ${this.deviceLogging}`);
|
|
78
79
|
}
|
|
79
80
|
else {
|
|
80
81
|
this.deviceLogging = accessory.context.logging = 'standard';
|
|
81
|
-
this.debugWarnLog(
|
|
82
|
+
this.debugWarnLog(`Logging Not Set, Using: ${this.deviceLogging}`);
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
async getDeviceRateSettings(accessory, device) {
|
|
85
86
|
// refreshRate
|
|
86
87
|
if (device.refreshRate) {
|
|
87
88
|
this.deviceRefreshRate = device.refreshRate;
|
|
88
|
-
this.debugLog(
|
|
89
|
+
this.debugLog(`Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
89
90
|
}
|
|
90
91
|
else if (this.config.options?.refreshRate) {
|
|
91
92
|
this.deviceRefreshRate = this.config.options.refreshRate;
|
|
92
|
-
this.debugLog(
|
|
93
|
+
this.debugLog(`Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
93
94
|
}
|
|
94
95
|
else {
|
|
95
96
|
this.deviceRefreshRate = 5;
|
|
96
|
-
this.debugLog(
|
|
97
|
+
this.debugLog(`Using Default refreshRate: ${this.deviceRefreshRate}`);
|
|
97
98
|
}
|
|
98
99
|
accessory.context.deviceRefreshRate = this.deviceRefreshRate;
|
|
99
100
|
// updateRate
|
|
100
101
|
if (device.updateRate) {
|
|
101
102
|
this.deviceUpdateRate = device.updateRate;
|
|
102
|
-
this.debugLog(
|
|
103
|
+
this.debugLog(`Using Device Config updateRate: ${this.deviceUpdateRate}`);
|
|
103
104
|
}
|
|
104
105
|
else if (this.config.options?.updateRate) {
|
|
105
106
|
this.deviceUpdateRate = this.config.options.updateRate;
|
|
106
|
-
this.debugLog(
|
|
107
|
+
this.debugLog(`Using Platform Config updateRate: ${this.deviceUpdateRate}`);
|
|
107
108
|
}
|
|
108
109
|
else {
|
|
109
110
|
this.deviceUpdateRate = 5;
|
|
110
|
-
this.debugLog(
|
|
111
|
+
this.debugLog(`Using Default updateRate: ${this.deviceUpdateRate}`);
|
|
111
112
|
}
|
|
112
113
|
accessory.context.deviceUpdateRate = this.deviceUpdateRate;
|
|
113
114
|
// pushRate
|
|
114
115
|
if (device.pushRate) {
|
|
115
116
|
this.devicePushRate = device.pushRate;
|
|
116
|
-
this.debugLog(
|
|
117
|
+
this.debugLog(`Using Device Config pushRate: ${this.deviceUpdateRate}`);
|
|
117
118
|
}
|
|
118
119
|
else if (this.config.options?.pushRate) {
|
|
119
120
|
this.devicePushRate = this.config.options.pushRate;
|
|
120
|
-
this.debugLog(
|
|
121
|
+
this.debugLog(`Using Platform Config pushRate: ${this.deviceUpdateRate}`);
|
|
121
122
|
}
|
|
122
123
|
else {
|
|
123
124
|
this.devicePushRate = 1;
|
|
124
|
-
this.debugLog(
|
|
125
|
+
this.debugLog(`Using Default pushRate: ${this.deviceUpdateRate}`);
|
|
125
126
|
}
|
|
126
127
|
accessory.context.devicePushRate = this.devicePushRate;
|
|
127
128
|
}
|
|
128
|
-
async getDeviceRetry(
|
|
129
|
+
async getDeviceRetry(device) {
|
|
129
130
|
if (device.maxRetries) {
|
|
130
131
|
this.deviceMaxRetries = device.maxRetries;
|
|
131
|
-
this.debugLog(
|
|
132
|
+
this.debugLog(`Using Device Max Retries: ${this.deviceMaxRetries}`);
|
|
132
133
|
}
|
|
133
134
|
else {
|
|
134
135
|
this.deviceMaxRetries = 5; // Maximum number of retries
|
|
135
|
-
this.debugLog(
|
|
136
|
+
this.debugLog(`Max Retries Not Set, Using: ${this.deviceMaxRetries}`);
|
|
136
137
|
}
|
|
137
138
|
if (device.delayBetweenRetries) {
|
|
138
139
|
this.deviceDelayBetweenRetries = device.delayBetweenRetries * 1000;
|
|
139
|
-
this.debugLog(
|
|
140
|
+
this.debugLog(`Using Device Delay Between Retries: ${this.deviceDelayBetweenRetries}`);
|
|
140
141
|
}
|
|
141
142
|
else {
|
|
142
143
|
this.deviceDelayBetweenRetries = 3000; // Delay between retries in milliseconds
|
|
143
|
-
this.debugLog(
|
|
144
|
-
+ ` Using: ${this.deviceDelayBetweenRetries}`);
|
|
144
|
+
this.debugLog(`Delay Between Retries Not Set, Using: ${this.deviceDelayBetweenRetries}`);
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
async retryBLE({ max, fn }) {
|
|
@@ -149,8 +149,8 @@ export class deviceBase {
|
|
|
149
149
|
if (max === 0) {
|
|
150
150
|
throw e;
|
|
151
151
|
}
|
|
152
|
-
this.
|
|
153
|
-
this.infoLog(
|
|
152
|
+
await this.warnLog(e);
|
|
153
|
+
await this.infoLog('Retrying');
|
|
154
154
|
await sleep(1000);
|
|
155
155
|
return this.retryBLE({ max: max - 1, fn });
|
|
156
156
|
});
|
|
@@ -163,20 +163,19 @@ export class deviceBase {
|
|
|
163
163
|
return 5;
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
|
-
async
|
|
166
|
+
async getDeviceScanDuration(accessory, device) {
|
|
167
167
|
if (device.scanDuration) {
|
|
168
168
|
if (this.deviceUpdateRate > device.scanDuration) {
|
|
169
169
|
this.scanDuration = this.deviceUpdateRate;
|
|
170
170
|
if (this.BLE) {
|
|
171
|
-
this.warnLog(
|
|
172
|
-
+ `${accessory.displayName} scanDuration is less than updateRate, overriding scanDuration with updateRate`);
|
|
171
|
+
this.warnLog('scanDuration is less than updateRate, overriding scanDuration with updateRate');
|
|
173
172
|
}
|
|
174
173
|
}
|
|
175
174
|
else {
|
|
176
175
|
this.scanDuration = accessory.context.scanDuration = device.scanDuration;
|
|
177
176
|
}
|
|
178
177
|
if (this.BLE) {
|
|
179
|
-
this.debugLog(
|
|
178
|
+
this.debugLog(`Using Device Config scanDuration: ${this.scanDuration}`);
|
|
180
179
|
}
|
|
181
180
|
}
|
|
182
181
|
else {
|
|
@@ -187,7 +186,7 @@ export class deviceBase {
|
|
|
187
186
|
this.scanDuration = accessory.context.scanDuration = 1;
|
|
188
187
|
}
|
|
189
188
|
if (this.BLE) {
|
|
190
|
-
this.debugLog(
|
|
189
|
+
this.debugLog(`Using Default scanDuration: ${this.scanDuration}`);
|
|
191
190
|
}
|
|
192
191
|
}
|
|
193
192
|
}
|
|
@@ -294,14 +293,43 @@ export class deviceBase {
|
|
|
294
293
|
}
|
|
295
294
|
const config = Object.assign({}, deviceConfig, botConfig, curtainConfig, waterdetectorConfig, striplightConfig, plugConfig, iosensorConfig, meterConfig, humidifierConfig, hubConfig, lockConfig, ceilinglightConfig, colorbulbConfig, contactConfig, motionConfig, blindTiltConfig);
|
|
296
295
|
if (Object.entries(config).length !== 0) {
|
|
297
|
-
this.debugSuccessLog(
|
|
296
|
+
this.debugSuccessLog(`Config: ${JSON.stringify(config)}`);
|
|
298
297
|
}
|
|
299
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Get the current ambient light level based on the light level, set_minLux, set_maxLux, and spaceBetweenLevels.
|
|
301
|
+
* @param lightLevel: number
|
|
302
|
+
* @param set_minLux: number
|
|
303
|
+
* @param set_maxLux: number
|
|
304
|
+
* @param spaceBetweenLevels: number
|
|
305
|
+
* @returns CurrentAmbientLightLevel
|
|
306
|
+
*/
|
|
307
|
+
async getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels) {
|
|
308
|
+
const numberOfLevels = spaceBetweenLevels + 1;
|
|
309
|
+
this.debugLog(`LightLevel: ${lightLevel}, set_minLux: ${set_minLux}, set_maxLux: ${set_maxLux}, spaceBetweenLevels: ${spaceBetweenLevels},`
|
|
310
|
+
+ ` numberOfLevels: ${numberOfLevels}`);
|
|
311
|
+
let CurrentAmbientLightLevel;
|
|
312
|
+
switch (lightLevel) {
|
|
313
|
+
case 1:
|
|
314
|
+
CurrentAmbientLightLevel = set_minLux;
|
|
315
|
+
this.debugLog(`LightLevel: ${lightLevel}, set_minLux: ${set_minLux}`);
|
|
316
|
+
break;
|
|
317
|
+
case numberOfLevels:
|
|
318
|
+
CurrentAmbientLightLevel = set_maxLux;
|
|
319
|
+
this.debugLog(`LightLevel: ${lightLevel}, set_maxLux: ${set_maxLux}`);
|
|
320
|
+
break;
|
|
321
|
+
default:
|
|
322
|
+
CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * (Number(lightLevel) - 1);
|
|
323
|
+
this.debugLog(`LightLevel: ${lightLevel}`);
|
|
324
|
+
}
|
|
325
|
+
this.debugLog(`CurrentAmbientLightLevel: ${CurrentAmbientLightLevel}`);
|
|
326
|
+
return CurrentAmbientLightLevel;
|
|
327
|
+
}
|
|
300
328
|
/*
|
|
301
329
|
* Publish MQTT message for topics of
|
|
302
330
|
* 'homebridge-switchbot/${this.device.deviceType}/xx:xx:xx:xx:xx:xx'
|
|
303
331
|
*/
|
|
304
|
-
mqttPublish(message, topic) {
|
|
332
|
+
async mqttPublish(message, topic) {
|
|
305
333
|
const mac = this.device.deviceId
|
|
306
334
|
?.toLowerCase()
|
|
307
335
|
.match(/[\s\S]{1,2}/g)
|
|
@@ -318,25 +346,24 @@ export class deviceBase {
|
|
|
318
346
|
mqttMessageTopic = '';
|
|
319
347
|
}
|
|
320
348
|
this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options);
|
|
321
|
-
this.debugLog(
|
|
322
|
-
+ ` MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`);
|
|
349
|
+
this.debugLog(`MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`);
|
|
323
350
|
}
|
|
324
351
|
/*
|
|
325
352
|
* Setup MQTT hadler if URL is specified.
|
|
326
353
|
*/
|
|
327
|
-
async setupMqtt(
|
|
354
|
+
async setupMqtt(device) {
|
|
328
355
|
if (device.mqttURL) {
|
|
329
356
|
try {
|
|
330
357
|
const { connectAsync } = asyncmqtt;
|
|
331
358
|
this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
|
|
332
|
-
this.debugLog(
|
|
359
|
+
this.debugLog('MQTT connection has been established successfully.');
|
|
333
360
|
this.mqttClient.on('error', (e) => {
|
|
334
|
-
this.errorLog(
|
|
361
|
+
this.errorLog(`Failed to publish MQTT messages. ${e}`);
|
|
335
362
|
});
|
|
336
363
|
}
|
|
337
364
|
catch (e) {
|
|
338
365
|
this.mqttClient = null;
|
|
339
|
-
this.errorLog(
|
|
366
|
+
this.errorLog(`Failed to establish MQTT connection. ${e}`);
|
|
340
367
|
}
|
|
341
368
|
}
|
|
342
369
|
}
|
|
@@ -357,7 +384,7 @@ export class deviceBase {
|
|
|
357
384
|
: null;
|
|
358
385
|
}
|
|
359
386
|
async switchbotBLE() {
|
|
360
|
-
const switchbot = await this.platform.connectBLE();
|
|
387
|
+
const switchbot = await this.platform.connectBLE(this.accessory, this.device);
|
|
361
388
|
// Convert to BLE Address
|
|
362
389
|
await this.convertBLEAddress();
|
|
363
390
|
await this.getCustomBLEAddress(switchbot);
|
|
@@ -368,24 +395,24 @@ export class deviceBase {
|
|
|
368
395
|
.deviceId.match(/.{1,2}/g)
|
|
369
396
|
.join(':')
|
|
370
397
|
.toLowerCase();
|
|
371
|
-
this.debugLog(
|
|
398
|
+
this.debugLog(`BLE Address: ${this.device.bleMac}`);
|
|
372
399
|
}
|
|
373
400
|
async monitorAdvertisementPackets(switchbot) {
|
|
374
|
-
this.debugLog(
|
|
401
|
+
this.debugLog(`Scanning for ${this.device.bleModelName} devices...`);
|
|
375
402
|
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
376
403
|
// Set an event handler
|
|
377
404
|
let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName };
|
|
378
405
|
switchbot.onadvertisement = async (ad) => {
|
|
379
|
-
this.warnLog(
|
|
406
|
+
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
380
407
|
if (this.device.bleMac === ad.address && ad.serviceData.model === this.device.bleModel) {
|
|
381
|
-
this.debugLog(`${
|
|
382
|
-
this.debugLog(
|
|
383
|
-
this.debugLog(
|
|
408
|
+
this.debugLog(`${JSON.stringify(ad, null, ' ')}`);
|
|
409
|
+
this.debugLog(`address: ${ad.address}, model: ${ad.serviceData.model}`);
|
|
410
|
+
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
384
411
|
serviceData = ad.serviceData;
|
|
385
412
|
}
|
|
386
413
|
else {
|
|
387
414
|
serviceData = { model: '', modelName: '' };
|
|
388
|
-
this.debugLog(
|
|
415
|
+
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
389
416
|
}
|
|
390
417
|
};
|
|
391
418
|
// Wait 10 seconds
|
|
@@ -396,13 +423,13 @@ export class deviceBase {
|
|
|
396
423
|
}
|
|
397
424
|
async getCustomBLEAddress(switchbot) {
|
|
398
425
|
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
399
|
-
this.debugLog(
|
|
426
|
+
this.debugLog(`customBLEaddress: ${this.device.customBLEaddress}`);
|
|
400
427
|
(async () => {
|
|
401
428
|
// Start to monitor advertisement packets
|
|
402
429
|
await switchbot.startScan({ model: this.device.bleModel });
|
|
403
430
|
// Set an event handler
|
|
404
431
|
switchbot.onadvertisement = async (ad) => {
|
|
405
|
-
this.warnLog(
|
|
432
|
+
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
406
433
|
};
|
|
407
434
|
await sleep(10000);
|
|
408
435
|
// Stop to monitor
|
|
@@ -410,27 +437,6 @@ export class deviceBase {
|
|
|
410
437
|
})();
|
|
411
438
|
}
|
|
412
439
|
}
|
|
413
|
-
async successfulPushChange_BLE(Characteristic, CharacteristicValue, Connection) {
|
|
414
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} ${Characteristic}: ${CharacteristicValue}`
|
|
415
|
-
+ ` sent over ${Connection}, sent successfully`);
|
|
416
|
-
}
|
|
417
|
-
async successfulPushChange(statusCode, deviceStatus, bodyChange) {
|
|
418
|
-
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode} & deviceStatus`
|
|
419
|
-
+ ` StatusCode: ${deviceStatus.statusCode}`);
|
|
420
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} request to SwitchBot API,`
|
|
421
|
-
+ ` body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
422
|
-
}
|
|
423
|
-
async failedBLEChanges(e) {
|
|
424
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
425
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
426
|
-
}
|
|
427
|
-
async bodyChange(bodyChange, Connection) {
|
|
428
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to ${Connection}, body: ${bodyChange},`);
|
|
429
|
-
}
|
|
430
|
-
async pushChangeDisabled() {
|
|
431
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
432
|
-
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
433
|
-
}
|
|
434
440
|
async pushChangeRequest(bodyChange) {
|
|
435
441
|
return await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
436
442
|
body: bodyChange,
|
|
@@ -438,46 +444,15 @@ export class deviceBase {
|
|
|
438
444
|
headers: this.platform.generateHeaders(),
|
|
439
445
|
});
|
|
440
446
|
}
|
|
441
|
-
async pushChange(pushChange) {
|
|
442
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${pushChange}`);
|
|
443
|
-
}
|
|
444
|
-
async pushChangeError(Change, e) {
|
|
445
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed ${Change} with ${this.device.connectionType}`
|
|
446
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
447
|
-
}
|
|
448
|
-
async noChanges(Change, CharacteristicValue, CharacteristicValueCached) {
|
|
449
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, ${Change}: ${CharacteristicValue}, `
|
|
450
|
-
+ `${Change}Cached: ${CharacteristicValueCached}`);
|
|
451
|
-
}
|
|
452
|
-
async successfulRefreshStatus(statusCode, deviceStatus) {
|
|
453
|
-
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
454
|
-
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
455
|
-
}
|
|
456
|
-
async openAPIRefreshError(e) {
|
|
457
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
458
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
459
|
-
}
|
|
460
|
-
async statusCodes(statusCode, deviceStatus) {
|
|
461
|
-
this.statusCode(statusCode);
|
|
462
|
-
this.statusCode(deviceStatus.statusCode);
|
|
463
|
-
}
|
|
464
|
-
async refreshStatusCodes(statusCode, deviceStatus) {
|
|
465
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
466
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
467
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
468
|
-
}
|
|
469
|
-
async pushStatusCodes(statusCode, deviceStatus) {
|
|
470
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
471
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
472
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
473
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
474
|
-
}
|
|
475
447
|
async deviceRefreshStatus() {
|
|
476
448
|
return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
477
449
|
}
|
|
450
|
+
async successfulStatusCodes(statusCode, deviceStatus) {
|
|
451
|
+
return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100);
|
|
452
|
+
}
|
|
478
453
|
/**
|
|
479
454
|
* Update the characteristic value and log the change.
|
|
480
|
-
*
|
|
455
|
+
*
|
|
481
456
|
* @param Service: Service
|
|
482
457
|
* @param Characteristic: Characteristic
|
|
483
458
|
* @param CharacteristicValue: CharacteristicValue | undefined
|
|
@@ -488,7 +463,7 @@ export class deviceBase {
|
|
|
488
463
|
*/
|
|
489
464
|
async updateCharacteristic(Service, Characteristic, CharacteristicValue, CharacteristicName, history) {
|
|
490
465
|
if (CharacteristicValue === undefined) {
|
|
491
|
-
this.debugLog(`${
|
|
466
|
+
this.debugLog(`${CharacteristicName}: ${CharacteristicValue}`);
|
|
492
467
|
}
|
|
493
468
|
else {
|
|
494
469
|
await this.mqtt(CharacteristicName, CharacteristicValue);
|
|
@@ -496,11 +471,10 @@ export class deviceBase {
|
|
|
496
471
|
this.historyService?.addEntry(history);
|
|
497
472
|
}
|
|
498
473
|
Service.updateCharacteristic(Characteristic, CharacteristicValue);
|
|
499
|
-
this.debugLog(
|
|
500
|
-
|
|
501
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} context before: ${this.accessory.context[CharacteristicName]}`);
|
|
474
|
+
this.debugLog(`updateCharacteristic ${CharacteristicName}: ${CharacteristicValue}`);
|
|
475
|
+
this.debugWarnLog(`context before: ${this.accessory.context[CharacteristicName]}`);
|
|
502
476
|
this.accessory.context[CharacteristicName] = CharacteristicValue;
|
|
503
|
-
this.
|
|
477
|
+
this.debugWarnLog(`context after: ${this.accessory.context[CharacteristicName]}`);
|
|
504
478
|
}
|
|
505
479
|
}
|
|
506
480
|
async mqtt(CharacteristicName, CharacteristicValue) {
|
|
@@ -508,12 +482,6 @@ export class deviceBase {
|
|
|
508
482
|
this.mqttPublish(CharacteristicName, CharacteristicValue.toString());
|
|
509
483
|
}
|
|
510
484
|
}
|
|
511
|
-
async noChangeSet(value, CharacteristicName) {
|
|
512
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set ${CharacteristicName}: ${value}`);
|
|
513
|
-
}
|
|
514
|
-
async changeSet(value, CharacteristicName) {
|
|
515
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ${CharacteristicName}: ${value}`);
|
|
516
|
-
}
|
|
517
485
|
async getDeviceContext(accessory, device) {
|
|
518
486
|
// Set the accessory context
|
|
519
487
|
switch (device.deviceType) {
|
|
@@ -521,236 +489,241 @@ export class deviceBase {
|
|
|
521
489
|
device.model = SwitchBotModel.Humidifier;
|
|
522
490
|
device.bleModel = SwitchBotBLEModel.Humidifier;
|
|
523
491
|
device.bleModelName = SwitchBotBLEModelName.Humidifier;
|
|
524
|
-
|
|
492
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Humidifier;
|
|
525
493
|
break;
|
|
526
494
|
case 'Hub Mini':
|
|
527
495
|
device.model = SwitchBotModel.HubMini;
|
|
528
496
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
529
497
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
530
|
-
|
|
498
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
531
499
|
break;
|
|
532
500
|
case 'Hub Plus':
|
|
533
501
|
device.model = SwitchBotModel.HubPlus;
|
|
534
502
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
535
503
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
536
|
-
|
|
504
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
537
505
|
break;
|
|
538
506
|
case 'Hub 2':
|
|
539
507
|
device.model = SwitchBotModel.Hub2;
|
|
540
508
|
device.bleModel = SwitchBotBLEModel.Hub2;
|
|
541
509
|
device.bleModelName = SwitchBotBLEModelName.Hub2;
|
|
542
|
-
|
|
510
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Hub2;
|
|
543
511
|
break;
|
|
544
512
|
case 'Bot':
|
|
545
513
|
device.model = SwitchBotModel.Bot;
|
|
546
514
|
device.bleModel = SwitchBotBLEModel.Bot;
|
|
547
|
-
|
|
515
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Bot;
|
|
548
516
|
break;
|
|
549
517
|
case 'Meter':
|
|
550
518
|
device.model = SwitchBotModel.Meter;
|
|
551
519
|
device.bleModel = SwitchBotBLEModel.Meter;
|
|
552
520
|
device.bleModelName = SwitchBotBLEModelName.Meter;
|
|
553
|
-
|
|
521
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Meter;
|
|
554
522
|
break;
|
|
555
523
|
case 'MeterPlus':
|
|
556
524
|
device.model = SwitchBotModel.MeterPlusUS;
|
|
557
525
|
device.bleModel = SwitchBotBLEModel.MeterPlus;
|
|
558
526
|
device.bleModelName = SwitchBotBLEModelName.MeterPlus;
|
|
559
|
-
|
|
527
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MeterPlus;
|
|
560
528
|
break;
|
|
561
529
|
case 'Meter Plus (JP)':
|
|
562
530
|
device.model = SwitchBotModel.MeterPlusJP;
|
|
563
531
|
device.bleModel = SwitchBotBLEModel.MeterPlus;
|
|
564
532
|
device.bleModelName = SwitchBotBLEModelName.MeterPlus;
|
|
565
|
-
|
|
533
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MeterPlus;
|
|
566
534
|
break;
|
|
567
535
|
case 'WoIOSensor':
|
|
568
536
|
device.model = SwitchBotModel.OutdoorMeter;
|
|
569
537
|
device.bleModel = SwitchBotBLEModel.OutdoorMeter;
|
|
570
538
|
device.bleModelName = SwitchBotBLEModelName.OutdoorMeter;
|
|
571
|
-
|
|
539
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.OutdoorMeter;
|
|
572
540
|
break;
|
|
573
541
|
case 'Water Detector':
|
|
574
542
|
device.model = SwitchBotModel.WaterDetector;
|
|
575
543
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
576
544
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
577
|
-
|
|
545
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
578
546
|
break;
|
|
579
547
|
case 'Motion Sensor':
|
|
580
548
|
device.model = SwitchBotModel.MotionSensor;
|
|
581
549
|
device.bleModel = SwitchBotBLEModel.MotionSensor;
|
|
582
550
|
device.bleModelName = SwitchBotBLEModelName.MotionSensor;
|
|
583
|
-
|
|
551
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MotionSensor;
|
|
584
552
|
break;
|
|
585
553
|
case 'Contact Sensor':
|
|
586
554
|
device.model = SwitchBotModel.ContactSensor;
|
|
587
555
|
device.bleModel = SwitchBotBLEModel.ContactSensor;
|
|
588
556
|
device.bleModelName = SwitchBotBLEModelName.ContactSensor;
|
|
589
|
-
|
|
557
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.ContactSensor;
|
|
590
558
|
break;
|
|
591
559
|
case 'Curtain':
|
|
592
560
|
device.model = SwitchBotModel.Curtain;
|
|
593
561
|
device.bleModel = SwitchBotBLEModel.Curtain;
|
|
594
562
|
device.bleModelName = SwitchBotBLEModelName.Curtain;
|
|
595
|
-
|
|
563
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Curtain;
|
|
596
564
|
break;
|
|
597
565
|
case 'Curtain3':
|
|
598
566
|
device.model = SwitchBotModel.Curtain3;
|
|
599
567
|
device.bleModel = SwitchBotBLEModel.Curtain3;
|
|
600
568
|
device.bleModelName = SwitchBotBLEModelName.Curtain3;
|
|
601
|
-
|
|
569
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Curtain3;
|
|
602
570
|
break;
|
|
603
571
|
case 'Blind Tilt':
|
|
604
572
|
device.model = SwitchBotModel.BlindTilt;
|
|
605
573
|
device.bleModel = SwitchBotBLEModel.BlindTilt;
|
|
606
574
|
device.bleModelName = SwitchBotBLEModelName.BlindTilt;
|
|
607
|
-
|
|
575
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.BlindTilt;
|
|
608
576
|
break;
|
|
609
577
|
case 'Plug':
|
|
610
578
|
device.model = SwitchBotModel.Plug;
|
|
611
579
|
device.bleModel = SwitchBotBLEModel.PlugMiniUS;
|
|
612
580
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
613
|
-
|
|
581
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
614
582
|
break;
|
|
615
583
|
case 'Plug Mini (US)':
|
|
616
584
|
device.model = SwitchBotModel.PlugMiniUS;
|
|
617
585
|
device.bleModel = SwitchBotBLEModel.PlugMiniUS;
|
|
618
586
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
619
|
-
|
|
587
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
620
588
|
break;
|
|
621
589
|
case 'Plug Mini (JP)':
|
|
622
590
|
device.model = SwitchBotModel.PlugMiniJP;
|
|
623
591
|
device.bleModel = SwitchBotBLEModel.PlugMiniJP;
|
|
624
592
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
625
|
-
|
|
593
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
626
594
|
break;
|
|
627
595
|
case 'Smart Lock':
|
|
628
596
|
device.model = SwitchBotModel.Lock;
|
|
629
597
|
device.bleModel = SwitchBotBLEModel.Lock;
|
|
630
598
|
device.bleModelName = SwitchBotBLEModelName.Lock;
|
|
631
|
-
|
|
599
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Lock;
|
|
632
600
|
break;
|
|
633
601
|
case 'Smart Lock Pro':
|
|
634
602
|
device.model = SwitchBotModel.LockPro;
|
|
635
|
-
device.bleModel = SwitchBotBLEModel.
|
|
636
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
637
|
-
|
|
603
|
+
device.bleModel = SwitchBotBLEModel.LockPro;
|
|
604
|
+
device.bleModelName = SwitchBotBLEModelName.LockPro;
|
|
605
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.LockPro;
|
|
638
606
|
break;
|
|
639
607
|
case 'Color Bulb':
|
|
640
608
|
device.model = SwitchBotModel.ColorBulb;
|
|
641
609
|
device.bleModel = SwitchBotBLEModel.ColorBulb;
|
|
642
610
|
device.bleModelName = SwitchBotBLEModelName.ColorBulb;
|
|
643
|
-
|
|
611
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.ColorBulb;
|
|
644
612
|
break;
|
|
645
613
|
case 'K10+':
|
|
646
614
|
device.model = SwitchBotModel.K10;
|
|
647
615
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
648
616
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
649
|
-
|
|
617
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
650
618
|
break;
|
|
651
619
|
case 'WoSweeper':
|
|
652
620
|
device.model = SwitchBotModel.WoSweeper;
|
|
653
621
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
654
622
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
655
|
-
|
|
623
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
656
624
|
break;
|
|
657
625
|
case 'WoSweeperMini':
|
|
658
626
|
device.model = SwitchBotModel.WoSweeperMini;
|
|
659
627
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
660
628
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
661
|
-
|
|
629
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
662
630
|
break;
|
|
663
631
|
case 'Robot Vacuum Cleaner S1':
|
|
664
632
|
device.model = SwitchBotModel.RobotVacuumCleanerS1;
|
|
665
633
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
666
|
-
|
|
634
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
667
635
|
break;
|
|
668
636
|
case 'Robot Vacuum Cleaner S1 Plus':
|
|
669
637
|
device.model = SwitchBotModel.RobotVacuumCleanerS1Plus;
|
|
670
638
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
671
639
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
672
|
-
|
|
640
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
673
641
|
break;
|
|
674
642
|
case 'Robot Vacuum Cleaner S10':
|
|
675
643
|
device.model = SwitchBotModel.RobotVacuumCleanerS10;
|
|
676
644
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
677
645
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
678
|
-
|
|
646
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
679
647
|
break;
|
|
680
648
|
case 'Ceiling Light':
|
|
681
649
|
device.model = SwitchBotModel.CeilingLight;
|
|
682
650
|
device.bleModel = SwitchBotBLEModel.CeilingLight;
|
|
683
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
684
|
-
|
|
651
|
+
device.bleModelName = SwitchBotBLEModelName.CeilingLight;
|
|
652
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.CeilingLight;
|
|
685
653
|
break;
|
|
686
654
|
case 'Ceiling Light Pro':
|
|
687
655
|
device.model = SwitchBotModel.CeilingLightPro;
|
|
688
656
|
device.bleModel = SwitchBotBLEModel.CeilingLightPro;
|
|
689
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
690
|
-
|
|
657
|
+
device.bleModelName = SwitchBotBLEModelName.CeilingLightPro;
|
|
658
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.CeilingLightPro;
|
|
691
659
|
break;
|
|
692
660
|
case 'Strip Light':
|
|
693
661
|
device.model = SwitchBotModel.StripLight;
|
|
694
662
|
device.bleModel = SwitchBotBLEModel.StripLight;
|
|
695
663
|
device.bleModelName = SwitchBotBLEModelName.StripLight;
|
|
696
|
-
|
|
664
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.StripLight;
|
|
697
665
|
break;
|
|
698
666
|
case 'Indoor Cam':
|
|
699
667
|
device.model = SwitchBotModel.IndoorCam;
|
|
700
668
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
701
669
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
702
|
-
|
|
670
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
703
671
|
break;
|
|
704
672
|
case 'Remote':
|
|
705
673
|
device.model = SwitchBotModel.Remote;
|
|
706
674
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
707
675
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
708
|
-
|
|
676
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
709
677
|
break;
|
|
710
678
|
case 'remote with screen+':
|
|
711
679
|
device.model = SwitchBotModel.UniversalRemote;
|
|
712
680
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
713
681
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
714
|
-
|
|
682
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
715
683
|
break;
|
|
716
684
|
case 'Battery Circulator Fan':
|
|
717
685
|
device.model = SwitchBotModel.BatteryCirculatorFan;
|
|
718
686
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
719
687
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
720
|
-
|
|
688
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
721
689
|
break;
|
|
722
690
|
default:
|
|
723
691
|
device.model = SwitchBotModel.Unknown;
|
|
724
692
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
725
693
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
726
|
-
|
|
694
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
727
695
|
}
|
|
696
|
+
this.debugLog(`Model: ${device.model}, BLE Model: ${device.bleModel}, BLE Model Name: ${device.bleModelName}, `
|
|
697
|
+
+ `BLE Model Friendly Name: ${device.bleModelFriednlyName}`);
|
|
728
698
|
accessory.context.model = device.model;
|
|
699
|
+
accessory.context.bleModel = device.bleModel;
|
|
700
|
+
accessory.context.bleModelName = device.bleModelName;
|
|
701
|
+
accessory.context.bleModelFriednlyName = device.bleModelFriednlyName;
|
|
729
702
|
// Firmware Version
|
|
730
703
|
let deviceFirmwareVersion;
|
|
731
704
|
if (device.firmware) {
|
|
732
705
|
deviceFirmwareVersion = device.firmware;
|
|
733
|
-
this.debugSuccessLog(
|
|
706
|
+
this.debugSuccessLog(`1 FirmwareRevision: ${device.firmware}`);
|
|
734
707
|
}
|
|
735
708
|
else if (device.version) {
|
|
736
|
-
deviceFirmwareVersion = device.version;
|
|
737
|
-
this.debugSuccessLog(
|
|
709
|
+
deviceFirmwareVersion = JSON.stringify(device.version);
|
|
710
|
+
this.debugSuccessLog(`2 FirmwareRevision: ${device.version}`);
|
|
738
711
|
}
|
|
739
712
|
else if (accessory.context.deviceVersion) {
|
|
740
713
|
deviceFirmwareVersion = accessory.context.deviceVersion;
|
|
741
|
-
this.debugSuccessLog(
|
|
714
|
+
this.debugSuccessLog(`3 FirmwareRevision: ${accessory.context.deviceVersion}`);
|
|
742
715
|
}
|
|
743
716
|
else {
|
|
744
717
|
deviceFirmwareVersion = this.platform.version ?? '0.0.0';
|
|
745
718
|
if (this.platform.version) {
|
|
746
|
-
this.debugSuccessLog(
|
|
719
|
+
this.debugSuccessLog(`4 FirmwareRevision: ${this.platform.version}`);
|
|
747
720
|
}
|
|
748
721
|
else {
|
|
749
|
-
this.debugSuccessLog(
|
|
722
|
+
this.debugSuccessLog(`5 FirmwareRevision: ${deviceFirmwareVersion}`);
|
|
750
723
|
}
|
|
751
724
|
}
|
|
752
725
|
const version = deviceFirmwareVersion.toString();
|
|
753
|
-
this.debugLog(
|
|
726
|
+
this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
754
727
|
let deviceVersion;
|
|
755
728
|
if (version?.includes('.') === false) {
|
|
756
729
|
const replace = version?.replace(/^V|-.*$/g, '');
|
|
@@ -759,7 +732,7 @@ export class deviceBase {
|
|
|
759
732
|
deviceVersion = validVersion ?? '0.0.0';
|
|
760
733
|
}
|
|
761
734
|
else {
|
|
762
|
-
deviceVersion = version
|
|
735
|
+
deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
763
736
|
}
|
|
764
737
|
accessory
|
|
765
738
|
.getService(this.hap.Service.AccessoryInformation)
|
|
@@ -769,147 +742,135 @@ export class deviceBase {
|
|
|
769
742
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
770
743
|
.updateValue(deviceVersion);
|
|
771
744
|
accessory.context.deviceVersion = deviceVersion;
|
|
772
|
-
this.debugSuccessLog(
|
|
745
|
+
this.debugSuccessLog(`deviceVersion: ${accessory.context.deviceVersion}`);
|
|
773
746
|
}
|
|
774
747
|
async statusCode(statusCode) {
|
|
775
748
|
if (statusCode === 171) {
|
|
776
749
|
const previousStatusCode = statusCode;
|
|
777
750
|
if (this.device.hubDeviceId === this.device.deviceId) {
|
|
778
751
|
statusCode = 161;
|
|
779
|
-
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${previousStatusCode} is now statusCode: `
|
|
780
|
-
+ `${statusCode}, because the hubDeviceId: ${this.device.hubDeviceId} is set to the same as the deviceId: `
|
|
781
|
-
+ `${this.device.deviceId}, meaning the device is it's own hub.`);
|
|
782
752
|
}
|
|
783
753
|
if (this.device.hubDeviceId === '000000000000') {
|
|
784
754
|
statusCode = 161;
|
|
785
|
-
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${previousStatusCode} is now statusCode: `
|
|
786
|
-
+ `${statusCode}, because the hubDeviceId: ${this.device.hubDeviceId} is set to the same as the deviceId: `
|
|
787
|
-
+ `${this.device.deviceId}, meaning the device is it's own hub.`);
|
|
788
755
|
}
|
|
756
|
+
this.debugErrorLog(`statusCode: ${previousStatusCode} is now statusCode: ${statusCode}, because the hubDeviceId: ${this.device.hubDeviceId}`
|
|
757
|
+
+ ` is set to the same as the deviceId: ${this.device.deviceId}, meaning the device is it's own hub.`);
|
|
789
758
|
}
|
|
790
759
|
switch (statusCode) {
|
|
791
760
|
case 151:
|
|
792
|
-
this.errorLog(
|
|
761
|
+
this.errorLog(`Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
793
762
|
break;
|
|
794
763
|
case 152:
|
|
795
|
-
this.errorLog(
|
|
764
|
+
this.errorLog(`Device not found, statusCode: ${statusCode}`);
|
|
796
765
|
break;
|
|
797
766
|
case 160:
|
|
798
|
-
this.errorLog(
|
|
767
|
+
this.errorLog(`Command is not supported, statusCode: ${statusCode}`);
|
|
799
768
|
break;
|
|
800
769
|
case 161:
|
|
801
|
-
this.errorLog(
|
|
770
|
+
this.errorLog(`Device is offline, statusCode: ${statusCode}`);
|
|
802
771
|
break;
|
|
803
772
|
case 171:
|
|
804
|
-
this.errorLog(
|
|
805
|
-
+ `Hub: ${this.device.hubDeviceId}`);
|
|
773
|
+
this.errorLog(`Hub Device is offline, statusCode: ${statusCode}. Hub: ${this.device.hubDeviceId}`);
|
|
806
774
|
break;
|
|
807
775
|
case 190:
|
|
808
|
-
this.errorLog(
|
|
809
|
-
+ `
|
|
776
|
+
this.errorLog('Device internal error due to device states not synchronized with server, or command format is invalid,'
|
|
777
|
+
+ ` statusCode: ${statusCode}`);
|
|
810
778
|
break;
|
|
811
779
|
case 100:
|
|
812
|
-
this.debugLog(
|
|
780
|
+
this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
|
|
813
781
|
break;
|
|
814
782
|
case 200:
|
|
815
|
-
this.debugLog(
|
|
783
|
+
this.debugLog(`Request successful, statusCode: ${statusCode}`);
|
|
816
784
|
break;
|
|
817
785
|
case 400:
|
|
818
|
-
this.errorLog(
|
|
819
|
-
+ `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
|
|
786
|
+
this.errorLog(`Bad Request, an invalid payload request, statusCode: ${statusCode}`);
|
|
820
787
|
break;
|
|
821
788
|
case 401:
|
|
822
|
-
this.errorLog(
|
|
823
|
-
+ `but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
789
|
+
this.errorLog(`Unauthorized, Authorization for the API is required, but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
824
790
|
break;
|
|
825
791
|
case 403:
|
|
826
|
-
this.errorLog(
|
|
827
|
-
+ `
|
|
792
|
+
this.errorLog('Forbidden, The request has been authenticated but does not have appropriate permissions,'
|
|
793
|
+
+ ` or a requested resource is not found, statusCode: ${statusCode}`);
|
|
828
794
|
break;
|
|
829
795
|
case 404:
|
|
830
|
-
this.errorLog(
|
|
831
|
-
+ `statusCode: ${statusCode}`);
|
|
796
|
+
this.errorLog(`Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`);
|
|
832
797
|
break;
|
|
833
798
|
case 406:
|
|
834
|
-
this.errorLog(
|
|
835
|
-
+ `
|
|
799
|
+
this.errorLog('Not Acceptable, a MIME type has been requested via the Accept header for a value not supported by the server,'
|
|
800
|
+
+ ` statusCode: ${statusCode}`);
|
|
836
801
|
break;
|
|
837
802
|
case 415:
|
|
838
|
-
this.errorLog(
|
|
839
|
-
+ `header that is not supported by the server, statusCode: ${statusCode}`);
|
|
803
|
+
this.errorLog(`Unsupported Media Type, a contentType header has been defined that is not supported by the server, statusCode: ${statusCode}`);
|
|
840
804
|
break;
|
|
841
805
|
case 422:
|
|
842
|
-
this.errorLog(
|
|
843
|
-
+ `
|
|
806
|
+
this.errorLog('Unprocessable Entity, a valid request has been made, but the server cannot process it. '
|
|
807
|
+
+ `This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
|
|
844
808
|
break;
|
|
845
809
|
case 429:
|
|
846
|
-
this.errorLog(
|
|
847
|
-
+ `requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
810
|
+
this.errorLog(`Too Many Requests, exceeded the number of requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
848
811
|
break;
|
|
849
812
|
case 500:
|
|
850
|
-
this.errorLog(
|
|
851
|
-
+ `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
813
|
+
this.errorLog(`Internal Server Error, An unexpected error occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
852
814
|
break;
|
|
853
815
|
default:
|
|
854
|
-
this.infoLog(
|
|
855
|
-
+ `${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
|
|
816
|
+
this.infoLog(`Unknown statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
|
|
856
817
|
}
|
|
857
818
|
}
|
|
858
819
|
/**
|
|
859
820
|
* Logging for Device
|
|
860
821
|
*/
|
|
861
|
-
infoLog(...log) {
|
|
862
|
-
if (this.enablingDeviceLogging()) {
|
|
863
|
-
this.log.info(String(...log));
|
|
822
|
+
async infoLog(...log) {
|
|
823
|
+
if (await this.enablingDeviceLogging()) {
|
|
824
|
+
this.log.info(`${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
864
825
|
}
|
|
865
826
|
}
|
|
866
|
-
successLog(...log) {
|
|
867
|
-
if (this.enablingDeviceLogging()) {
|
|
868
|
-
this.log.success(String(...log));
|
|
827
|
+
async successLog(...log) {
|
|
828
|
+
if (await this.enablingDeviceLogging()) {
|
|
829
|
+
this.log.success(`${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
869
830
|
}
|
|
870
831
|
}
|
|
871
|
-
debugSuccessLog(...log) {
|
|
872
|
-
if (this.enablingDeviceLogging()) {
|
|
832
|
+
async debugSuccessLog(...log) {
|
|
833
|
+
if (await this.enablingDeviceLogging()) {
|
|
873
834
|
if (this.deviceLogging?.includes('debug')) {
|
|
874
|
-
this.log.success(
|
|
835
|
+
this.log.success(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
875
836
|
}
|
|
876
837
|
}
|
|
877
838
|
}
|
|
878
|
-
warnLog(...log) {
|
|
879
|
-
if (this.enablingDeviceLogging()) {
|
|
880
|
-
this.log.warn(String(...log));
|
|
839
|
+
async warnLog(...log) {
|
|
840
|
+
if (await this.enablingDeviceLogging()) {
|
|
841
|
+
this.log.warn(`${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
881
842
|
}
|
|
882
843
|
}
|
|
883
|
-
debugWarnLog(...log) {
|
|
884
|
-
if (this.enablingDeviceLogging()) {
|
|
844
|
+
async debugWarnLog(...log) {
|
|
845
|
+
if (await this.enablingDeviceLogging()) {
|
|
885
846
|
if (this.deviceLogging?.includes('debug')) {
|
|
886
|
-
this.log.warn(
|
|
847
|
+
this.log.warn(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
887
848
|
}
|
|
888
849
|
}
|
|
889
850
|
}
|
|
890
|
-
errorLog(...log) {
|
|
891
|
-
if (this.enablingDeviceLogging()) {
|
|
892
|
-
this.log.error(String(...log));
|
|
851
|
+
async errorLog(...log) {
|
|
852
|
+
if (await this.enablingDeviceLogging()) {
|
|
853
|
+
this.log.error(`${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
893
854
|
}
|
|
894
855
|
}
|
|
895
|
-
debugErrorLog(...log) {
|
|
896
|
-
if (this.enablingDeviceLogging()) {
|
|
856
|
+
async debugErrorLog(...log) {
|
|
857
|
+
if (await this.enablingDeviceLogging()) {
|
|
897
858
|
if (this.deviceLogging?.includes('debug')) {
|
|
898
|
-
this.log.error(
|
|
859
|
+
this.log.error(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
899
860
|
}
|
|
900
861
|
}
|
|
901
862
|
}
|
|
902
|
-
debugLog(...log) {
|
|
903
|
-
if (this.enablingDeviceLogging()) {
|
|
863
|
+
async debugLog(...log) {
|
|
864
|
+
if (await this.enablingDeviceLogging()) {
|
|
904
865
|
if (this.deviceLogging === 'debug') {
|
|
905
|
-
this.log.info(
|
|
866
|
+
this.log.info(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
906
867
|
}
|
|
907
868
|
else {
|
|
908
|
-
this.log.debug(String(...log));
|
|
869
|
+
this.log.debug(`${this.device.deviceType}: ${this.accessory.displayName} `, String(...log));
|
|
909
870
|
}
|
|
910
871
|
}
|
|
911
872
|
}
|
|
912
|
-
enablingDeviceLogging() {
|
|
873
|
+
async enablingDeviceLogging() {
|
|
913
874
|
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
914
875
|
}
|
|
915
876
|
}
|