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