@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10
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/README.md +16 -0
- package/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +224 -370
- 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 +197 -221
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +10 -5
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +214 -292
- 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 +335 -421
- 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 +121 -200
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +20 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +229 -403
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +41 -22
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +212 -181
- 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 +204 -249
- 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 +107 -233
- 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 +193 -235
- 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 +92 -159
- 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 +288 -370
- 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 +172 -219
- 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 +94 -160
- 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 +95 -157
- 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 +85 -144
- 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 +100 -127
- 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 +172 -259
- 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 +106 -131
- 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 +2 -3
- 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 +2 -3
- 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 +2 -3
- 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 +2 -3
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +3 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +5 -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 +2 -3
- 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 +2 -3
- 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 +2 -3
- 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 +2 -3
- 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 +2 -3
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -14
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +102 -86
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -190
- 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 +11 -11
- package/src/device/blindtilt.ts +254 -376
- package/src/device/bot.ts +209 -224
- package/src/device/ceilinglight.ts +227 -297
- package/src/device/colorbulb.ts +345 -435
- package/src/device/contact.ts +139 -200
- package/src/device/curtain.ts +254 -414
- package/src/device/device.ts +221 -190
- package/src/device/fan.ts +222 -253
- package/src/device/hub.ts +127 -248
- package/src/device/humidifier.ts +217 -246
- package/src/device/iosensor.ts +115 -168
- package/src/device/lightstrip.ts +303 -380
- package/src/device/lock.ts +188 -224
- package/src/device/meter.ts +117 -179
- package/src/device/meterplus.ts +118 -176
- package/src/device/motion.ts +100 -146
- package/src/device/plug.ts +111 -131
- package/src/device/robotvacuumcleaner.ts +191 -258
- package/src/device/waterdetector.ts +124 -141
- package/src/irdevice/airconditioner.ts +4 -4
- package/src/irdevice/airpurifier.ts +4 -4
- package/src/irdevice/camera.ts +4 -4
- package/src/irdevice/fan.ts +4 -4
- package/src/irdevice/irdevice.ts +8 -2
- package/src/irdevice/light.ts +4 -4
- package/src/irdevice/other.ts +4 -4
- package/src/irdevice/tv.ts +4 -4
- package/src/irdevice/vacuumcleaner.ts +4 -4
- package/src/irdevice/waterheater.ts +4 -4
- package/src/platform.ts +104 -87
- package/src/settings.ts +30 -238
- 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,23 +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) {
|
|
401
|
+
this.debugLog(`Scanning for ${this.device.bleModelName} devices...`);
|
|
374
402
|
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
|
|
375
403
|
// Set an event handler
|
|
376
404
|
let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName };
|
|
377
405
|
switchbot.onadvertisement = async (ad) => {
|
|
378
|
-
this.warnLog(
|
|
406
|
+
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
379
407
|
if (this.device.bleMac === ad.address && ad.serviceData.model === this.device.bleModel) {
|
|
380
|
-
this.debugLog(`${
|
|
381
|
-
this.debugLog(
|
|
382
|
-
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)}`);
|
|
383
411
|
serviceData = ad.serviceData;
|
|
384
412
|
}
|
|
385
413
|
else {
|
|
386
414
|
serviceData = { model: '', modelName: '' };
|
|
387
|
-
this.debugLog(
|
|
415
|
+
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
388
416
|
}
|
|
389
417
|
};
|
|
390
418
|
// Wait 10 seconds
|
|
@@ -395,13 +423,13 @@ export class deviceBase {
|
|
|
395
423
|
}
|
|
396
424
|
async getCustomBLEAddress(switchbot) {
|
|
397
425
|
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
398
|
-
this.debugLog(
|
|
426
|
+
this.debugLog(`customBLEaddress: ${this.device.customBLEaddress}`);
|
|
399
427
|
(async () => {
|
|
400
428
|
// Start to monitor advertisement packets
|
|
401
429
|
await switchbot.startScan({ model: this.device.bleModel });
|
|
402
430
|
// Set an event handler
|
|
403
431
|
switchbot.onadvertisement = async (ad) => {
|
|
404
|
-
this.warnLog(
|
|
432
|
+
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
405
433
|
};
|
|
406
434
|
await sleep(10000);
|
|
407
435
|
// Stop to monitor
|
|
@@ -416,33 +444,43 @@ export class deviceBase {
|
|
|
416
444
|
headers: this.platform.generateHeaders(),
|
|
417
445
|
});
|
|
418
446
|
}
|
|
419
|
-
async
|
|
420
|
-
this.
|
|
421
|
-
+ ` StatusCode: ${deviceStatus.statusCode}`);
|
|
422
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} request to SwitchBot API,`
|
|
423
|
-
+ ` body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
424
|
-
}
|
|
425
|
-
async successfulRefreshStatus(statusCode, deviceStatus) {
|
|
426
|
-
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
427
|
-
+ `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
|
|
428
|
-
}
|
|
429
|
-
async statusCodes(statusCode, deviceStatus) {
|
|
430
|
-
this.statusCode(statusCode);
|
|
431
|
-
this.statusCode(deviceStatus.statusCode);
|
|
447
|
+
async deviceRefreshStatus() {
|
|
448
|
+
return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
432
449
|
}
|
|
433
|
-
async
|
|
434
|
-
|
|
435
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
436
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
450
|
+
async successfulStatusCodes(statusCode, deviceStatus) {
|
|
451
|
+
return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100);
|
|
437
452
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
453
|
+
/**
|
|
454
|
+
* Update the characteristic value and log the change.
|
|
455
|
+
*
|
|
456
|
+
* @param Service: Service
|
|
457
|
+
* @param Characteristic: Characteristic
|
|
458
|
+
* @param CharacteristicValue: CharacteristicValue | undefined
|
|
459
|
+
* @param CharacteristicName: string
|
|
460
|
+
* @param history: object
|
|
461
|
+
* @return: void
|
|
462
|
+
*
|
|
463
|
+
*/
|
|
464
|
+
async updateCharacteristic(Service, Characteristic, CharacteristicValue, CharacteristicName, history) {
|
|
465
|
+
if (CharacteristicValue === undefined) {
|
|
466
|
+
this.debugLog(`${CharacteristicName}: ${CharacteristicValue}`);
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
await this.mqtt(CharacteristicName, CharacteristicValue);
|
|
470
|
+
if (this.device.history) {
|
|
471
|
+
this.historyService?.addEntry(history);
|
|
472
|
+
}
|
|
473
|
+
Service.updateCharacteristic(Characteristic, CharacteristicValue);
|
|
474
|
+
this.debugLog(`updateCharacteristic ${CharacteristicName}: ${CharacteristicValue}`);
|
|
475
|
+
this.debugWarnLog(`context before: ${this.accessory.context[CharacteristicName]}`);
|
|
476
|
+
this.accessory.context[CharacteristicName] = CharacteristicValue;
|
|
477
|
+
this.debugWarnLog(`context after: ${this.accessory.context[CharacteristicName]}`);
|
|
478
|
+
}
|
|
443
479
|
}
|
|
444
|
-
async
|
|
445
|
-
|
|
480
|
+
async mqtt(CharacteristicName, CharacteristicValue) {
|
|
481
|
+
if (this.device.mqttURL) {
|
|
482
|
+
this.mqttPublish(CharacteristicName, CharacteristicValue.toString());
|
|
483
|
+
}
|
|
446
484
|
}
|
|
447
485
|
async getDeviceContext(accessory, device) {
|
|
448
486
|
// Set the accessory context
|
|
@@ -451,236 +489,241 @@ export class deviceBase {
|
|
|
451
489
|
device.model = SwitchBotModel.Humidifier;
|
|
452
490
|
device.bleModel = SwitchBotBLEModel.Humidifier;
|
|
453
491
|
device.bleModelName = SwitchBotBLEModelName.Humidifier;
|
|
454
|
-
|
|
492
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Humidifier;
|
|
455
493
|
break;
|
|
456
494
|
case 'Hub Mini':
|
|
457
495
|
device.model = SwitchBotModel.HubMini;
|
|
458
496
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
459
497
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
460
|
-
|
|
498
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
461
499
|
break;
|
|
462
500
|
case 'Hub Plus':
|
|
463
501
|
device.model = SwitchBotModel.HubPlus;
|
|
464
502
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
465
503
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
466
|
-
|
|
504
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
467
505
|
break;
|
|
468
506
|
case 'Hub 2':
|
|
469
507
|
device.model = SwitchBotModel.Hub2;
|
|
470
508
|
device.bleModel = SwitchBotBLEModel.Hub2;
|
|
471
509
|
device.bleModelName = SwitchBotBLEModelName.Hub2;
|
|
472
|
-
|
|
510
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Hub2;
|
|
473
511
|
break;
|
|
474
512
|
case 'Bot':
|
|
475
513
|
device.model = SwitchBotModel.Bot;
|
|
476
514
|
device.bleModel = SwitchBotBLEModel.Bot;
|
|
477
|
-
|
|
515
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Bot;
|
|
478
516
|
break;
|
|
479
517
|
case 'Meter':
|
|
480
518
|
device.model = SwitchBotModel.Meter;
|
|
481
519
|
device.bleModel = SwitchBotBLEModel.Meter;
|
|
482
520
|
device.bleModelName = SwitchBotBLEModelName.Meter;
|
|
483
|
-
|
|
521
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Meter;
|
|
484
522
|
break;
|
|
485
523
|
case 'MeterPlus':
|
|
486
524
|
device.model = SwitchBotModel.MeterPlusUS;
|
|
487
525
|
device.bleModel = SwitchBotBLEModel.MeterPlus;
|
|
488
526
|
device.bleModelName = SwitchBotBLEModelName.MeterPlus;
|
|
489
|
-
|
|
527
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MeterPlus;
|
|
490
528
|
break;
|
|
491
529
|
case 'Meter Plus (JP)':
|
|
492
530
|
device.model = SwitchBotModel.MeterPlusJP;
|
|
493
531
|
device.bleModel = SwitchBotBLEModel.MeterPlus;
|
|
494
532
|
device.bleModelName = SwitchBotBLEModelName.MeterPlus;
|
|
495
|
-
|
|
533
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MeterPlus;
|
|
496
534
|
break;
|
|
497
535
|
case 'WoIOSensor':
|
|
498
536
|
device.model = SwitchBotModel.OutdoorMeter;
|
|
499
537
|
device.bleModel = SwitchBotBLEModel.OutdoorMeter;
|
|
500
538
|
device.bleModelName = SwitchBotBLEModelName.OutdoorMeter;
|
|
501
|
-
|
|
539
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.OutdoorMeter;
|
|
502
540
|
break;
|
|
503
541
|
case 'Water Detector':
|
|
504
542
|
device.model = SwitchBotModel.WaterDetector;
|
|
505
543
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
506
544
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
507
|
-
|
|
545
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
508
546
|
break;
|
|
509
547
|
case 'Motion Sensor':
|
|
510
548
|
device.model = SwitchBotModel.MotionSensor;
|
|
511
549
|
device.bleModel = SwitchBotBLEModel.MotionSensor;
|
|
512
550
|
device.bleModelName = SwitchBotBLEModelName.MotionSensor;
|
|
513
|
-
|
|
551
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.MotionSensor;
|
|
514
552
|
break;
|
|
515
553
|
case 'Contact Sensor':
|
|
516
554
|
device.model = SwitchBotModel.ContactSensor;
|
|
517
555
|
device.bleModel = SwitchBotBLEModel.ContactSensor;
|
|
518
556
|
device.bleModelName = SwitchBotBLEModelName.ContactSensor;
|
|
519
|
-
|
|
557
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.ContactSensor;
|
|
520
558
|
break;
|
|
521
559
|
case 'Curtain':
|
|
522
560
|
device.model = SwitchBotModel.Curtain;
|
|
523
561
|
device.bleModel = SwitchBotBLEModel.Curtain;
|
|
524
562
|
device.bleModelName = SwitchBotBLEModelName.Curtain;
|
|
525
|
-
|
|
563
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Curtain;
|
|
526
564
|
break;
|
|
527
565
|
case 'Curtain3':
|
|
528
566
|
device.model = SwitchBotModel.Curtain3;
|
|
529
567
|
device.bleModel = SwitchBotBLEModel.Curtain3;
|
|
530
568
|
device.bleModelName = SwitchBotBLEModelName.Curtain3;
|
|
531
|
-
|
|
569
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Curtain3;
|
|
532
570
|
break;
|
|
533
571
|
case 'Blind Tilt':
|
|
534
572
|
device.model = SwitchBotModel.BlindTilt;
|
|
535
573
|
device.bleModel = SwitchBotBLEModel.BlindTilt;
|
|
536
574
|
device.bleModelName = SwitchBotBLEModelName.BlindTilt;
|
|
537
|
-
|
|
575
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.BlindTilt;
|
|
538
576
|
break;
|
|
539
577
|
case 'Plug':
|
|
540
578
|
device.model = SwitchBotModel.Plug;
|
|
541
579
|
device.bleModel = SwitchBotBLEModel.PlugMiniUS;
|
|
542
580
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
543
|
-
|
|
581
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
544
582
|
break;
|
|
545
583
|
case 'Plug Mini (US)':
|
|
546
584
|
device.model = SwitchBotModel.PlugMiniUS;
|
|
547
585
|
device.bleModel = SwitchBotBLEModel.PlugMiniUS;
|
|
548
586
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
549
|
-
|
|
587
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
550
588
|
break;
|
|
551
589
|
case 'Plug Mini (JP)':
|
|
552
590
|
device.model = SwitchBotModel.PlugMiniJP;
|
|
553
591
|
device.bleModel = SwitchBotBLEModel.PlugMiniJP;
|
|
554
592
|
device.bleModelName = SwitchBotBLEModelName.PlugMini;
|
|
555
|
-
|
|
593
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.PlugMini;
|
|
556
594
|
break;
|
|
557
595
|
case 'Smart Lock':
|
|
558
596
|
device.model = SwitchBotModel.Lock;
|
|
559
597
|
device.bleModel = SwitchBotBLEModel.Lock;
|
|
560
598
|
device.bleModelName = SwitchBotBLEModelName.Lock;
|
|
561
|
-
|
|
599
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Lock;
|
|
562
600
|
break;
|
|
563
601
|
case 'Smart Lock Pro':
|
|
564
602
|
device.model = SwitchBotModel.LockPro;
|
|
565
|
-
device.bleModel = SwitchBotBLEModel.
|
|
566
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
567
|
-
|
|
603
|
+
device.bleModel = SwitchBotBLEModel.LockPro;
|
|
604
|
+
device.bleModelName = SwitchBotBLEModelName.LockPro;
|
|
605
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.LockPro;
|
|
568
606
|
break;
|
|
569
607
|
case 'Color Bulb':
|
|
570
608
|
device.model = SwitchBotModel.ColorBulb;
|
|
571
609
|
device.bleModel = SwitchBotBLEModel.ColorBulb;
|
|
572
610
|
device.bleModelName = SwitchBotBLEModelName.ColorBulb;
|
|
573
|
-
|
|
611
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.ColorBulb;
|
|
574
612
|
break;
|
|
575
613
|
case 'K10+':
|
|
576
614
|
device.model = SwitchBotModel.K10;
|
|
577
615
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
578
616
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
579
|
-
|
|
617
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
580
618
|
break;
|
|
581
619
|
case 'WoSweeper':
|
|
582
620
|
device.model = SwitchBotModel.WoSweeper;
|
|
583
621
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
584
622
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
585
|
-
|
|
623
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
586
624
|
break;
|
|
587
625
|
case 'WoSweeperMini':
|
|
588
626
|
device.model = SwitchBotModel.WoSweeperMini;
|
|
589
627
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
590
628
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
591
|
-
|
|
629
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
592
630
|
break;
|
|
593
631
|
case 'Robot Vacuum Cleaner S1':
|
|
594
632
|
device.model = SwitchBotModel.RobotVacuumCleanerS1;
|
|
595
633
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
596
|
-
|
|
634
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
597
635
|
break;
|
|
598
636
|
case 'Robot Vacuum Cleaner S1 Plus':
|
|
599
637
|
device.model = SwitchBotModel.RobotVacuumCleanerS1Plus;
|
|
600
638
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
601
639
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
602
|
-
|
|
640
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
603
641
|
break;
|
|
604
642
|
case 'Robot Vacuum Cleaner S10':
|
|
605
643
|
device.model = SwitchBotModel.RobotVacuumCleanerS10;
|
|
606
644
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
607
645
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
608
|
-
|
|
646
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
609
647
|
break;
|
|
610
648
|
case 'Ceiling Light':
|
|
611
649
|
device.model = SwitchBotModel.CeilingLight;
|
|
612
650
|
device.bleModel = SwitchBotBLEModel.CeilingLight;
|
|
613
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
614
|
-
|
|
651
|
+
device.bleModelName = SwitchBotBLEModelName.CeilingLight;
|
|
652
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.CeilingLight;
|
|
615
653
|
break;
|
|
616
654
|
case 'Ceiling Light Pro':
|
|
617
655
|
device.model = SwitchBotModel.CeilingLightPro;
|
|
618
656
|
device.bleModel = SwitchBotBLEModel.CeilingLightPro;
|
|
619
|
-
device.bleModelName = SwitchBotBLEModelName.
|
|
620
|
-
|
|
657
|
+
device.bleModelName = SwitchBotBLEModelName.CeilingLightPro;
|
|
658
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.CeilingLightPro;
|
|
621
659
|
break;
|
|
622
660
|
case 'Strip Light':
|
|
623
661
|
device.model = SwitchBotModel.StripLight;
|
|
624
662
|
device.bleModel = SwitchBotBLEModel.StripLight;
|
|
625
663
|
device.bleModelName = SwitchBotBLEModelName.StripLight;
|
|
626
|
-
|
|
664
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.StripLight;
|
|
627
665
|
break;
|
|
628
666
|
case 'Indoor Cam':
|
|
629
667
|
device.model = SwitchBotModel.IndoorCam;
|
|
630
668
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
631
669
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
632
|
-
|
|
670
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
633
671
|
break;
|
|
634
672
|
case 'Remote':
|
|
635
673
|
device.model = SwitchBotModel.Remote;
|
|
636
674
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
637
675
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
638
|
-
|
|
676
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
639
677
|
break;
|
|
640
678
|
case 'remote with screen+':
|
|
641
679
|
device.model = SwitchBotModel.UniversalRemote;
|
|
642
680
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
643
681
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
644
|
-
|
|
682
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
645
683
|
break;
|
|
646
684
|
case 'Battery Circulator Fan':
|
|
647
685
|
device.model = SwitchBotModel.BatteryCirculatorFan;
|
|
648
686
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
649
687
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
650
|
-
|
|
688
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
651
689
|
break;
|
|
652
690
|
default:
|
|
653
691
|
device.model = SwitchBotModel.Unknown;
|
|
654
692
|
device.bleModel = SwitchBotBLEModel.Unknown;
|
|
655
693
|
device.bleModelName = SwitchBotBLEModelName.Unknown;
|
|
656
|
-
|
|
694
|
+
device.bleModelFriednlyName = SwitchBotBLEModelFriendlyName.Unknown;
|
|
657
695
|
}
|
|
696
|
+
this.debugLog(`Model: ${device.model}, BLE Model: ${device.bleModel}, BLE Model Name: ${device.bleModelName}, `
|
|
697
|
+
+ `BLE Model Friendly Name: ${device.bleModelFriednlyName}`);
|
|
658
698
|
accessory.context.model = device.model;
|
|
699
|
+
accessory.context.bleModel = device.bleModel;
|
|
700
|
+
accessory.context.bleModelName = device.bleModelName;
|
|
701
|
+
accessory.context.bleModelFriednlyName = device.bleModelFriednlyName;
|
|
659
702
|
// Firmware Version
|
|
660
703
|
let deviceFirmwareVersion;
|
|
661
704
|
if (device.firmware) {
|
|
662
705
|
deviceFirmwareVersion = device.firmware;
|
|
663
|
-
this.debugSuccessLog(
|
|
706
|
+
this.debugSuccessLog(`1 FirmwareRevision: ${device.firmware}`);
|
|
664
707
|
}
|
|
665
708
|
else if (device.version) {
|
|
666
|
-
deviceFirmwareVersion = device.version;
|
|
667
|
-
this.debugSuccessLog(
|
|
709
|
+
deviceFirmwareVersion = JSON.stringify(device.version);
|
|
710
|
+
this.debugSuccessLog(`2 FirmwareRevision: ${device.version}`);
|
|
668
711
|
}
|
|
669
712
|
else if (accessory.context.deviceVersion) {
|
|
670
713
|
deviceFirmwareVersion = accessory.context.deviceVersion;
|
|
671
|
-
this.debugSuccessLog(
|
|
714
|
+
this.debugSuccessLog(`3 FirmwareRevision: ${accessory.context.deviceVersion}`);
|
|
672
715
|
}
|
|
673
716
|
else {
|
|
674
717
|
deviceFirmwareVersion = this.platform.version ?? '0.0.0';
|
|
675
718
|
if (this.platform.version) {
|
|
676
|
-
this.debugSuccessLog(
|
|
719
|
+
this.debugSuccessLog(`4 FirmwareRevision: ${this.platform.version}`);
|
|
677
720
|
}
|
|
678
721
|
else {
|
|
679
|
-
this.debugSuccessLog(
|
|
722
|
+
this.debugSuccessLog(`5 FirmwareRevision: ${deviceFirmwareVersion}`);
|
|
680
723
|
}
|
|
681
724
|
}
|
|
682
725
|
const version = deviceFirmwareVersion.toString();
|
|
683
|
-
this.debugLog(
|
|
726
|
+
this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
684
727
|
let deviceVersion;
|
|
685
728
|
if (version?.includes('.') === false) {
|
|
686
729
|
const replace = version?.replace(/^V|-.*$/g, '');
|
|
@@ -689,7 +732,7 @@ export class deviceBase {
|
|
|
689
732
|
deviceVersion = validVersion ?? '0.0.0';
|
|
690
733
|
}
|
|
691
734
|
else {
|
|
692
|
-
deviceVersion = version
|
|
735
|
+
deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
693
736
|
}
|
|
694
737
|
accessory
|
|
695
738
|
.getService(this.hap.Service.AccessoryInformation)
|
|
@@ -699,147 +742,135 @@ export class deviceBase {
|
|
|
699
742
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
700
743
|
.updateValue(deviceVersion);
|
|
701
744
|
accessory.context.deviceVersion = deviceVersion;
|
|
702
|
-
this.debugSuccessLog(
|
|
745
|
+
this.debugSuccessLog(`deviceVersion: ${accessory.context.deviceVersion}`);
|
|
703
746
|
}
|
|
704
747
|
async statusCode(statusCode) {
|
|
705
748
|
if (statusCode === 171) {
|
|
706
749
|
const previousStatusCode = statusCode;
|
|
707
750
|
if (this.device.hubDeviceId === this.device.deviceId) {
|
|
708
751
|
statusCode = 161;
|
|
709
|
-
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${previousStatusCode} is now statusCode: `
|
|
710
|
-
+ `${statusCode}, because the hubDeviceId: ${this.device.hubDeviceId} is set to the same as the deviceId: `
|
|
711
|
-
+ `${this.device.deviceId}, meaning the device is it's own hub.`);
|
|
712
752
|
}
|
|
713
753
|
if (this.device.hubDeviceId === '000000000000') {
|
|
714
754
|
statusCode = 161;
|
|
715
|
-
this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${previousStatusCode} is now statusCode: `
|
|
716
|
-
+ `${statusCode}, because the hubDeviceId: ${this.device.hubDeviceId} is set to the same as the deviceId: `
|
|
717
|
-
+ `${this.device.deviceId}, meaning the device is it's own hub.`);
|
|
718
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.`);
|
|
719
758
|
}
|
|
720
759
|
switch (statusCode) {
|
|
721
760
|
case 151:
|
|
722
|
-
this.errorLog(
|
|
761
|
+
this.errorLog(`Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
723
762
|
break;
|
|
724
763
|
case 152:
|
|
725
|
-
this.errorLog(
|
|
764
|
+
this.errorLog(`Device not found, statusCode: ${statusCode}`);
|
|
726
765
|
break;
|
|
727
766
|
case 160:
|
|
728
|
-
this.errorLog(
|
|
767
|
+
this.errorLog(`Command is not supported, statusCode: ${statusCode}`);
|
|
729
768
|
break;
|
|
730
769
|
case 161:
|
|
731
|
-
this.errorLog(
|
|
770
|
+
this.errorLog(`Device is offline, statusCode: ${statusCode}`);
|
|
732
771
|
break;
|
|
733
772
|
case 171:
|
|
734
|
-
this.errorLog(
|
|
735
|
-
+ `Hub: ${this.device.hubDeviceId}`);
|
|
773
|
+
this.errorLog(`Hub Device is offline, statusCode: ${statusCode}. Hub: ${this.device.hubDeviceId}`);
|
|
736
774
|
break;
|
|
737
775
|
case 190:
|
|
738
|
-
this.errorLog(
|
|
739
|
-
+ `
|
|
776
|
+
this.errorLog('Device internal error due to device states not synchronized with server, or command format is invalid,'
|
|
777
|
+
+ ` statusCode: ${statusCode}`);
|
|
740
778
|
break;
|
|
741
779
|
case 100:
|
|
742
|
-
this.debugLog(
|
|
780
|
+
this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
|
|
743
781
|
break;
|
|
744
782
|
case 200:
|
|
745
|
-
this.debugLog(
|
|
783
|
+
this.debugLog(`Request successful, statusCode: ${statusCode}`);
|
|
746
784
|
break;
|
|
747
785
|
case 400:
|
|
748
|
-
this.errorLog(
|
|
749
|
-
+ `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}`);
|
|
750
787
|
break;
|
|
751
788
|
case 401:
|
|
752
|
-
this.errorLog(
|
|
753
|
-
+ `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}`);
|
|
754
790
|
break;
|
|
755
791
|
case 403:
|
|
756
|
-
this.errorLog(
|
|
757
|
-
+ `
|
|
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}`);
|
|
758
794
|
break;
|
|
759
795
|
case 404:
|
|
760
|
-
this.errorLog(
|
|
761
|
-
+ `statusCode: ${statusCode}`);
|
|
796
|
+
this.errorLog(`Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`);
|
|
762
797
|
break;
|
|
763
798
|
case 406:
|
|
764
|
-
this.errorLog(
|
|
765
|
-
+ `
|
|
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}`);
|
|
766
801
|
break;
|
|
767
802
|
case 415:
|
|
768
|
-
this.errorLog(
|
|
769
|
-
+ `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}`);
|
|
770
804
|
break;
|
|
771
805
|
case 422:
|
|
772
|
-
this.errorLog(
|
|
773
|
-
+ `
|
|
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}`);
|
|
774
808
|
break;
|
|
775
809
|
case 429:
|
|
776
|
-
this.errorLog(
|
|
777
|
-
+ `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}`);
|
|
778
811
|
break;
|
|
779
812
|
case 500:
|
|
780
|
-
this.errorLog(
|
|
781
|
-
+ `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}`);
|
|
782
814
|
break;
|
|
783
815
|
default:
|
|
784
|
-
this.infoLog(
|
|
785
|
-
+ `${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
|
|
816
|
+
this.infoLog(`Unknown statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
|
|
786
817
|
}
|
|
787
818
|
}
|
|
788
819
|
/**
|
|
789
820
|
* Logging for Device
|
|
790
821
|
*/
|
|
791
|
-
infoLog(...log) {
|
|
792
|
-
if (this.enablingDeviceLogging()) {
|
|
793
|
-
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));
|
|
794
825
|
}
|
|
795
826
|
}
|
|
796
|
-
successLog(...log) {
|
|
797
|
-
if (this.enablingDeviceLogging()) {
|
|
798
|
-
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));
|
|
799
830
|
}
|
|
800
831
|
}
|
|
801
|
-
debugSuccessLog(...log) {
|
|
802
|
-
if (this.enablingDeviceLogging()) {
|
|
832
|
+
async debugSuccessLog(...log) {
|
|
833
|
+
if (await this.enablingDeviceLogging()) {
|
|
803
834
|
if (this.deviceLogging?.includes('debug')) {
|
|
804
|
-
this.log.success(
|
|
835
|
+
this.log.success(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log));
|
|
805
836
|
}
|
|
806
837
|
}
|
|
807
838
|
}
|
|
808
|
-
warnLog(...log) {
|
|
809
|
-
if (this.enablingDeviceLogging()) {
|
|
810
|
-
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));
|
|
811
842
|
}
|
|
812
843
|
}
|
|
813
|
-
debugWarnLog(...log) {
|
|
814
|
-
if (this.enablingDeviceLogging()) {
|
|
844
|
+
async debugWarnLog(...log) {
|
|
845
|
+
if (await this.enablingDeviceLogging()) {
|
|
815
846
|
if (this.deviceLogging?.includes('debug')) {
|
|
816
|
-
this.log.warn(
|
|
847
|
+
this.log.warn(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log));
|
|
817
848
|
}
|
|
818
849
|
}
|
|
819
850
|
}
|
|
820
|
-
errorLog(...log) {
|
|
821
|
-
if (this.enablingDeviceLogging()) {
|
|
822
|
-
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));
|
|
823
854
|
}
|
|
824
855
|
}
|
|
825
|
-
debugErrorLog(...log) {
|
|
826
|
-
if (this.enablingDeviceLogging()) {
|
|
856
|
+
async debugErrorLog(...log) {
|
|
857
|
+
if (await this.enablingDeviceLogging()) {
|
|
827
858
|
if (this.deviceLogging?.includes('debug')) {
|
|
828
|
-
this.log.error(
|
|
859
|
+
this.log.error(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log));
|
|
829
860
|
}
|
|
830
861
|
}
|
|
831
862
|
}
|
|
832
|
-
debugLog(...log) {
|
|
833
|
-
if (this.enablingDeviceLogging()) {
|
|
863
|
+
async debugLog(...log) {
|
|
864
|
+
if (await this.enablingDeviceLogging()) {
|
|
834
865
|
if (this.deviceLogging === 'debug') {
|
|
835
|
-
this.log.info(
|
|
866
|
+
this.log.info(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log));
|
|
836
867
|
}
|
|
837
868
|
else {
|
|
838
|
-
this.log.debug(String(...log));
|
|
869
|
+
this.log.debug(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log));
|
|
839
870
|
}
|
|
840
871
|
}
|
|
841
872
|
}
|
|
842
|
-
enablingDeviceLogging() {
|
|
873
|
+
async enablingDeviceLogging() {
|
|
843
874
|
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
844
875
|
}
|
|
845
876
|
}
|