@switchbot/homebridge-switchbot 3.7.1-beta.2 → 3.7.1-beta.21

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 (199) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +14 -0
  3. package/assets/security-privacy-bluetooth.png +0 -0
  4. package/config.schema.json +74 -2
  5. package/dist/device/blindtilt.d.ts +21 -9
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +167 -96
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot-old.d.ts +132 -0
  10. package/dist/device/bot-old.d.ts.map +1 -0
  11. package/dist/device/bot-old.js +1482 -0
  12. package/dist/device/bot-old.js.map +1 -0
  13. package/dist/device/bot.d.ts +10 -9
  14. package/dist/device/bot.d.ts.map +1 -1
  15. package/dist/device/bot.js +186 -187
  16. package/dist/device/bot.js.map +1 -1
  17. package/dist/device/ceilinglight.d.ts +14 -13
  18. package/dist/device/ceilinglight.d.ts.map +1 -1
  19. package/dist/device/ceilinglight.js +87 -97
  20. package/dist/device/ceilinglight.js.map +1 -1
  21. package/dist/device/colorbulb.d.ts +13 -12
  22. package/dist/device/colorbulb.d.ts.map +1 -1
  23. package/dist/device/colorbulb.js +92 -105
  24. package/dist/device/colorbulb.js.map +1 -1
  25. package/dist/device/contact.d.ts +6 -5
  26. package/dist/device/contact.d.ts.map +1 -1
  27. package/dist/device/contact.js +71 -68
  28. package/dist/device/contact.js.map +1 -1
  29. package/dist/device/curtain.d.ts +17 -5
  30. package/dist/device/curtain.d.ts.map +1 -1
  31. package/dist/device/curtain.js +184 -118
  32. package/dist/device/curtain.js.map +1 -1
  33. package/dist/device/device.d.ts +18 -18
  34. package/dist/device/device.d.ts.map +1 -1
  35. package/dist/device/device.js +33 -36
  36. package/dist/device/device.js.map +1 -1
  37. package/dist/device/fan.d.ts +9 -8
  38. package/dist/device/fan.d.ts.map +1 -1
  39. package/dist/device/fan.js +94 -101
  40. package/dist/device/fan.js.map +1 -1
  41. package/dist/device/hub.d.ts +6 -5
  42. package/dist/device/hub.d.ts.map +1 -1
  43. package/dist/device/hub.js +69 -65
  44. package/dist/device/hub.js.map +1 -1
  45. package/dist/device/humidifier.d.ts +6 -5
  46. package/dist/device/humidifier.d.ts.map +1 -1
  47. package/dist/device/humidifier.js +89 -104
  48. package/dist/device/humidifier.js.map +1 -1
  49. package/dist/device/iosensor.d.ts +6 -5
  50. package/dist/device/iosensor.d.ts.map +1 -1
  51. package/dist/device/iosensor.js +72 -66
  52. package/dist/device/iosensor.js.map +1 -1
  53. package/dist/device/lightstrip.d.ts +7 -6
  54. package/dist/device/lightstrip.d.ts.map +1 -1
  55. package/dist/device/lightstrip.js +85 -99
  56. package/dist/device/lightstrip.js.map +1 -1
  57. package/dist/device/lock.d.ts +10 -9
  58. package/dist/device/lock.d.ts.map +1 -1
  59. package/dist/device/lock.js +89 -79
  60. package/dist/device/lock.js.map +1 -1
  61. package/dist/device/meter.d.ts +6 -5
  62. package/dist/device/meter.d.ts.map +1 -1
  63. package/dist/device/meter.js +72 -65
  64. package/dist/device/meter.js.map +1 -1
  65. package/dist/device/meterplus.d.ts +6 -5
  66. package/dist/device/meterplus.d.ts.map +1 -1
  67. package/dist/device/meterplus.js +72 -64
  68. package/dist/device/meterplus.js.map +1 -1
  69. package/dist/device/motion.d.ts +6 -5
  70. package/dist/device/motion.d.ts.map +1 -1
  71. package/dist/device/motion.js +65 -58
  72. package/dist/device/motion.js.map +1 -1
  73. package/dist/device/plug.d.ts +13 -12
  74. package/dist/device/plug.d.ts.map +1 -1
  75. package/dist/device/plug.js +68 -52
  76. package/dist/device/plug.js.map +1 -1
  77. package/dist/device/robotvacuumcleaner.d.ts +12 -11
  78. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  79. package/dist/device/robotvacuumcleaner.js +93 -82
  80. package/dist/device/robotvacuumcleaner.js.map +1 -1
  81. package/dist/device/waterdetector.d.ts +6 -5
  82. package/dist/device/waterdetector.d.ts.map +1 -1
  83. package/dist/device/waterdetector.js +73 -63
  84. package/dist/device/waterdetector.js.map +1 -1
  85. package/dist/homebridge-ui/server.js +2 -2
  86. package/dist/homebridge-ui/server.js.map +1 -1
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +0 -4
  89. package/dist/index.js.map +1 -1
  90. package/dist/irdevice/airconditioner.d.ts +8 -8
  91. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  92. package/dist/irdevice/airconditioner.js +61 -105
  93. package/dist/irdevice/airconditioner.js.map +1 -1
  94. package/dist/irdevice/airpurifier.d.ts +10 -10
  95. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  96. package/dist/irdevice/airpurifier.js +22 -42
  97. package/dist/irdevice/airpurifier.js.map +1 -1
  98. package/dist/irdevice/camera.d.ts +9 -9
  99. package/dist/irdevice/camera.d.ts.map +1 -1
  100. package/dist/irdevice/camera.js +15 -25
  101. package/dist/irdevice/camera.js.map +1 -1
  102. package/dist/irdevice/fan.d.ts +8 -8
  103. package/dist/irdevice/fan.d.ts.map +1 -1
  104. package/dist/irdevice/fan.js +21 -40
  105. package/dist/irdevice/fan.js.map +1 -1
  106. package/dist/irdevice/irdevice.d.ts +11 -11
  107. package/dist/irdevice/irdevice.d.ts.map +1 -1
  108. package/dist/irdevice/irdevice.js +14 -22
  109. package/dist/irdevice/irdevice.js.map +1 -1
  110. package/dist/irdevice/light.d.ts +9 -9
  111. package/dist/irdevice/light.d.ts.map +1 -1
  112. package/dist/irdevice/light.js +24 -47
  113. package/dist/irdevice/light.js.map +1 -1
  114. package/dist/irdevice/other.d.ts +9 -9
  115. package/dist/irdevice/other.d.ts.map +1 -1
  116. package/dist/irdevice/other.js +40 -83
  117. package/dist/irdevice/other.js.map +1 -1
  118. package/dist/irdevice/tv.d.ts +9 -9
  119. package/dist/irdevice/tv.d.ts.map +1 -1
  120. package/dist/irdevice/tv.js +29 -54
  121. package/dist/irdevice/tv.js.map +1 -1
  122. package/dist/irdevice/vacuumcleaner.d.ts +5 -5
  123. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  124. package/dist/irdevice/vacuumcleaner.js +11 -21
  125. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  126. package/dist/irdevice/waterheater.d.ts +5 -5
  127. package/dist/irdevice/waterheater.d.ts.map +1 -1
  128. package/dist/irdevice/waterheater.js +11 -22
  129. package/dist/irdevice/waterheater.js.map +1 -1
  130. package/dist/platform.d.ts +24 -23
  131. package/dist/platform.d.ts.map +1 -1
  132. package/dist/platform.js +509 -723
  133. package/dist/platform.js.map +1 -1
  134. package/dist/settings.d.ts +5 -2
  135. package/dist/settings.d.ts.map +1 -1
  136. package/dist/settings.js.map +1 -1
  137. package/dist/types/bledevicestatus.d.ts +9 -9
  138. package/dist/types/bledevicestatus.d.ts.map +1 -1
  139. package/dist/types/devicelist.d.ts +6 -6
  140. package/dist/types/devicelist.d.ts.map +1 -1
  141. package/dist/types/deviceresponse.d.ts +4 -4
  142. package/dist/types/deviceresponse.d.ts.map +1 -1
  143. package/dist/types/devicestatus.d.ts +2 -2
  144. package/dist/types/devicestatus.d.ts.map +1 -1
  145. package/dist/types/devicewebhookstatus.d.ts +5 -5
  146. package/dist/types/devicewebhookstatus.d.ts.map +1 -1
  147. package/dist/types/irdevicelist.d.ts +4 -4
  148. package/dist/types/irdevicelist.d.ts.map +1 -1
  149. package/dist/types/pushbody.d.ts +2 -2
  150. package/dist/types/pushbody.d.ts.map +1 -1
  151. package/dist/utils.d.ts +4 -4
  152. package/dist/utils.d.ts.map +1 -1
  153. package/dist/utils.js +9 -13
  154. package/dist/utils.js.map +1 -1
  155. package/eslint.config.js +48 -93
  156. package/package.json +64 -57
  157. package/src/device/blindtilt.ts +537 -456
  158. package/src/device/bot-old.ts +1540 -0
  159. package/src/device/bot.ts +732 -725
  160. package/src/device/ceilinglight.ts +359 -368
  161. package/src/device/colorbulb.ts +441 -453
  162. package/src/device/contact.ts +229 -226
  163. package/src/device/curtain.ts +526 -444
  164. package/src/device/device.ts +206 -208
  165. package/src/device/fan.ts +336 -345
  166. package/src/device/hub.ts +213 -214
  167. package/src/device/humidifier.ts +326 -347
  168. package/src/device/iosensor.ts +211 -209
  169. package/src/device/lightstrip.ts +391 -404
  170. package/src/device/lock.ts +292 -282
  171. package/src/device/meter.ts +212 -210
  172. package/src/device/meterplus.ts +212 -209
  173. package/src/device/motion.ts +198 -190
  174. package/src/device/plug.ts +203 -182
  175. package/src/device/robotvacuumcleaner.ts +280 -269
  176. package/src/device/waterdetector.ts +198 -189
  177. package/src/homebridge-ui/server.ts +16 -15
  178. package/src/index.ts +5 -6
  179. package/src/irdevice/airconditioner.ts +277 -326
  180. package/src/irdevice/airpurifier.ts +124 -143
  181. package/src/irdevice/camera.ts +58 -64
  182. package/src/irdevice/fan.ts +106 -123
  183. package/src/irdevice/irdevice.ts +116 -119
  184. package/src/irdevice/light.ts +119 -143
  185. package/src/irdevice/other.ts +417 -461
  186. package/src/irdevice/tv.ts +155 -177
  187. package/src/irdevice/vacuumcleaner.ts +54 -60
  188. package/src/irdevice/waterheater.ts +56 -63
  189. package/src/platform.ts +1491 -1681
  190. package/src/settings.ts +172 -169
  191. package/src/types/bledevicestatus.ts +252 -256
  192. package/src/types/devicelist.ts +79 -79
  193. package/src/types/deviceresponse.ts +12 -13
  194. package/src/types/devicestatus.ts +124 -124
  195. package/src/types/devicewebhookstatus.ts +126 -131
  196. package/src/types/irdevicelist.ts +10 -10
  197. package/src/types/pushbody.ts +5 -5
  198. package/src/utils.ts +64 -65
  199. package/tsconfig.json +10 -9
