@switchbot/homebridge-switchbot 3.5.0-beta.4 → 3.5.0-beta.41

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