@switchbot/homebridge-switchbot 3.5.0-beta.3 → 3.5.0-beta.30

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 (166) hide show
  1. package/CHANGELOG.md +4 -1
  2. package/config.schema.json +147 -9
  3. package/dist/device/blindtilt.d.ts +21 -94
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +347 -756
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +25 -71
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +582 -759
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +13 -67
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +209 -587
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +13 -74
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +250 -632
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +14 -65
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +204 -531
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +17 -86
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +335 -786
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts +59 -0
  28. package/dist/device/device.d.ts.map +1 -0
  29. package/dist/device/device.js +646 -0
  30. package/dist/device/device.js.map +1 -0
  31. package/dist/device/fan.d.ts +51 -0
  32. package/dist/device/fan.d.ts.map +1 -0
  33. package/dist/device/fan.js +499 -0
  34. package/dist/device/fan.js.map +1 -0
  35. package/dist/device/hub.d.ts +15 -59
  36. package/dist/device/hub.d.ts.map +1 -1
  37. package/dist/device/hub.js +242 -596
  38. package/dist/device/hub.js.map +1 -1
  39. package/dist/device/humidifier.d.ts +10 -65
  40. package/dist/device/humidifier.d.ts.map +1 -1
  41. package/dist/device/humidifier.js +207 -571
  42. package/dist/device/humidifier.js.map +1 -1
  43. package/dist/device/iosensor.d.ts +11 -64
  44. package/dist/device/iosensor.d.ts.map +1 -1
  45. package/dist/device/iosensor.js +174 -584
  46. package/dist/device/iosensor.js.map +1 -1
  47. package/dist/device/lightstrip.d.ts +10 -68
  48. package/dist/device/lightstrip.d.ts.map +1 -1
  49. package/dist/device/lightstrip.js +252 -568
  50. package/dist/device/lightstrip.js.map +1 -1
  51. package/dist/device/lock.d.ts +15 -62
  52. package/dist/device/lock.d.ts.map +1 -1
  53. package/dist/device/lock.js +210 -542
  54. package/dist/device/lock.js.map +1 -1
  55. package/dist/device/meter.d.ts +13 -63
  56. package/dist/device/meter.d.ts.map +1 -1
  57. package/dist/device/meter.js +170 -577
  58. package/dist/device/meter.js.map +1 -1
  59. package/dist/device/meterplus.d.ts +13 -64
  60. package/dist/device/meterplus.d.ts.map +1 -1
  61. package/dist/device/meterplus.js +159 -577
  62. package/dist/device/meterplus.js.map +1 -1
  63. package/dist/device/motion.d.ts +13 -69
  64. package/dist/device/motion.d.ts.map +1 -1
  65. package/dist/device/motion.js +136 -518
  66. package/dist/device/motion.js.map +1 -1
  67. package/dist/device/plug.d.ts +9 -57
  68. package/dist/device/plug.d.ts.map +1 -1
  69. package/dist/device/plug.js +82 -452
  70. package/dist/device/plug.js.map +1 -1
  71. package/dist/device/robotvacuumcleaner.d.ts +11 -62
  72. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  73. package/dist/device/robotvacuumcleaner.js +140 -451
  74. package/dist/device/robotvacuumcleaner.js.map +1 -1
  75. package/dist/device/waterdetector.d.ts +9 -70
  76. package/dist/device/waterdetector.d.ts.map +1 -1
  77. package/dist/device/waterdetector.js +121 -664
  78. package/dist/device/waterdetector.js.map +1 -1
  79. package/dist/index.js +1 -1
  80. package/dist/irdevice/airconditioner.d.ts +8 -48
  81. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  82. package/dist/irdevice/airconditioner.js +309 -419
  83. package/dist/irdevice/airconditioner.js.map +1 -1
  84. package/dist/irdevice/airpurifier.d.ts +7 -38
  85. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  86. package/dist/irdevice/airpurifier.js +72 -304
  87. package/dist/irdevice/airpurifier.js.map +1 -1
  88. package/dist/irdevice/camera.d.ts +6 -35
  89. package/dist/irdevice/camera.d.ts.map +1 -1
  90. package/dist/irdevice/camera.js +34 -278
  91. package/dist/irdevice/camera.js.map +1 -1
  92. package/dist/irdevice/fan.d.ts +6 -39
  93. package/dist/irdevice/fan.d.ts.map +1 -1
  94. package/dist/irdevice/fan.js +67 -311
  95. package/dist/irdevice/fan.js.map +1 -1
  96. package/dist/irdevice/irdevice.d.ts +40 -0
  97. package/dist/irdevice/irdevice.d.ts.map +1 -0
  98. package/dist/irdevice/irdevice.js +509 -0
  99. package/dist/irdevice/irdevice.js.map +1 -0
  100. package/dist/irdevice/light.d.ts +6 -34
  101. package/dist/irdevice/light.d.ts.map +1 -1
  102. package/dist/irdevice/light.js +23 -272
  103. package/dist/irdevice/light.js.map +1 -1
  104. package/dist/irdevice/other.d.ts +18 -47
  105. package/dist/irdevice/other.d.ts.map +1 -1
  106. package/dist/irdevice/other.js +352 -546
  107. package/dist/irdevice/other.js.map +1 -1
  108. package/dist/irdevice/tv.d.ts +7 -39
  109. package/dist/irdevice/tv.d.ts.map +1 -1
  110. package/dist/irdevice/tv.js +82 -321
  111. package/dist/irdevice/tv.js.map +1 -1
  112. package/dist/irdevice/vacuumcleaner.d.ts +6 -35
  113. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  114. package/dist/irdevice/vacuumcleaner.js +41 -284
  115. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  116. package/dist/irdevice/waterheater.d.ts +6 -35
  117. package/dist/irdevice/waterheater.d.ts.map +1 -1
  118. package/dist/irdevice/waterheater.js +42 -287
  119. package/dist/irdevice/waterheater.js.map +1 -1
  120. package/dist/platform.d.ts +14 -1
  121. package/dist/platform.d.ts.map +1 -1
  122. package/dist/platform.js +237 -94
  123. package/dist/platform.js.map +1 -1
  124. package/dist/settings.d.ts +40 -10
  125. package/dist/settings.d.ts.map +1 -1
  126. package/dist/settings.js +0 -552
  127. package/dist/settings.js.map +1 -1
  128. package/dist/utils.d.ts +77 -0
  129. package/dist/utils.d.ts.map +1 -1
  130. package/dist/utils.js +638 -1
  131. package/dist/utils.js.map +1 -1
  132. package/package.json +16 -15
  133. package/src/custom.d.ts +2 -1
  134. package/src/device/blindtilt.ts +373 -782
  135. package/src/device/bot.ts +616 -844
  136. package/src/device/ceilinglight.ts +222 -607
  137. package/src/device/colorbulb.ts +264 -656
  138. package/src/device/contact.ts +229 -553
  139. package/src/device/curtain.ts +355 -810
  140. package/src/device/device.ts +673 -0
  141. package/src/device/fan.ts +548 -0
  142. package/src/device/hub.ts +268 -623
  143. package/src/device/humidifier.ts +237 -618
  144. package/src/device/iosensor.ts +200 -610
  145. package/src/device/lightstrip.ts +273 -596
  146. package/src/device/lock.ts +247 -582
  147. package/src/device/meter.ts +196 -601
  148. package/src/device/meterplus.ts +184 -602
  149. package/src/device/motion.ts +156 -541
  150. package/src/device/plug.ts +92 -480
  151. package/src/device/robotvacuumcleaner.ts +161 -477
  152. package/src/device/waterdetector.ts +139 -689
  153. package/src/index.ts +1 -1
  154. package/src/irdevice/airconditioner.ts +330 -446
  155. package/src/irdevice/airpurifier.ts +89 -321
  156. package/src/irdevice/camera.ts +42 -293
  157. package/src/irdevice/fan.ts +78 -327
  158. package/src/irdevice/irdevice.ts +526 -0
  159. package/src/irdevice/light.ts +32 -287
  160. package/src/irdevice/other.ts +396 -615
  161. package/src/irdevice/tv.ts +100 -339
  162. package/src/irdevice/vacuumcleaner.ts +49 -305
  163. package/src/irdevice/waterheater.ts +50 -310
  164. package/src/platform.ts +247 -102
  165. package/src/settings.ts +56 -565
  166. package/src/utils.ts +642 -1