@@ -2,144 +2,163 @@
2
2
  *
3
3
  * plug.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
- import { deviceBase } from './device.js';
6
- import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
7
- import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
8
-
9
- import type { devicesConfig } from '../settings.js';
10
- import type { device } from '../types/devicelist.js';
11
- import type { SwitchBotPlatform } from '../platform.js';
12
- import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
13
- import type { plugStatus, plugMiniStatus } from '../types/devicestatus.js';
14
- import type { plugMiniUSServiceData, plugMiniJPServiceData } from '../types/bledevicestatus.js';
15
- import type { plugMiniJPWebhookContext, plugMiniUSWebhookContext, plugWebhookContext } from '../types/devicewebhookstatus.js';
5
+ import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
6
+
7
+ import type { SwitchBotPlatform } from '../platform.js'
8
+ import type { devicesConfig } from '../settings.js'
9
+ import type { plugMiniJPServiceData, plugMiniUSServiceData } from '../types/bledevicestatus.js'
10
+ import type { device } from '../types/devicelist.js'
11
+ import type { plugMiniStatus, plugStatus } from '../types/devicestatus.js'
12
+ import type { plugMiniJPWebhookContext, plugMiniUSWebhookContext, plugWebhookContext } from '../types/devicewebhookstatus.js'
13
+
14
+ /*
15
+ * For Testing Locally:
16
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
17
+ */
18
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
+ import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
20
+
21
+ import { deviceBase } from './device.js'
22
+
16
23
  export class Plug extends deviceBase {
17
24
  // Services
18
25
  private Outlet: {
19
- Name: CharacteristicValue;
20
- Service: Service;
21
- On: CharacteristicValue;
22
- };
26
+ Name: CharacteristicValue
27
+ Service: Service
28
+ On: CharacteristicValue
29
+ }
23
30
 
