@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10

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