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