24
31
  // OpenAPI
25
- deviceStatus!: plugStatus | plugMiniStatus;
32
+ deviceStatus!: plugStatus | plugMiniStatus
26
33
 
27
- //Webhook
28
- webhookContext!: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext;
34
+ // Webhook
35
+ webhookContext!: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext
29
36
 
30
37
  // BLE
31
- serviceData!: plugMiniUSServiceData | plugMiniJPServiceData;
38
+ serviceData!: plugMiniUSServiceData | plugMiniJPServiceData
32
39
 
33
40
  // Updates
34
- plugUpdateInProgress!: boolean;
35
- doPlugUpdate!: Subject<void>;
41
+ plugUpdateInProgress!: boolean
42
+ doPlugUpdate!: Subject<void>
36
43
 
37
44
  constructor(
38
45
  readonly platform: SwitchBotPlatform,
39
46
  accessory: PlatformAccessory,
40
47
  device: device & devicesConfig,
41
48
  ) {
42
- super(platform, accessory, device);
49
+ super(platform, accessory, device)
43
50
  // Set category
44
- accessory.category = this.hap.Categories.OUTLET;
51
+ accessory.category = this.hap.Categories.OUTLET
45
52
 
46
53
  // this is subject we use to track when we need to POST changes to the SwitchBot API
47
- this.doPlugUpdate = new Subject();
48
- this.plugUpdateInProgress = false;
54
+ this.doPlugUpdate = new Subject()
55
+ this.plugUpdateInProgress = false
49
56
 
50
57
  // Initialize Outlet Service
51
- accessory.context.Outlet = accessory.context.Outlet ?? {};
58
+ accessory.context.Outlet = accessory.context.Outlet ?? {}
52
59
  this.Outlet = {
53
60
  Name: accessory.displayName,
54
61
  Service: accessory.getService(this.hap.Service.Outlet) ?? accessory.addService(this.hap.Service.Outlet) as Service,
55
62
  On: accessory.context.On || false,
56
- };
57
- accessory.context.Outlet = this.Outlet as object;
63
+ }
64
+ accessory.context.Outlet = this.Outlet as object
58
65
 
