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

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 +349 -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 +584 -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 +211 -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 +252 -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 +206 -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 +337 -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 +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 +503 -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 +244 -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 +209 -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 +176 -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 +254 -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 +212 -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 +172 -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 +161 -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 +138 -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 +84 -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 +142 -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 +123 -664
  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 +375 -782
  136. package/src/device/bot.ts +618 -844
  137. package/src/device/ceilinglight.ts +224 -607
  138. package/src/device/colorbulb.ts +266 -656
  139. package/src/device/contact.ts +231 -553
  140. package/src/device/curtain.ts +357 -810
  141. package/src/device/device.ts +699 -0
  142. package/src/device/fan.ts +552 -0
  143. package/src/device/hub.ts +271 -624
  144. package/src/device/humidifier.ts +239 -618
  145. package/src/device/iosensor.ts +202 -610
  146. package/src/device/lightstrip.ts +275 -596
  147. package/src/device/lock.ts +249 -582
  148. package/src/device/meter.ts +198 -601
  149. package/src/device/meterplus.ts +186 -602
  150. package/src/device/motion.ts +158 -541
  151. package/src/device/plug.ts +94 -480
  152. package/src/device/robotvacuumcleaner.ts +163 -477
  153. package/src/device/waterdetector.ts +141 -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,699 @@
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.version)
75
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
76
+ .updateValue(accessory.context.version);
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
+ const deviceConfig = {};
183
+ if (device.logging !== 'standard') {
184
+ deviceConfig['logging'] = device.logging;
185
+ }
186
+ if (device.refreshRate !== 0) {
187
+ deviceConfig['refreshRate'] = device.refreshRate;
188
+ }
189
+ if (device.updateRate !== 0) {
190
+ deviceConfig['updateRate'] = device.updateRate;
191
+ }
192
+ if (device.scanDuration !== 0) {
193
+ deviceConfig['scanDuration'] = device.scanDuration;
194
+ }
195
+ if (device.offline === true) {
196
+ deviceConfig['offline'] = device.offline;
197
+ }
198
+ if (device.maxRetry !== 0) {
199
+ deviceConfig['maxRetry'] = device.maxRetry;
200
+ }
201
+ if (device.webhook === true) {
202
+ deviceConfig['webhook'] = device.webhook;
203
+ }
204
+ if (device.connectionType !== '') {
205
+ deviceConfig['connectionType'] = device.connectionType;
206
+ }
207
+ if (device.external === true) {
208
+ deviceConfig['external'] = device.external;
209
+ }
210
+ if (device.mqttURL !== '') {
211
+ deviceConfig['mqttURL'] = device.mqttURL;
212
+ }
213
+ if (device.maxRetries !== 0) {
214
+ deviceConfig['maxRetries'] = device.maxRetries;
215
+ }
216
+ if (device.delayBetweenRetries !== 0) {
217
+ deviceConfig['delayBetweenRetries'] = device.delayBetweenRetries;
218
+ }
219
+ let botConfig = {};
220
+ if (device.bot) {
221
+ botConfig = device.bot;
222
+ }
223
+ let lockConfig = {};
224
+ if (device.lock) {
225
+ lockConfig = device.lock;
226
+ }
227
+ let ceilinglightConfig = {};
228
+ if (device.ceilinglight) {
229
+ ceilinglightConfig = device.ceilinglight;
230
+ }
231
+ let colorbulbConfig = {};
232
+ if (device.colorbulb) {
233
+ colorbulbConfig = device.colorbulb;
234
+ }
235
+ let contactConfig = {};
236
+ if (device.contact) {
237
+ contactConfig = device.contact;
238
+ }
239
+ let motionConfig = {};
240
+ if (device.motion) {
241
+ motionConfig = device.motion;
242
+ }
243
+ let curtainConfig = {};
244
+ if (device.curtain) {
245
+ curtainConfig = device.curtain;
246
+ }
247
+ let hubConfig = {};
248
+ if (device.hub) {
249
+ hubConfig = device.hub;
250
+ }
251
+ let waterdetectorConfig = {};
252
+ if (device.waterdetector) {
253
+ waterdetectorConfig = device.waterdetector;
254
+ }
255
+ let humidifierConfig = {};
256
+ if (device.humidifier) {
257
+ humidifierConfig = device.humidifier;
258
+ }
259
+ let meterConfig = {};
260
+ if (device.meter) {
261
+ meterConfig = device.meter;
262
+ }
263
+ let iosensorConfig = {};
264
+ if (device.iosensor) {
265
+ iosensorConfig = device.iosensor;
266
+ }
267
+ let striplightConfig = {};
268
+ if (device.striplight) {
269
+ striplightConfig = device.striplight;
270
+ }
271
+ let plugConfig = {};
272
+ if (device.plug) {
273
+ plugConfig = device.plug;
274
+ }
275
+ let blindTiltConfig = {};
276
+ if (device.blindTilt) {
277
+ if (device.blindTilt?.mode === undefined) {
278
+ blindTiltConfig['mode'] = BlindTiltMappingMode.OnlyUp;
279
+ }
280
+ blindTiltConfig = device.blindTilt;
281
+ }
282
+ const config = Object.assign({}, deviceConfig, botConfig, curtainConfig, waterdetectorConfig, striplightConfig, plugConfig, iosensorConfig,
283
+ meterConfig, humidifierConfig, hubConfig, lockConfig, ceilinglightConfig, colorbulbConfig, contactConfig, motionConfig, blindTiltConfig);
284
+ if (Object.entries(config).length !== 0) {
285
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
286
+ }
287
+ }
288
+
289
+ /*
290
+ * Publish MQTT message for topics of
291
+ * 'homebridge-switchbot/${this.device.deviceType}/xx:xx:xx:xx:xx:xx'
292
+ */
293
+ mqttPublish(message: string, topic?: string) {
294
+ const mac = this.device.deviceId
295
+ ?.toLowerCase()
296
+ .match(/[\s\S]{1,2}/g)
297
+ ?.join(':');
298
+ const options = this.device.mqttPubOptions || {};
299
+ let mqttTopic: string;
300
+ let mqttMessageTopic: string;
301
+ if (topic) {
302
+ mqttTopic = `/${topic}`;
303
+ mqttMessageTopic = `${topic}/`;
304
+ } else {
305
+ mqttTopic = '';
306
+ mqttMessageTopic = '';
307
+ }
308
+ this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options);
309
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
310
+ + ` MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`);
311
+ }
312
+
313
+ /*
314
+ * Setup MQTT hadler if URL is specified.
315
+ */
316
+ async setupMqtt(device: device & devicesConfig): Promise<void> {
317
+ if (device.mqttURL) {
318
+ try {
319
+ const { connectAsync } = asyncmqtt;
320
+ this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
321
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
322
+ this.mqttClient.on('error', (e: Error) => {
323
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
324
+ });
325
+ } catch (e) {
326
+ this.mqttClient = null;
327
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
328
+ }
329
+ }
330
+ }
331
+
332
+ /*
333
+ * Setup EVE history graph feature if enabled.
334
+ */
335
+ async setupHistoryService(device: device & devicesConfig): Promise<void> {
336
+ const mac = this.device
337
+ .deviceId!.match(/.{1,2}/g)!
338
+ .join(':')
339
+ .toLowerCase();
340
+ this.historyService = device.history
341
+ ? new this.platform.fakegatoAPI('room', this.accessory, {
342
+ log: this.platform.log,
343
+ storage: 'fs',
344
+ filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
345
+ })
346
+ : null;
347
+ }
348
+
349
+ async getCustomBLEAddress(switchbot: any) {
350
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
351
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} customBLEaddress: ${this.device.customBLEaddress}`);
352
+ (async () => {
353
+ // Start to monitor advertisement packets
354
+ await switchbot.startScan({ model: this.device.bleModel });
355
+ // Set an event handler
356
+ switchbot.onadvertisement = (ad: any) => {
357
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
358
+ };
359
+ await sleep(10000);
360
+ // Stop to monitor
361
+ switchbot.stopScan();
362
+ })();
363
+ }
364
+ }
365
+
366
+ async getDeviceContext(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
367
+ // Set the accessory context
368
+ switch (device.deviceType) {
369
+ case 'Humidifier':
370
+ device.model = SwitchBotModel.Humidifier;
371
+ device.bleModel = SwitchBotBLEModel.Humidifier;
372
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
373
+ break;
374
+ case 'Hub Mini':
375
+ device.model = SwitchBotModel.HubMini;
376
+ device.bleModel = SwitchBotBLEModel.Unknown;
377
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
378
+ break;
379
+ case 'Hub Plus':
380
+ device.model = SwitchBotModel.HubPlus;
381
+ device.bleModel = SwitchBotBLEModel.Unknown;
382
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
383
+ break;
384
+ case 'Hub 2':
385
+ device.model = SwitchBotModel.Hub2;
386
+ device.bleModel = SwitchBotBLEModel.Unknown;
387
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
388
+ break;
389
+ case 'Bot':
390
+ device.model = SwitchBotModel.Bot;
391
+ device.bleModel = SwitchBotBLEModel.Bot;
392
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
393
+ break;
394
+ case 'Meter':
395
+ device.model = SwitchBotModel.Meter;
396
+ device.bleModel = SwitchBotBLEModel.Meter;
397
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
398
+ break;
399
+ case 'MeterPlus':
400
+ device.model = SwitchBotModel.MeterPlusUS;
401
+ device.bleModel = SwitchBotBLEModel.MeterPlus;
402
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
403
+ break;
404
+ case 'Meter Plus (JP)':
405
+ device.model = SwitchBotModel.MeterPlusJP;
406
+ device.bleModel = SwitchBotBLEModel.MeterPlus;
407
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
408
+ break;
409
+ case 'WoIOSensor':
410
+ device.model = SwitchBotModel.OutdoorMeter;
411
+ device.bleModel = SwitchBotBLEModel.OutdoorMeter;
412
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
413
+ break;
414
+ case 'Water Detector':
415
+ device.model = SwitchBotModel.WaterDetector;
416
+ device.bleModel = SwitchBotBLEModel.Unknown;
417
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
418
+ break;
419
+ case 'Motion Sensor':
420
+ device.model = SwitchBotModel.MotionSensor;
421
+ device.bleModel = SwitchBotBLEModel.MotionSensor;
422
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
423
+ break;
424
+ case 'Contact Sensor':
425
+ device.model = SwitchBotModel.ContactSensor;
426
+ device.bleModel = SwitchBotBLEModel.ContactSensor;
427
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
428
+ break;
429
+ case 'Curtain':
430
+ device.model = SwitchBotModel.Curtain;
431
+ device.bleModel = SwitchBotBLEModel.Curtain;
432
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
433
+ break;
434
+ case 'Curtain3':
435
+ device.model = SwitchBotModel.Curtain3;
436
+ device.bleModel = SwitchBotBLEModel.Curtain3;
437
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
438
+ break;
439
+ case 'Blind Tilt':
440
+ device.model = SwitchBotModel.BlindTilt;
441
+ device.bleModel = SwitchBotBLEModel.BlindTilt;
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
443
+ break;
444
+ case 'Plug':
445
+ device.model = SwitchBotModel.Plug;
446
+ device.bleModel = SwitchBotBLEModel.PlugMiniUS;
447
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
448
+ break;
449
+ case 'Plug Mini (US)':
450
+ device.model = SwitchBotModel.PlugMiniUS;
451
+ device.bleModel = SwitchBotBLEModel.PlugMiniUS;
452
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
453
+ break;
454
+ case 'Plug Mini (JP)':
455
+ device.model = SwitchBotModel.PlugMiniJP;
456
+ device.bleModel = SwitchBotBLEModel.PlugMiniJP;
457
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
458
+ break;
459
+ case 'Smart Lock':
460
+ device.model = SwitchBotModel.Lock;
461
+ device.bleModel = SwitchBotBLEModel.Lock;
462
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
463
+ break;
464
+ case 'Smart Lock Pro':
465
+ device.model = SwitchBotModel.LockPro;
466
+ device.bleModel = SwitchBotBLEModel.Unknown;
467
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
468
+ break;
469
+ case 'Color Bulb':
470
+ device.model = SwitchBotModel.ColorBulb;
471
+ device.bleModel = SwitchBotBLEModel.ColorBulb;
472
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
473
+ break;
474
+ case 'K10+':
475
+ device.model = SwitchBotModel.K10;
476
+ device.bleModel = SwitchBotBLEModel.Unknown;
477
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
478
+ break;
479
+ case 'WoSweeper':
480
+ device.model = SwitchBotModel.WoSweeper;
481
+ device.bleModel = SwitchBotBLEModel.Unknown;
482
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
483
+ break;
484
+ case 'WoSweeperMini':
485
+ device.model = SwitchBotModel.WoSweeperMini;
486
+ device.bleModel = SwitchBotBLEModel.Unknown;
487
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
488
+ break;
489
+ case 'Robot Vacuum Cleaner S1':
490
+ device.model = SwitchBotModel.RobotVacuumCleanerS1;
491
+ device.bleModel = SwitchBotBLEModel.Unknown;
492
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
493
+ break;
494
+ case 'Robot Vacuum Cleaner S1 Plus':
495
+ device.model = SwitchBotModel.RobotVacuumCleanerS1Plus;
496
+ device.bleModel = SwitchBotBLEModel.Unknown;
497
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
498
+ break;
499
+ case 'Robot Vacuum Cleaner S10':
500
+ device.model = SwitchBotModel.RobotVacuumCleanerS10;
501
+ device.bleModel = SwitchBotBLEModel.Unknown;
502
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
503
+ break;
504
+ case 'Ceiling Light':
505
+ device.model = SwitchBotModel.CeilingLight;
506
+ device.bleModel = SwitchBotBLEModel.CeilingLight;
507
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
508
+ break;
509
+ case 'Ceiling Light Pro':
510
+ device.model = SwitchBotModel.CeilingLightPro;
511
+ device.bleModel = SwitchBotBLEModel.CeilingLightPro;
512
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
513
+ break;
514
+ case 'Strip Light':
515
+ device.model = SwitchBotModel.StripLight;
516
+ device.bleModel = SwitchBotBLEModel.StripLight;
517
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
518
+ break;
519
+ case 'Indoor Cam':
520
+ device.model = SwitchBotModel.IndoorCam;
521
+ device.bleModel = SwitchBotBLEModel.Unknown;
522
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
523
+ break;
524
+ case 'Remote':
525
+ device.model = SwitchBotModel.Remote;
526
+ device.bleModel = SwitchBotBLEModel.Remote;
527
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
528
+ break;
529
+ case 'remote with screen+':
530
+ device.model = SwitchBotModel.UniversalRemote;
531
+ device.bleModel = SwitchBotBLEModel.Unknown;
532
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
533
+ break;
534
+ case 'Battery Circulator Fan':
535
+ device.model = SwitchBotModel.BatteryCirculatorFan;
536
+ device.bleModel = SwitchBotBLEModel.Unknown;
537
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
538
+ break;
539
+ default:
540
+ device.model = SwitchBotModel.Unknown;
541
+ device.bleModel = SwitchBotBLEModel.Unknown;
542
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Model: ${device.model}, BLE Model: ${device.bleModel}`);
543
+ }
544
+ accessory.context.name = device.deviceName;
545
+ accessory.context.model = device.model;
546
+ accessory.context.deviceId = device.deviceId;
547
+ accessory.context.deviceType = device.deviceType;
548
+
549
+ if (device.firmware) {
550
+ accessory.context.version = device.firmware;
551
+ } else if (device.firmware === undefined && device.version === undefined && accessory.context.version === undefined) {
552
+ device.version = this.platform.version;
553
+ accessory.context.version = this.platform.version;
554
+ } else {
555
+ accessory.context.version = device.version;
556
+ }
557
+ }
558
+
559
+ async statusCode(statusCode: number): Promise<void> {
560
+ switch (this.device.deviceType) {
561
+ case this.device.hubDeviceId:
562
+ case '000000000000':
563
+ statusCode = 161;
564
+ break;
565
+ }
566
+ switch (statusCode) {
567
+ case 151:
568
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
569
+ break;
570
+ case 152:
571
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
572
+ break;
573
+ case 160:
574
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
575
+ break;
576
+ case 161:
577
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
578
+ break;
579
+ case 171:
580
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
581
+ `Hub: ${this.device.hubDeviceId}`);
582
+ break;
583
+ case 190:
584
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with` +
585
+ ` server, Or command format is invalid, statusCode: ${statusCode}`);
586
+ break;
587
+ case 100:
588
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
589
+ break;
590
+ case 200:
591
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
592
+ break;
593
+ case 400:
594
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
595
+ + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
596
+ break;
597
+ case 401:
598
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
599
+ + `but the request has not been authenticated, statusCode: ${statusCode}`);
600
+ break;
601
+ case 403:
602
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
603
+ + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
604
+ break;
605
+ case 404:
606
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
607
+ + `statusCode: ${statusCode}`);
608
+ break;
609
+ case 406:
610
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
611
+ + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
612
+ break;
613
+ case 415:
614
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
615
+ + `header that is not supported by the server, statusCode: ${statusCode}`);
616
+ break;
617
+ case 422:
618
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
619
+ + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
620
+ break;
621
+ case 429:
622
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
623
+ + `requests allowed for a given time window, statusCode: ${statusCode}`);
624
+ break;
625
+ case 500:
626
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
627
+ + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
628
+ break;
629
+ default:
630
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
631
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
632
+ }
633
+ }
634
+
635
+ /**
636
+ * Logging for Device
637
+ */
638
+ infoLog(...log: any[]): void {
639
+ if (this.enablingDeviceLogging()) {
640
+ this.log.info(String(...log));
641
+ }
642
+ }
643
+
644
+ successLog(...log: any[]): void {
645
+ if (this.enablingDeviceLogging()) {
646
+ this.log.success(String(...log));
647
+ }
648
+ }
649
+
650
+ debugSuccessLog(...log: any[]): void {
651
+ if (this.enablingDeviceLogging()) {
652
+ if (this.deviceLogging?.includes('debug')) {
653
+ this.log.success('[DEBUG]', String(...log));
654
+ }
655
+ }
656
+ }
657
+
658
+ warnLog(...log: any[]): void {
659
+ if (this.enablingDeviceLogging()) {
660
+ this.log.warn(String(...log));
661
+ }
662
+ }
663
+
664
+ debugWarnLog(...log: any[]): void {
665
+ if (this.enablingDeviceLogging()) {
666
+ if (this.deviceLogging?.includes('debug')) {
667
+ this.log.warn('[DEBUG]', String(...log));
668
+ }
669
+ }
670
+ }
671
+
672
+ errorLog(...log: any[]): void {
673
+ if (this.enablingDeviceLogging()) {
674
+ this.log.error(String(...log));
675
+ }
676
+ }
677
+
678
+ debugErrorLog(...log: any[]): void {
679
+ if (this.enablingDeviceLogging()) {
680
+ if (this.deviceLogging?.includes('debug')) {
681
+ this.log.error('[DEBUG]', String(...log));
682
+ }
683
+ }
684
+ }
685
+
686
+ debugLog(...log: any[]): void {
687
+ if (this.enablingDeviceLogging()) {
688
+ if (this.deviceLogging === 'debug') {
689
+ this.log.info('[DEBUG]', String(...log));
690
+ } else {
691
+ this.log.debug(String(...log));
692
+ }
693
+ }
694
+ }
695
+
696
+ enablingDeviceLogging(): boolean {
697
+ return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
698
+ }
699
+ }