@@ -0,0 +1,673 @@
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 !== 0 ) {
199
+ config['maxRetry'] = device.maxRetry;
200
+ }
201
+ if (device.webhook === true) {
202
+ config['webhook'] = device.webhook;
203
+ }
204
+ if (device.connectionType) {
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 'Robot Vacuum Cleaner S10':
483
+ device.model = SwitchBotModel.RobotVacuumCleanerS10;
484
+ device.bleModel = SwitchBotBLEModel.Unknown;
485
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
486
+ break;
487
+ case 'Ceiling Light':
488
+ device.model = SwitchBotModel.CeilingLight;
489
+ device.bleModel = SwitchBotBLEModel.CeilingLight;
490
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
491
+ break;
492
+ case 'Ceiling Light Pro':
493
+ device.model = SwitchBotModel.CeilingLightPro;
494
+ device.bleModel = SwitchBotBLEModel.CeilingLightPro;
495
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
496
+ break;
497
+ case 'Strip Light':
498
+ device.model = SwitchBotModel.StripLight;
499
+ device.bleModel = SwitchBotBLEModel.StripLight;
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
501
+ break;
502
+ case 'Indoor Cam':
503
+ device.model = SwitchBotModel.IndoorCam;
504
+ device.bleModel = SwitchBotBLEModel.Unknown;
505
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
506
+ break;
507
+ case 'Remote':
508
+ device.model = SwitchBotModel.Remote;
509
+ device.bleModel = SwitchBotBLEModel.Remote;
510
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
511
+ break;
512
+ case 'remote with screen+':
513
+ device.model = SwitchBotModel.UniversalRemote;
514
+ device.bleModel = SwitchBotBLEModel.Unknown;
515
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
516
+ break;
517
+ case 'Battery Circulator Fan':
518
+ device.model = SwitchBotModel.BatteryCirculatorFan;
519
+ device.bleModel = SwitchBotBLEModel.Unknown;
520
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
521
+ break;
522
+ default:
523
+ device.model = SwitchBotModel.Unknown;
524
+ device.bleModel = SwitchBotBLEModel.Unknown;
525
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
526
+ }
527
+ accessory.context.name = device.deviceName;
528
+ accessory.context.model = device.model;
529
+ accessory.context.deviceId = device.deviceId;
530
+ accessory.context.deviceType = device.deviceType;
531
+ if (device.firmware === undefined) {
532
+ device.firmware = this.platform.version;
533
+ accessory.context.FirmwareRevision = device.firmware;
534
+ } else {
535
+ accessory.context.FirmwareRevision = device.firmware;
536
+ }
537
+ }
538
+
539
+ async statusCode(statusCode: number): Promise<void> {
540
+ switch (statusCode) {
541
+ case 151:
542
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
543
+ break;
544
+ case 152:
545
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
546
+ break;
547
+ case 160:
548
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
549
+ break;
550
+ case 161:
551
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
552
+ break;
553
+ case 171:
554
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
555
+ `Hub: ${this.device.hubDeviceId}`);
556
+ break;
557
+ case 190:
558
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with` +
559
+ ` server, Or command format is invalid, statusCode: ${statusCode}`);
560
+ break;
561
+ case 100:
562
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
563
+ break;
564
+ case 200:
565
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
566
+ break;
567
+ case 400:
568
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
569
+ + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
570
+ break;
571
+ case 401:
572
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
573
+ + `but the request has not been authenticated, statusCode: ${statusCode}`);
574
+ break;
575
+ case 403:
576
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
577
+ + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
578
+ break;
579
+ case 404:
580
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
581
+ + `statusCode: ${statusCode}`);
582
+ break;
583
+ case 406:
584
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
585
+ + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
586
+ break;
587
+ case 415:
588
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
589
+ + `header that is not supported by the server, statusCode: ${statusCode}`);
590
+ break;
591
+ case 422:
592
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
593
+ + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
594
+ break;
595
+ case 429:
596
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
597
+ + `requests allowed for a given time window, statusCode: ${statusCode}`);
598
+ break;
599
+ case 500:
600
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
601
+ + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
602
+ break;
603
+ default:
604
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
605
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
606
+ }
607
+ }
608
+
609
+ /**
610
+ * Logging for Device
611
+ */
612
+ infoLog(...log: any[]): void {
613
+ if (this.enablingDeviceLogging()) {
614
+ this.log.info(String(...log));
615
+ }
616
+ }
617
+
618
+ successLog(...log: any[]): void {
619
+ if (this.enablingDeviceLogging()) {
620
+ this.log.success(String(...log));
621
+ }
622
+ }
623
+
624
+ debugSuccessLog(...log: any[]): void {
625
+ if (this.enablingDeviceLogging()) {
626
+ if (this.deviceLogging?.includes('debug')) {
627
+ this.log.success('[DEBUG]', String(...log));
628
+ }
629
+ }
630
+ }
631
+
632
+ warnLog(...log: any[]): void {
633
+ if (this.enablingDeviceLogging()) {
634
+ this.log.warn(String(...log));
635
+ }
636
+ }
637
+
638
+ debugWarnLog(...log: any[]): void {
639
+ if (this.enablingDeviceLogging()) {
640
+ if (this.deviceLogging?.includes('debug')) {
641
+ this.log.warn('[DEBUG]', String(...log));
642
+ }
643
+ }
644
+ }
645
+
646
+ errorLog(...log: any[]): void {
647
+ if (this.enablingDeviceLogging()) {
648
+ this.log.error(String(...log));
649
+ }
650
+ }
651
+
652
+ debugErrorLog(...log: any[]): void {
653
+ if (this.enablingDeviceLogging()) {
654
+ if (this.deviceLogging?.includes('debug')) {
655
+ this.log.error('[DEBUG]', String(...log));
656
+ }
657
+ }
658
+ }
659
+
660
+ debugLog(...log: any[]): void {
661
+ if (this.enablingDeviceLogging()) {
662
+ if (this.deviceLogging === 'debug') {
663
+ this.log.info('[DEBUG]', String(...log));
664
+ } else {
665
+ this.log.debug(String(...log));
666
+ }
667
+ }
668
+ }
669
+
670
+ enablingDeviceLogging(): boolean {
671
+ return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
672
+ }
673
+ }