59
66
  // Initialize Outlet Characteristics
60
- this.Outlet.Service
61
- .setCharacteristic(this.hap.Characteristic.Name, this.Outlet.Name)
62
- .getCharacteristic(this.hap.Characteristic.On)
63
- .onGet(() => {
64
- return this.Outlet.On;
65
- })
66
- .onSet(this.OnSet.bind(this));
67
+ this.Outlet.Service.setCharacteristic(this.hap.Characteristic.Name, this.Outlet.Name).getCharacteristic(this.hap.Characteristic.On).onGet(() => {
68
+ return this.Outlet.On
69
+ }).onSet(this.OnSet.bind(this))
67
70
 
68
71
  // Retrieve initial values and updateHomekit
69
- this.debugLog('Retrieve initial values and update Homekit');
70
- this.refreshStatus();
72
+ try {
73
+ this.debugLog('Retrieve initial values and update Homekit')
74
+ this.refreshStatus()
75
+ } catch (e: any) {
76
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
77
+ }
71
78
 
72
- //regisiter webhook event handler
73
- this.debugLog('Registering Webhook Event Handler');
74
- this.registerWebhook();
79
+ // regisiter webhook event handler if enabled
80
+ try {
81
+ this.debugLog('Registering Webhook Event Handler')
82
+ this.registerWebhook()
83
+ } catch (e: any) {
84
+ this.errorLog(`failed to registerWebhook, Error: ${e}`)
85
+ }
86
+
87
+ // regisiter platform BLE event handler if enabled
88
+ try {
89
+ this.debugLog('Registering Platform BLE Event Handler')
90
+ this.registerPlatformBLE()
91
+ } catch (e: any) {
92
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
93
+ }
75
94
 
76
95
  // Start an update interval
77
96
  interval(this.deviceRefreshRate * 1000)
78
97
  .pipe(skipWhile(() => this.plugUpdateInProgress))
