@switchbot/homebridge-switchbot 3.5.0-beta.6 → 3.5.0-beta.61

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