@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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.
Files changed (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -0,0 +1,655 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * device.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { API, HAP, Logging, PlatformAccessory } from 'homebridge';
6
+
7
+ import { hostname } from 'os';
8
+ import { MqttClient } from 'mqtt';
9
+ import asyncmqtt from 'async-mqtt';
10
+ import { SwitchBotPlatform } from '../platform.js';
11
+ import { SwitchBotPlatformConfig, device, devicesConfig } from '../settings.js';
12
+ import { BlindTiltMappingMode, SwitchBotModel, SwitchBotBLEModel, sleep } from '../utils.js';
13
+
14
+ export abstract class deviceBase {
15
+ public readonly api: API;
16
+ public readonly log: Logging;
17
+ public readonly config!: SwitchBotPlatformConfig;
18
+ protected readonly hap: HAP;
19
+
20
+ // Config
21
+ protected deviceLogging!: string;
22
+ protected deviceUpdateRate!: number;
23
+ protected deviceRefreshRate!: number;
24
+ protected deviceMaxRetries!: number;
25
+ protected deviceDelayBetweenRetries!: number;
26
+
27
+ // Connection
28
+ protected readonly BLE: boolean;
29
+ protected readonly OpenAPI: boolean;
30
+
31
+ // Accsrroy Information
32
+ protected deviceModel!: SwitchBotModel;
33
+ protected deviceBLEModel!: SwitchBotBLEModel;
34
+
35
+ // BLE
36
+ protected scanDuration!: number;
37
+
38
+ // EVE history service handler
39
+ protected historyService?: any = null;
40
+
41
+ //MQTT stuff
42
+ protected mqttClient: MqttClient | null = null;
43
+
44
+ constructor(
45
+ protected readonly platform: SwitchBotPlatform,
46
+ protected accessory: PlatformAccessory,
47
+ protected device: device & devicesConfig,
48
+ ) {
49
+ this.api = this.platform.api;
50
+ this.log = this.platform.log;
51
+ this.config = this.platform.config;
52
+ this.hap = this.api.hap;
53
+
54
+ // Connection
55
+ this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
56
+ this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
57
+
58
+ this.getDeviceLogSettings(device);
59
+ this.getDeviceRefreshRateSettings(device);
60
+ this.getDeviceRetry(device);
61
+ this.getDeviceConfigSettings(device);
62
+ this.getDeviceContext(accessory, device);
63
+ this.setupMqtt(device);
64
+ this.scan(device);
65
+
66
+ // Set accessory information
67
+ accessory
68
+ .getService(this.hap.Service.AccessoryInformation)!
69
+ .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
70
+ .setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
71
+ .setCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.context.name)
72
+ .setCharacteristic(this.hap.Characteristic.Model, accessory.context.model)
73
+ .setCharacteristic(this.hap.Characteristic.SerialNumber, accessory.context.deviceId)
74
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision)
75
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
76
+ .updateValue(accessory.context.FirmwareRevision);
77
+ }
78
+
79
+ async getDeviceLogSettings(device: device & devicesConfig): Promise<void> {
80
+ if (this.platform.debugMode) {
81
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
82
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
83
+ } else if (device.logging) {
84
+ this.deviceLogging = this.accessory.context.logging = device.logging;
85
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
86
+ } else if (this.config.logging) {
87
+ this.deviceLogging = this.accessory.context.logging = this.config.logging;
88
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
89
+ } else {
90
+ this.deviceLogging = this.accessory.context.logging = 'standard';
91
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
92
+ }
93
+ }
94
+
95
+ async getDeviceRefreshRateSettings(device: device & devicesConfig): Promise<void> {
96
+ // refreshRate
97
+ if (device.refreshRate) {
98
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
99
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
100
+ } else if (this.platform.config.options!.refreshRate) {
101
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
102
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
103
+ }
104
+ // updateRate
105
+ if (device.updateRate) {
106
+ this.deviceUpdateRate = device.updateRate;
107
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config updateRate: ${this.deviceUpdateRate}`);
108
+ } else {
109
+ this.deviceUpdateRate = 5;
110
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default updateRate: ${this.deviceUpdateRate}`);
111
+ }
112
+ }
113
+
114
+ async getDeviceRetry(device: device & devicesConfig): Promise<void> {
115
+ if (device.maxRetries) {
116
+ this.deviceMaxRetries = device.maxRetries;
117
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.deviceMaxRetries}`);
118
+ } else {
119
+ this.deviceMaxRetries = 5; // Maximum number of retries
120
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.deviceMaxRetries}`);
121
+ }
122
+ if (device.delayBetweenRetries) {
123
+ this.deviceDelayBetweenRetries = device.delayBetweenRetries * 1000;
124
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.deviceDelayBetweenRetries}`);
125
+ } else {
126
+ this.deviceDelayBetweenRetries = 3000; // Delay between retries in milliseconds
127
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set,`
128
+ + ` Using: ${this.deviceDelayBetweenRetries}`);
129
+ }
130
+ }
131
+
132
+ async retryBLE({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
133
+ return fn().catch(async (e: any) => {
134
+ if (max === 0) {
135
+ throw e;
136
+ }
137
+ this.infoLog(e);
138
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
139
+ await sleep(1000);
140
+ return this.retryBLE({ max: max - 1, fn });
141
+ });
142
+ }
143
+
144
+ async maxRetryBLE(): Promise<number> {
145
+ if (this.device.maxRetry) {
146
+ return this.device.maxRetry;
147
+ } else {
148
+ return 5;
149
+ }
150
+ }
151
+
152
+
153
+ async scan(device: device & devicesConfig): Promise<void> {
154
+ if (device.scanDuration) {
155
+ if (this.deviceUpdateRate > device.scanDuration) {
156
+ this.scanDuration = this.deviceUpdateRate;
157
+ if (this.BLE) {
158
+ this.warnLog(
159
+ `${this.device.deviceType}: ` +
160
+ `${this.accessory.displayName} scanDuration is less than updateRate, overriding scanDuration with updateRate`,
161
+ );
162
+ }
163
+ } else {
164
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
165
+ }
166
+ if (this.BLE) {
167
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
168
+ }
169
+ } else {
170
+ if (this.deviceUpdateRate > 1) {
171
+ this.scanDuration = this.deviceUpdateRate;
172
+ } else {
173
+ this.scanDuration = this.accessory.context.scanDuration = 1;
174
+ }
175
+ if (this.BLE) {
176
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
177
+ }
178
+ }
179
+ }
180
+
181
+ async getDeviceConfigSettings(device: device & devicesConfig): Promise<void> {
182
+ let config = {};
183
+ if (device.logging !== undefined) {
184
+ config['logging'] = device.logging;
185
+ }
186
+ if (device.refreshRate !== undefined) {
187
+ config['refreshRate'] = device.refreshRate;
188
+ }
189
+ if (device.updateRate !== undefined) {
190
+ config['updateRate'] = device.updateRate;
191
+ }
192
+ if (device.scanDuration !== undefined) {
193
+ config['scanDuration'] = device.scanDuration;
194
+ }
195
+ if (device.offline !== undefined) {
196
+ config['offline'] = device.offline;
197
+ }
198
+ if (device.maxRetry !== undefined) {
199
+ config['maxRetry'] = device.maxRetry;
200
+ }
201
+ if (device.webhook !== undefined) {
202
+ config['webhook'] = device.webhook;
203
+ }
204
+ if (device.connectionType !== undefined) {
205
+ config['connectionType'] = device.connectionType;
206
+ }
207
+ if (device.external !== undefined) {
208
+ config['external'] = device.external;
209
+ }
210
+ if (device.mqttURL !== undefined) {
211
+ config['mqttURL'] = device.mqttURL;
212
+ }
213
+ if (device.maxRetries !== undefined) {
214
+ config['maxRetries'] = device.maxRetries;
215
+ }
216
+ if (device.delayBetweenRetries !== undefined) {
217
+ config['delayBetweenRetries'] = device.delayBetweenRetries;
218
+ }
219
+ if (device.bot) {
220
+ config = device.bot;
221
+ }
222
+ if (device.lock) {
223
+ config = device.lock;
224
+ }
225
+ if (device.ceilinglight) {
226
+ config = device.ceilinglight;
227
+ }
228
+ if (device.colorbulb) {
229
+ config = device.colorbulb;
230
+ }
231
+ if (device.contact) {
232
+ config = device.contact;
233
+ }
234
+ if (device.motion) {
235
+ config = device.motion;
236
+ }
237
+ if (device.curtain) {
238
+ config = device.curtain;
239
+ }
240
+ if (device.hub) {
241
+ config = device.hub;
242
+ }
243
+ if (device.waterdetector) {
244
+ config = device.waterdetector;
245
+ }
246
+ if (device.humidifier) {
247
+ config = device.humidifier;
248
+ }
249
+ if (device.meter) {
250
+ config = device.meter;
251
+ }
252
+ if (device.striplight) {
253
+ config = device.striplight;
254
+ }
255
+ if (device.plug) {
256
+ config = device.plug;
257
+ }
258
+ if (device.blindTilt) {
259
+ if (device.blindTilt?.mode === undefined) {
260
+ config['mode'] = BlindTiltMappingMode.OnlyUp;
261
+ } else {
262
+ config['mode'] = device.blindTilt?.mode;
263
+
264
+ config = device.blindTilt;
265
+ }
266
+ }
267
+ if (Object.entries(config).length !== 0) {
268
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
269
+ }
270
+ }
271
+
272
+ /*
273
+ * Publish MQTT message for topics of
274
+ * 'homebridge-switchbot/${this.device.deviceType}/xx:xx:xx:xx:xx:xx'
275
+ */
276
+ mqttPublish(message: string, topic?: string) {
277
+ const mac = this.device.deviceId
278
+ ?.toLowerCase()
279
+ .match(/[\s\S]{1,2}/g)
280
+ ?.join(':');
281
+ const options = this.device.mqttPubOptions || {};
282
+ let mqttTopic: string;
283
+ let mqttMessageTopic: string;
284
+ if (topic) {
285
+ mqttTopic = `/${topic}`;
286
+ mqttMessageTopic = `${topic}/`;
287
+ } else {
288
+ mqttTopic = '';
289
+ mqttMessageTopic = '';
290
+ }
291
+ this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options);
292
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
293
+ + ` MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`);
294
+ }
295
+
296
+ /*
297
+ * Setup MQTT hadler if URL is specified.
298
+ */
299
+ async setupMqtt(device: device & devicesConfig): Promise<void> {
300
+ if (device.mqttURL) {
301
+ try {
302
+ const { connectAsync } = asyncmqtt;
303
+ this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
304
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
305
+ this.mqttClient.on('error', (e: Error) => {
306
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
307
+ });
308
+ } catch (e) {
309
+ this.mqttClient = null;
310
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
311
+ }
312
+ }
313
+ }
314
+
315
+ /*
316
+ * Setup EVE history graph feature if enabled.
317
+ */
318
+ async setupHistoryService(device: device & devicesConfig): Promise<void> {
319
+ const mac = this.device
320
+ .deviceId!.match(/.{1,2}/g)!
321
+ .join(':')
322
+ .toLowerCase();
323
+ this.historyService = device.history
324
+ ? new this.platform.fakegatoAPI('room', this.accessory, {
325
+ log: this.platform.log,
326
+ storage: 'fs',
327
+ filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
328
+ })
329
+ : null;
330
+ }
331
+
332
+ async getCustomBLEAddress(switchbot: any) {
333
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
334
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
335
+ (async () => {
336
+ // Start to monitor advertisement packets
337
+ await switchbot.startScan({ model: this.device.bleModel });
338
+ // Set an event handler
339
+ switchbot.onadvertisement = (ad: any) => {
340
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
341
+ };
342
+ await sleep(10000);
343
+ // Stop to monitor
344
+ switchbot.stopScan();
345
+ })();
346
+ }
347
+ }
348
+
349
+ async getDeviceContext(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
350
+ // Set the accessory context
351
+ switch (device.deviceType) {
352
+ case 'Humidifier':
353
+ device.model = SwitchBotModel.Humidifier;
354
+ device.bleModel = SwitchBotBLEModel.Humidifier;
355
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
356
+ break;
357
+ case 'Hub Mini':
358
+ device.model = SwitchBotModel.HubMini;
359
+ device.bleModel = SwitchBotBLEModel.Unknown;
360
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
361
+ break;
362
+ case 'Hub Plus':
363
+ device.model = SwitchBotModel.HubPlus;
364
+ device.bleModel = SwitchBotBLEModel.Unknown;
365
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
366
+ break;
367
+ case 'Hub 2':
368
+ device.model = SwitchBotModel.Hub2;
369
+ device.bleModel = SwitchBotBLEModel.Unknown;
370
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
371
+ break;
372
+ case 'Bot':
373
+ device.model = SwitchBotModel.Bot;
374
+ device.bleModel = SwitchBotBLEModel.Bot;
375
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
376
+ break;
377
+ case 'Meter':
378
+ device.model = SwitchBotModel.Meter;
379
+ device.bleModel = SwitchBotBLEModel.Meter;
380
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
381
+ break;
382
+ case 'MeterPlus':
383
+ device.model = SwitchBotModel.MeterPlusUS;
384
+ device.bleModel = SwitchBotBLEModel.MeterPlus;
385
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
386
+ break;
387
+ case 'Meter Plus (JP)':
388
+ device.model = SwitchBotModel.MeterPlusJP;
389
+ device.bleModel = SwitchBotBLEModel.MeterPlus;
390
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
391
+ break;
392
+ case 'WoIOSensor':
393
+ device.model = SwitchBotModel.OutdoorMeter;
394
+ device.bleModel = SwitchBotBLEModel.OutdoorMeter;
395
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
396
+ break;
397
+ case 'Water Detector':
398
+ device.model = SwitchBotModel.WaterDetector;
399
+ device.bleModel = SwitchBotBLEModel.Unknown;
400
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
401
+ break;
402
+ case 'Motion Sensor':
403
+ device.model = SwitchBotModel.MotionSensor;
404
+ device.bleModel = SwitchBotBLEModel.MotionSensor;
405
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
406
+ break;
407
+ case 'Contact Sensor':
408
+ device.model = SwitchBotModel.ContactSensor;
409
+ device.bleModel = SwitchBotBLEModel.ContactSensor;
410
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
411
+ break;
412
+ case 'Curtain':
413
+ device.model = SwitchBotModel.Curtain;
414
+ device.bleModel = SwitchBotBLEModel.Curtain;
415
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
416
+ break;
417
+ case 'Curtain3':
418
+ device.model = SwitchBotModel.Curtain3;
419
+ device.bleModel = SwitchBotBLEModel.Curtain3;
420
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
421
+ break;
422
+ case 'Blind Tilt':
423
+ device.model = SwitchBotModel.BlindTilt;
424
+ device.bleModel = SwitchBotBLEModel.BlindTilt;
425
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
426
+ break;
427
+ case 'Plug':
428
+ device.model = SwitchBotModel.Plug;
429
+ device.bleModel = SwitchBotBLEModel.PlugMiniUS;
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
431
+ break;
432
+ case 'Plug Mini (US)':
433
+ device.model = SwitchBotModel.PlugMiniUS;
434
+ device.bleModel = SwitchBotBLEModel.PlugMiniUS;
435
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
436
+ break;
437
+ case 'Plug Mini (JP)':
438
+ device.model = SwitchBotModel.PlugMiniJP;
439
+ device.bleModel = SwitchBotBLEModel.PlugMiniJP;
440
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
441
+ break;
442
+ case 'Smart Lock':
443
+ device.model = SwitchBotModel.Lock;
444
+ device.bleModel = SwitchBotBLEModel.Lock;
445
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
446
+ break;
447
+ case 'Smart Lock Pro':
448
+ device.model = SwitchBotModel.LockPro;
449
+ device.bleModel = SwitchBotBLEModel.Unknown;
450
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
451
+ break;
452
+ case 'Color Bulb':
453
+ device.model = SwitchBotModel.ColorBulb;
454
+ device.bleModel = SwitchBotBLEModel.ColorBulb;
455
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
456
+ break;
457
+ case 'K10+':
458
+ device.model = SwitchBotModel.K10;
459
+ device.bleModel = SwitchBotBLEModel.Unknown;
460
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
461
+ break;
462
+ case 'WoSweeper':
463
+ device.model = SwitchBotModel.WoSweeper;
464
+ device.bleModel = SwitchBotBLEModel.Unknown;
465
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
466
+ break;
467
+ case 'WoSweeperMini':
468
+ device.model = SwitchBotModel.WoSweeperMini;
469
+ device.bleModel = SwitchBotBLEModel.Unknown;
470
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
471
+ break;
472
+ case 'Robot Vacuum Cleaner S1':
473
+ device.model = SwitchBotModel.RobotVacuumCleanerS1;
474
+ device.bleModel = SwitchBotBLEModel.Unknown;
475
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
476
+ break;
477
+ case 'Robot Vacuum Cleaner S1 Plus':
478
+ device.model = SwitchBotModel.RobotVacuumCleanerS1Plus;
479
+ device.bleModel = SwitchBotBLEModel.Unknown;
480
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
481
+ break;
482
+ case 'Ceiling Light':
483
+ device.model = SwitchBotModel.CeilingLight;
484
+ device.bleModel = SwitchBotBLEModel.CeilingLight;
485
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
486
+ break;
487
+ case 'Ceiling Light Pro':
488
+ device.model = SwitchBotModel.CeilingLightPro;
489
+ device.bleModel = SwitchBotBLEModel.CeilingLightPro;
490
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
491
+ break;
492
+ case 'Strip Light':
493
+ device.model = SwitchBotModel.StripLight;
494
+ device.bleModel = SwitchBotBLEModel.StripLight;
495
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
496
+ break;
497
+ case 'Indoor Cam':
498
+ device.model = SwitchBotModel.IndoorCam;
499
+ device.bleModel = SwitchBotBLEModel.Unknown;
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
501
+ break;
502
+ case 'Remote':
503
+ device.model = SwitchBotModel.Remote;
504
+ device.bleModel = SwitchBotBLEModel.Remote;
505
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
506
+ break;
507
+ case 'remote with screen+':
508
+ device.model = SwitchBotModel.UniversalRemote;
509
+ device.bleModel = SwitchBotBLEModel.Unknown;
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
511
+ break;
512
+ default:
513
+ device.model = SwitchBotModel.Unknown;
514
+ device.bleModel = SwitchBotBLEModel.Unknown;
515
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
516
+ }
517
+ accessory.context.name = device.deviceName;
518
+ accessory.context.model = device.model;
519
+ accessory.context.deviceId = device.deviceId;
520
+ accessory.context.deviceType = device.deviceType;
521
+ if (device.firmware === undefined) {
522
+ device.firmware = this.platform.version;
523
+ accessory.context.FirmwareRevision = device.firmware;
524
+ } else {
525
+ accessory.context.FirmwareRevision = device.firmware;
526
+ }
527
+ }
528
+
529
+ async statusCode(statusCode: number): Promise<void> {
530
+ switch (statusCode) {
531
+ case 151:
532
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
533
+ break;
534
+ case 152:
535
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
536
+ break;
537
+ case 160:
538
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
539
+ break;
540
+ case 161:
541
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
542
+ break;
543
+ case 171:
544
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
545
+ `Hub: ${this.device.hubDeviceId}`);
546
+ break;
547
+ case 190:
548
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with` +
549
+ ` server, Or command format is invalid, statusCode: ${statusCode}`);
550
+ break;
551
+ case 100:
552
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
553
+ break;
554
+ case 200:
555
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
556
+ break;
557
+ case 400:
558
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
559
+ + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
560
+ break;
561
+ case 401:
562
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
563
+ + `but the request has not been authenticated, statusCode: ${statusCode}`);
564
+ break;
565
+ case 403:
566
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
567
+ + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
568
+ break;
569
+ case 404:
570
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
571
+ + `statusCode: ${statusCode}`);
572
+ break;
573
+ case 406:
574
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
575
+ + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
576
+ break;
577
+ case 415:
578
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
579
+ + `header that is not supported by the server, statusCode: ${statusCode}`);
580
+ break;
581
+ case 422:
582
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
583
+ + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
584
+ break;
585
+ case 429:
586
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
587
+ + `requests allowed for a given time window, statusCode: ${statusCode}`);
588
+ break;
589
+ case 500:
590
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
591
+ + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
592
+ break;
593
+ default:
594
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
595
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
596
+ }
597
+ }
598
+
599
+ /**
600
+ * Logging for Device
601
+ */
602
+ successLog(...log: any[]): void {
603
+ if (this.enablingDeviceLogging()) {
604
+ this.platform.log.success(String(...log));
605
+ }
606
+ }
607
+
608
+ infoLog(...log: any[]): void {
609
+ if (this.enablingDeviceLogging()) {
610
+ this.log.info(String(...log));
611
+ }
612
+ }
613
+
614
+ warnLog(...log: any[]): void {
615
+ if (this.enablingDeviceLogging()) {
616
+ this.log.warn(String(...log));
617
+ }
618
+ }
619
+
620
+ debugWarnLog(...log: any[]): void {
621
+ if (this.enablingDeviceLogging()) {
622
+ if (this.deviceLogging?.includes('debug')) {
623
+ this.log.warn('[DEBUG]', String(...log));
624
+ }
625
+ }
626
+ }
627
+
628
+ errorLog(...log: any[]): void {
629
+ if (this.enablingDeviceLogging()) {
630
+ this.log.error(String(...log));
631
+ }
632
+ }
633
+
634
+ debugErrorLog(...log: any[]): void {
635
+ if (this.enablingDeviceLogging()) {
636
+ if (this.deviceLogging?.includes('debug')) {
637
+ this.log.error('[DEBUG]', String(...log));
638
+ }
639
+ }
640
+ }
641
+
642
+ debugLog(...log: any[]): void {
643
+ if (this.enablingDeviceLogging()) {
644
+ if (this.deviceLogging === 'debug') {
645
+ this.log.info('[DEBUG]', String(...log));
646
+ } else {
647
+ this.log.debug(String(...log));
648
+ }
649
+ }
650
+ }
651
+
652
+ enablingDeviceLogging(): boolean {
653
+ return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
654
+ }
655
+ }