79
98
  .subscribe(async () => {
80
- await this.refreshStatus();
81
- });
99
+ await this.refreshStatus()
100
+ })
82
101
 
83
102
  // Watch for Plug change events
84
103
  // We put in a debounce of 100ms so we don't make duplicate calls
85
104
  this.doPlugUpdate
86
105
  .pipe(
87
106
  tap(() => {
88
- this.plugUpdateInProgress = true;
107
+ this.plugUpdateInProgress = true
89
108
  }),
90
109
  debounceTime(this.devicePushRate * 1000),
91
110
  )
92
111
  .subscribe(async () => {
93
112
  try {
94
- await this.pushChanges();
113
+ await this.pushChanges()
95
114
  } catch (e: any) {
96
- await this.apiError(e);
97
- await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
115
+ await this.apiError(e)
116
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
98
117
  }
99
- this.plugUpdateInProgress = false;
100
- });
118
+ this.plugUpdateInProgress = false
119
+ })
101
120
  }
102
121
 
103
122
  async BLEparseStatus(): Promise<void> {
104
- await this.debugLog('BLEparseStatus');
105
- await this.debugLog(`(powerState) = BLE: (${this.serviceData.state}), current:(${this.Outlet.On})`);
123
+ await this.debugLog('BLEparseStatus')
124
+ await this.debugLog(`(powerState) = BLE: (${this.serviceData.state}), current:(${this.Outlet.On})`)
106
125
 
107
126
  // On
108
- this.Outlet.On = this.serviceData.state === 'on' ? true : false;
109
- await this.debugLog(`On: ${this.Outlet.On}`);
127
+ this.Outlet.On = this.serviceData.state === 'on'
128
+ await this.debugLog(`On: ${this.Outlet.On}`)
110
129
  }
111
130
 
112
131
  async openAPIparseStatus() {
113
- await this.debugLog('openAPIparseStatus');
114
- await this.debugLog(`(powerState) = OpenAPI: (${this.deviceStatus.power}), current:(${this.Outlet.On})`);
132
+ await this.debugLog('openAPIparseStatus')
133
+ await this.debugLog(`(powerState) = OpenAPI: (${this.deviceStatus.power}), current:(${this.Outlet.On})`)
115
134
 
116
135
  // On
117
- this.Outlet.On = this.deviceStatus.power === 'on' ? true : false;
118
- await this.debugLog(`On: ${this.Outlet.On}`);
136
+ this.Outlet.On = this.deviceStatus.power === 'on'
137
+ await this.debugLog(`On: ${this.Outlet.On}`)
119
138
 
120
139
  // Firmware Version
121
140
  if (this.deviceStatus.version) {
122
- const version = this.deviceStatus.version.toString();
123
- await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
124
- const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
141
+ const version = this.deviceStatus.version.toString()
142
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`)
143
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
125
144
  this.accessory
126
145
  .getService(this.hap.Service.AccessoryInformation)!
127
146
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
128
147
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
129
148
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
130
- .updateValue(deviceVersion);
131
- this.accessory.context.version = deviceVersion;
132
- await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
149
+ .updateValue(deviceVersion)
150
+ this.accessory.context.version = deviceVersion
151
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`)
133
152
  }
134
153
  }
135
154
 
136
155
  async parseStatusWebhook(): Promise<void> {
137
- await this.debugLog('parseStatusWebhook');
138
- await this.debugLog(`(powerState) = Webhook: (${this.webhookContext.powerState}), current:(${this.Outlet.On})`);
156
+ await this.debugLog('parseStatusWebhook')
157
+ await this.debugLog(`(powerState) = Webhook: (${this.webhookContext.powerState}), current:(${this.Outlet.On})`)
139
158
 
140
159
  // On
141
- this.Outlet.On = this.webhookContext.powerState === 'ON' ? true : false;
142
- await this.debugLog(`On: ${this.Outlet.On}`);
160
+ this.Outlet.On = this.webhookContext.powerState === 'ON'
161
+ await this.debugLog(`On: ${this.Outlet.On}`)
143
162
  }
144
163
 
145
164
  /**
@@ -147,195 +166,198 @@ export class Plug extends deviceBase {
147
166
  */
