@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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