148
167
  async refreshStatus(): Promise<void> {
149
168
  if (!this.device.enableCloudService && this.OpenAPI) {
150
- await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
151
- } else if (this.BLE || this.config.options?.BLE) {
152
- await this.BLERefreshStatus();
169
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`)
170
+ } else if (this.BLE) {
171
+ await this.BLERefreshStatus()
153
172
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
154
- await this.openAPIRefreshStatus();
173
+ await this.openAPIRefreshStatus()
155
174
  } else {
156
- await this.offlineOff();
157
- await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
175
+ await this.offlineOff()
176
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`)
158
177
  }
159
178
  }
160
179
 
161
180
  async BLERefreshStatus(): Promise<void> {
162
- await this.debugLog('BLERefreshStatus');
181
+ await this.debugLog('BLERefreshStatus')
182
+ const switchbot = await this.switchbotBLE()
183
+ if (switchbot === undefined) {
184
+ await this.BLERefreshConnection(switchbot)
185
+ } else {
186
+ // Start to monitor advertisement packets
187
+ (async () => {
188
+ // Start to monitor advertisement packets
189
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData
190
+ // Update HomeKit
191
+ if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
192
+ && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
193
+ this.serviceData = serviceData
194
+ await this.BLEparseStatus()
195
+ await this.updateHomeKitCharacteristics()
196
+ } else {
197
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
198
+ await this.BLERefreshConnection(switchbot)
199
+ }
200
+ })()
201
+ }
202
+ }
203
+
204
+ async registerPlatformBLE(): Promise<void> {
205
+ await this.debugLog('registerPlatformBLE')
163
206
  if (this.config.options?.BLE) {
164
- await this.debugLog('is listening to Platform BLE.');
165
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
166
- await this.debugLog(`bleMac: ${this.device.bleMac}`);
207
+ await this.debugLog('is listening to Platform BLE.')
208
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
209
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
167
210
  this.platform.bleEventHandler[this.device.bleMac] = async (context: plugMiniUSServiceData | plugMiniJPServiceData) => {
168
211
  try {
169
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
170
- this.serviceData = context;
171
- await this.BLEparseStatus();
172
- await this.updateHomeKitCharacteristics();
212
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
213
+ this.serviceData = context
214
+ await this.BLEparseStatus()
215
+ await this.updateHomeKitCharacteristics()
173
216
  } catch (e: any) {
174
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
217
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
175
218
  }
176
- };
177
- } else {
178
- await this.debugLog('is using Device BLE Scanning.');
179
- const switchbot = await this.switchbotBLE();
180
- if (switchbot === undefined) {
181
- await this.BLERefreshConnection(switchbot);
182
- } else {
183
- // Start to monitor advertisement packets
184
- (async () => {
185
- // Start to monitor advertisement packets
186
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
187
- // Update HomeKit
188
- if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
189
- && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
190
- this.serviceData = serviceData;
191
- await this.BLEparseStatus();
192
- await this.updateHomeKitCharacteristics();
193
- } else {
194
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
195
- await this.BLERefreshConnection(switchbot);
196
- }
197
- })();
198
219
  }
220
+ } else {
221
+ await this.debugLog('is not listening to Platform BLE')
199
222
  }
200
223
  }
201
224
 
202
225
  async openAPIRefreshStatus(): Promise<void> {
203
- await this.debugLog('openAPIRefreshStatus');
226
+ await this.debugLog('openAPIRefreshStatus')
204
227
  try {
205
- const { body, statusCode } = await this.deviceRefreshStatus();
206
- const deviceStatus: any = await body.json();
207
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
228
+ const { body, statusCode } = await this.deviceRefreshStatus()
229
+ const deviceStatus: any = await body.json()
230
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
208
231
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
209
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
210
- this.deviceStatus = deviceStatus.body;
211
- await this.openAPIparseStatus();
212
- await this.updateHomeKitCharacteristics();
232
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
233
+ this.deviceStatus = deviceStatus.body
234
+ await this.openAPIparseStatus()
235
+ await this.updateHomeKitCharacteristics()
213
236
  } else {
214
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
215
- await this.debugWarnLog(statusCode, deviceStatus);
237
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
238
+ await this.debugWarnLog(statusCode, deviceStatus)
216
239
  }
217
240
  } catch (e: any) {
218
- await this.apiError(e);
219
- await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
241
+ await this.apiError(e)
242
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
220
243
  }
221
244
  }
222
245
 
223
246
  async registerWebhook() {
224
247
  if (this.device.webhook) {
225
- await this.debugLog('is listening webhook.');
226
- this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
227
- | plugMiniJPWebhookContext) => {
248
+ await this.debugLog('is listening webhook.')
249
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext) => {
228
250
  try {
229
- await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
230
- this.webhookContext = context;
231
- await this.parseStatusWebhook();
232
- await this.updateHomeKitCharacteristics();
251
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`)
252
+ this.webhookContext = context
253
+ await this.parseStatusWebhook()
254
+ await this.updateHomeKitCharacteristics()
233
255
  } catch (e: any) {
234
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
256
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
235
257
  }
236
- };
258
+ }
237
259
  } else {
238
- await this.debugLog('is not listening webhook.');
260
+ await this.debugLog('is not listening webhook.')
239
261
  }
240
262
  }
241
263
 
242
264
  /**
243
265
  * Pushes the requested changes to the SwitchBot API
244
- * deviceType commandType Command command parameter Description
245
- * Plug - "command" "turnOff" "default" = set to OFF state
246
- * Plug - "command" "turnOn" "default" = set to ON state
247
- * Plug Mini (US/JP) - "command" turnOn default = set to ON state
248
- * Plug Mini (US/JP) - "command" turnOff default = set to OFF state
249
- * Plug Mini (US/JP) - "command" toggle default = toggle state
266
+ * deviceType commandType Command command parameter Description
267
+ * Plug - "command" "turnOff" "default" = set to OFF state
268
+ * Plug - "command" "turnOn" "default" = set to ON state
269
+ * Plug Mini (US/JP) - "command" "turnOn" "default" = set to ON state
270
+ * Plug Mini (US/JP) - "command" "turnOff" "default" = set to OFF state
271
+ * Plug Mini (US/JP) - "command" "toggle" "default" = toggle state
250
272
  */
251
273
 
252
274
  async pushChanges(): Promise<void> {
253
275
  if (!this.device.enableCloudService && this.OpenAPI) {
254
- await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
276
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`)
255
277
  } else if (this.BLE) {
256
- await this.BLEpushChanges();
278
+ await this.BLEpushChanges()
257
279
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
258
- await this.openAPIpushChanges();
280
+ await this.openAPIpushChanges()
259
281
  } else {
260
- await this.offlineOff();
261
- await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
282
+ await this.offlineOff()
283
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`)
262
284
  }
263
285
  // Refresh the status from the API
264
286
  interval(15000)
265
287
  .pipe(skipWhile(() => this.plugUpdateInProgress))
266
288
  .pipe(take(1))
267
289
  .subscribe(async () => {
268
- await this.refreshStatus();
269
- });
290
+ await this.refreshStatus()
291
+ })
270
292
  }
271
293
 
272
294
  async BLEpushChanges(): Promise<void> {
273
- await this.debugLog('BLEpushChanges');
295
+ await this.debugLog('BLEpushChanges')
274
296
  if (this.Outlet.On !== this.accessory.context.On) {
275
- await this.debugLog(`BLEpushChanges On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
276
- const switchbot = await this.platform.connectBLE(this.accessory, this.device);
277
- await this.convertBLEAddress();
297
+ await this.debugLog(`BLEpushChanges On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`)
298
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device)
299
+ await this.convertBLEAddress()
278
300
  if (switchbot !== false) {
279
301
  switchbot
280
302
  .discover({ model: this.device.bleModel, id: this.device.bleMac })
281
303
  .then(async (device_list: any) => {
282
- await this.infoLog(`On: ${this.Outlet.On}`);
304
+ await this.infoLog(`On: ${this.Outlet.On}`)
283
305
  return await this.retryBLE({
284
306
  max: await this.maxRetryBLE(),
285
307
  fn: async () => {
286
308
  if (this.Outlet.On) {
287
- return await device_list[0].turnOn({ id: this.device.bleMac });
309
+ return await device_list[0].turnOn({ id: this.device.bleMac })
288
310
  } else {
289
- return await device_list[0].turnOff({ id: this.device.bleMac });
311
+ return await device_list[0].turnOff({ id: this.device.bleMac })
290
312
  }
291
313
  },
292
- });
314
+ })
293
315
  })
294
316
  .then(async () => {
295
- await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`);
296
- await this.updateHomeKitCharacteristics();
317
+ await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`)
318
+ await this.updateHomeKitCharacteristics()
297
319
  })
298
320
  .catch(async (e: any) => {
299
- await this.apiError(e);
300
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
301
- await this.BLEPushConnection();
302
- });
321
+ await this.apiError(e)
322
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
323
+ await this.BLEPushConnection()
324
+ })
303
325
  } else {
304
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
305
- await this.BLEPushConnection();
326
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
327
+ await this.BLEPushConnection()
306
328
  }
307
329
  } else {
308
- await this.debugLog(`No changes (BLEpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
330
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`)
309
331
  }
310
332
  }
311
333
 
312
334
  async openAPIpushChanges() {
313
- await this.debugLog('openAPIpushChanges');
335
+ await this.debugLog('openAPIpushChanges')
314
336
  if (this.Outlet.On !== this.accessory.context.On) {
315
- const command = this.Outlet.On ? 'turnOn' : 'turnOff';
337
+ const command = this.Outlet.On ? 'turnOn' : 'turnOff'
316
338
  const bodyChange = JSON.stringify({
317
339
  command: `${command}`,
318
340
  parameter: 'default',
319
341
  commandType: 'command',
320
- });
321
- await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
342
+ })
343
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`)
322
344
  try {
323
- const { body, statusCode } = await this.pushChangeRequest(bodyChange);
324
- const deviceStatus: any = await body.json();
325
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
345
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange)
346
+ const deviceStatus: any = await body.json()
347
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
326
348
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
327
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
328
- await this.updateHomeKitCharacteristics();
349
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
350
+ await this.updateHomeKitCharacteristics()
329
351
  } else {
330
- await this.statusCode(statusCode);
331
- await this.statusCode(deviceStatus.statusCode);
352
+ await this.statusCode(statusCode)
353
+ await this.statusCode(deviceStatus.statusCode)
332
354
  }
333
355
  } catch (e: any) {
334
- await this.apiError(e);
335
- await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
356
+ await this.apiError(e)
357
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
336
358
  }
337
359
  } else {
338
- await this.debugLog(`No changes (openAPIpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
360
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`)
339
361
  }
340
362
  }
341
363
 
@@ -344,43 +366,42 @@ export class Plug extends deviceBase {
344
366
  */
345
367
  async OnSet(value: CharacteristicValue): Promise<void> {
346
368
  if (this.Outlet.On !== this.accessory.context.On) {
347
- await this.infoLog(`Set On: ${value}`);
369
+ await this.infoLog(`Set On: ${value}`)
348
370
  } else {
349
- await this.debugLog(`No Changes, On: ${value}`);
371
+ await this.debugLog(`No Changes, On: ${value}`)
350
372
  }
351
373
 
352
- this.Outlet.On = value;
353
- this.doPlugUpdate.next();
374
+ this.Outlet.On = value
375
+ this.doPlugUpdate.next()
354
376
  }
355
377
 
356
378
  async updateHomeKitCharacteristics(): Promise<void> {
357
379
  // On
358
- await this.updateCharacteristic(this.Outlet.Service, this.hap.Characteristic.On,
359
- this.Outlet.On, 'On');
380
+ await this.updateCharacteristic(this.Outlet.Service, this.hap.Characteristic.On, this.Outlet.On, 'On')
360
381
  }
361
382
 
362
383
  async BLEPushConnection() {
363
384
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
364
- await this.warnLog('Using OpenAPI Connection to Push Changes');
365
- await this.openAPIpushChanges();
385
+ await this.warnLog('Using OpenAPI Connection to Push Changes')
386
+ await this.openAPIpushChanges()
366
387
  }
367
388
  }
368
389
 
369
390
  async BLERefreshConnection(switchbot: any): Promise<void> {
370
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
391
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
371
392
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
372
- await this.warnLog('Using OpenAPI Connection to Refresh Status');
373
- await this.openAPIRefreshStatus();
393
+ await this.warnLog('Using OpenAPI Connection to Refresh Status')
394
+ await this.openAPIRefreshStatus()
374
395
  }
375
396
  }
376
397
 
377
398
  async offlineOff(): Promise<void> {
378
399
  if (this.device.offline) {
379
- this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, false);
400
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, false)
380
401
  }
381
402
  }
382
403
 
383
404
  async apiError(e: any): Promise<void> {
384
- this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, e);
405
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, e)
385
406
  }
386
407
  }