@switchbot/homebridge-switchbot 3.5.1-beta.2 → 3.5.1-beta.4

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 (187) hide show
  1. package/dist/device/blindtilt.d.ts +15 -6
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +203 -255
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts +9 -4
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +196 -218
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts +9 -4
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +202 -247
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +10 -5
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +329 -378
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.d.ts +9 -4
  18. package/dist/device/contact.d.ts.map +1 -1
  19. package/dist/device/contact.js +99 -142
  20. package/dist/device/contact.js.map +1 -1
  21. package/dist/device/curtain.d.ts +19 -7
  22. package/dist/device/curtain.d.ts.map +1 -1
  23. package/dist/device/curtain.js +203 -264
  24. package/dist/device/curtain.js.map +1 -1
  25. package/dist/device/device.d.ts +28 -32
  26. package/dist/device/device.d.ts.map +1 -1
  27. package/dist/device/device.js +186 -225
  28. package/dist/device/device.js.map +1 -1
  29. package/dist/device/fan.d.ts +9 -4
  30. package/dist/device/fan.d.ts.map +1 -1
  31. package/dist/device/fan.js +198 -227
  32. package/dist/device/fan.js.map +1 -1
  33. package/dist/device/hub.d.ts +9 -5
  34. package/dist/device/hub.d.ts.map +1 -1
  35. package/dist/device/hub.js +106 -231
  36. package/dist/device/hub.js.map +1 -1
  37. package/dist/device/humidifier.d.ts +12 -4
  38. package/dist/device/humidifier.d.ts.map +1 -1
  39. package/dist/device/humidifier.js +192 -232
  40. package/dist/device/humidifier.js.map +1 -1
  41. package/dist/device/iosensor.d.ts +9 -4
  42. package/dist/device/iosensor.d.ts.map +1 -1
  43. package/dist/device/iosensor.js +91 -157
  44. package/dist/device/iosensor.js.map +1 -1
  45. package/dist/device/lightstrip.d.ts +10 -5
  46. package/dist/device/lightstrip.d.ts.map +1 -1
  47. package/dist/device/lightstrip.js +287 -367
  48. package/dist/device/lightstrip.js.map +1 -1
  49. package/dist/device/lock.d.ts +10 -5
  50. package/dist/device/lock.d.ts.map +1 -1
  51. package/dist/device/lock.js +171 -216
  52. package/dist/device/lock.js.map +1 -1
  53. package/dist/device/meter.d.ts +9 -4
  54. package/dist/device/meter.d.ts.map +1 -1
  55. package/dist/device/meter.js +93 -158
  56. package/dist/device/meter.js.map +1 -1
  57. package/dist/device/meterplus.d.ts +9 -4
  58. package/dist/device/meterplus.d.ts.map +1 -1
  59. package/dist/device/meterplus.js +94 -155
  60. package/dist/device/meterplus.js.map +1 -1
  61. package/dist/device/motion.d.ts +9 -4
  62. package/dist/device/motion.d.ts.map +1 -1
  63. package/dist/device/motion.js +84 -142
  64. package/dist/device/motion.js.map +1 -1
  65. package/dist/device/plug.d.ts +9 -4
  66. package/dist/device/plug.d.ts.map +1 -1
  67. package/dist/device/plug.js +99 -124
  68. package/dist/device/plug.js.map +1 -1
  69. package/dist/device/robotvacuumcleaner.d.ts +9 -6
  70. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  71. package/dist/device/robotvacuumcleaner.js +170 -253
  72. package/dist/device/robotvacuumcleaner.js.map +1 -1
  73. package/dist/device/waterdetector.d.ts +12 -4
  74. package/dist/device/waterdetector.d.ts.map +1 -1
  75. package/dist/device/waterdetector.js +102 -128
  76. package/dist/device/waterdetector.js.map +1 -1
  77. package/dist/irdevice/airconditioner.d.ts +2 -1
  78. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  79. package/dist/irdevice/airconditioner.js.map +1 -1
  80. package/dist/irdevice/airpurifier.d.ts +2 -1
  81. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  82. package/dist/irdevice/airpurifier.js.map +1 -1
  83. package/dist/irdevice/camera.d.ts +2 -1
  84. package/dist/irdevice/camera.d.ts.map +1 -1
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +2 -1
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js.map +1 -1
  89. package/dist/irdevice/irdevice.d.ts +2 -1
  90. package/dist/irdevice/irdevice.d.ts.map +1 -1
  91. package/dist/irdevice/irdevice.js +1 -1
  92. package/dist/irdevice/irdevice.js.map +1 -1
  93. package/dist/irdevice/light.d.ts +2 -1
  94. package/dist/irdevice/light.d.ts.map +1 -1
  95. package/dist/irdevice/light.js.map +1 -1
  96. package/dist/irdevice/other.d.ts +2 -1
  97. package/dist/irdevice/other.d.ts.map +1 -1
  98. package/dist/irdevice/other.js.map +1 -1
  99. package/dist/irdevice/tv.d.ts +2 -1
  100. package/dist/irdevice/tv.d.ts.map +1 -1
  101. package/dist/irdevice/tv.js.map +1 -1
  102. package/dist/irdevice/vacuumcleaner.d.ts +2 -1
  103. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  104. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  105. package/dist/irdevice/waterheater.d.ts +2 -1
  106. package/dist/irdevice/waterheater.d.ts.map +1 -1
  107. package/dist/irdevice/waterheater.js.map +1 -1
  108. package/dist/platform.d.ts +5 -7
  109. package/dist/platform.d.ts.map +1 -1
  110. package/dist/platform.js +51 -34
  111. package/dist/platform.js.map +1 -1
  112. package/dist/settings.d.ts +49 -191
  113. package/dist/settings.d.ts.map +1 -1
  114. package/dist/settings.js +19 -0
  115. package/dist/settings.js.map +1 -1
  116. package/dist/types/bledevicestatus.d.ts +279 -0
  117. package/dist/types/bledevicestatus.d.ts.map +1 -0
  118. package/dist/types/bledevicestatus.js +2 -0
  119. package/dist/types/bledevicestatus.js.map +1 -0
  120. package/dist/types/devicelist.d.ts +89 -0
  121. package/dist/types/devicelist.d.ts.map +1 -0
  122. package/dist/types/devicelist.js +6 -0
  123. package/dist/types/devicelist.js.map +1 -0
  124. package/dist/types/deviceresponse.d.ts +13 -0
  125. package/dist/types/deviceresponse.d.ts.map +1 -0
  126. package/dist/types/deviceresponse.js +2 -0
  127. package/dist/types/deviceresponse.js.map +1 -0
  128. package/dist/types/devicestatus.d.ts +149 -0
  129. package/dist/types/devicestatus.d.ts.map +1 -0
  130. package/dist/types/devicestatus.js +3 -0
  131. package/dist/types/devicestatus.js.map +1 -0
  132. package/dist/types/devicewebhookstatus.d.ts +156 -0
  133. package/dist/types/devicewebhookstatus.d.ts.map +1 -0
  134. package/dist/types/devicewebhookstatus.js +2 -0
  135. package/dist/types/devicewebhookstatus.js.map +1 -0
  136. package/dist/types/irdevicelist.d.ts +10 -0
  137. package/dist/types/irdevicelist.d.ts.map +1 -0
  138. package/dist/types/irdevicelist.js +2 -0
  139. package/dist/types/irdevicelist.js.map +1 -0
  140. package/dist/types/pushbody.d.ts +6 -0
  141. package/dist/types/pushbody.d.ts.map +1 -0
  142. package/dist/types/pushbody.js +2 -0
  143. package/dist/types/pushbody.js.map +1 -0
  144. package/dist/utils.d.ts +25 -0
  145. package/dist/utils.d.ts.map +1 -1
  146. package/dist/utils.js +28 -0
  147. package/dist/utils.js.map +1 -1
  148. package/package.json +10 -10
  149. package/src/device/blindtilt.ts +224 -264
  150. package/src/device/bot.ts +208 -221
  151. package/src/device/ceilinglight.ts +210 -257
  152. package/src/device/colorbulb.ts +334 -397
  153. package/src/device/contact.ts +111 -146
  154. package/src/device/curtain.ts +220 -283
  155. package/src/device/device.ts +194 -245
  156. package/src/device/fan.ts +214 -230
  157. package/src/device/hub.ts +126 -246
  158. package/src/device/humidifier.ts +216 -243
  159. package/src/device/iosensor.ts +114 -166
  160. package/src/device/lightstrip.ts +302 -377
  161. package/src/device/lock.ts +187 -221
  162. package/src/device/meter.ts +116 -177
  163. package/src/device/meterplus.ts +117 -174
  164. package/src/device/motion.ts +99 -144
  165. package/src/device/plug.ts +110 -128
  166. package/src/device/robotvacuumcleaner.ts +189 -252
  167. package/src/device/waterdetector.ts +120 -138
  168. package/src/irdevice/airconditioner.ts +2 -1
  169. package/src/irdevice/airpurifier.ts +2 -1
  170. package/src/irdevice/camera.ts +2 -1
  171. package/src/irdevice/fan.ts +2 -1
  172. package/src/irdevice/irdevice.ts +3 -2
  173. package/src/irdevice/light.ts +2 -1
  174. package/src/irdevice/other.ts +2 -1
  175. package/src/irdevice/tv.ts +2 -1
  176. package/src/irdevice/vacuumcleaner.ts +2 -1
  177. package/src/irdevice/waterheater.ts +2 -1
  178. package/src/platform.ts +52 -34
  179. package/src/settings.ts +30 -239
  180. package/src/types/bledevicestatus.ts +305 -0
  181. package/src/types/devicelist.ts +124 -0
  182. package/src/types/deviceresponse.ts +15 -0
  183. package/src/types/devicestatus.ts +178 -0
  184. package/src/types/devicewebhookstatus.ts +196 -0
  185. package/src/types/irdevicelist.ts +15 -0
  186. package/src/types/pushbody.ts +9 -0
  187. package/src/utils.ts +33 -0
@@ -3,11 +3,16 @@
3
3
  * plug.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
+ import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
6
7
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
7
8
 
9
+ import type { devicesConfig } from '../settings.js';
10
+ import type { device } from '../types/devicelist.js';
8
11
  import type { SwitchBotPlatform } from '../platform.js';
9
12
  import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
- import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
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';
11
16
  export class Plug extends deviceBase {
12
17
  // Services
13
18
  private Outlet: {
@@ -54,6 +59,9 @@ export class Plug extends deviceBase {
54
59
  // Update Homekit
55
60
  this.updateHomeKitCharacteristics();
56
61
 
62
+ //regisiter webhook event handler
63
+ this.registerWebhook();
64
+
57
65
  // Start an update interval
58
66
  interval(this.deviceRefreshRate * 1000)
59
67
  .pipe(skipWhile(() => this.plugUpdateInProgress))
@@ -61,9 +69,6 @@ export class Plug extends deviceBase {
61
69
  await this.refreshStatus();
62
70
  });
63
71
 
64
- //regisiter webhook event handler
65
- this.registerWebhook(accessory, device);
66
-
67
72
  // Watch for Plug change events
68
73
  // We put in a debounce of 100ms so we don't make duplicate calls
69
74
  this.doPlugUpdate
@@ -77,43 +82,30 @@ export class Plug extends deviceBase {
77
82
  try {
78
83
  await this.pushChanges();
79
84
  } catch (e: any) {
80
- this.apiError(e);
81
- this.errorLog(`${device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
82
- + ` Error Message: ${JSON.stringify(e.message)}`);
85
+ await this.apiError(e);
86
+ await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
83
87
  }
84
88
  this.plugUpdateInProgress = false;
85
89
  });
86
90
  }
87
91
 
88
- async BLEparseStatus(serviceData: serviceData): Promise<void> {
89
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
90
- // State
91
- switch (serviceData.state) {
92
- case 'on':
93
- this.Outlet.On = true;
94
- break;
95
- default:
96
- this.Outlet.On = false;
97
- }
98
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
92
+ async BLEparseStatus(serviceData: plugMiniUSServiceData | plugMiniJPServiceData): Promise<void> {
93
+ await this.debugLog('BLEparseStatus');
94
+ // On
95
+ this.Outlet.On = serviceData.state === 'on' ? true : false;
96
+ await this.debugLog(`On: ${this.Outlet.On}`);
99
97
  }
100
98
 
101
- async openAPIparseStatus(deviceStatus: deviceStatus) {
102
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
103
- switch (deviceStatus.body.power) {
104
- case 'on':
105
- this.Outlet.On = true;
106
- break;
107
- default:
108
- this.Outlet.On = false;
109
- }
110
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
111
-
99
+ async openAPIparseStatus(deviceStatus: plugStatus | plugMiniStatus) {
100
+ await this.debugLog('openAPIparseStatus');
101
+ // On
102
+ this.Outlet.On = deviceStatus.power === 'on' ? true : false;
103
+ await this.debugLog(`On: ${this.Outlet.On}`);
112
104
  // Firmware Version
113
- const version = deviceStatus.body.version?.toString();
114
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
115
- if (deviceStatus.body.version) {
116
- const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
105
+ const version = deviceStatus.version.toString();
106
+ await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
107
+ if (deviceStatus.version) {
108
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
117
109
  this.accessory
118
110
  .getService(this.hap.Service.AccessoryInformation)!
119
111
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
@@ -121,29 +113,36 @@ export class Plug extends deviceBase {
121
113
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
122
114
  .updateValue(deviceVersion);
123
115
  this.accessory.context.deviceVersion = deviceVersion;
124
- this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
116
+ await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
125
117
  }
126
118
  }
127
119
 
120
+ async parseStatusWebhook(context: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext): Promise<void> {
121
+ await this.debugLog('parseStatusWebhook');
122
+ await this.debugLog(`(powerState) = Webhook: (${context.powerState}), current:(${this.Outlet.On})`);
123
+ // On
124
+ this.Outlet.On = context.powerState === 'ON' ? true : false;
125
+ await this.debugLog(`On: ${this.Outlet.On}`);
126
+ }
127
+
128
128
  /**
129
129
  * Asks the SwitchBot API for the latest device information
130
130
  */
131
131
  async refreshStatus(): Promise<void> {
132
132
  if (!this.device.enableCloudService && this.OpenAPI) {
133
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
133
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
134
134
  } else if (this.BLE) {
135
135
  await this.BLERefreshStatus();
136
136
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
137
137
  await this.openAPIRefreshStatus();
138
138
  } else {
139
139
  await this.offlineOff();
140
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
141
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
140
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
142
141
  }
143
142
  }
144
143
 
145
144
  async BLERefreshStatus(): Promise<void> {
146
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
145
+ await this.debugLog('BLERefreshStatus');
147
146
  const switchbot = await this.switchbotBLE();
148
147
 
149
148
  if (switchbot === undefined) {
@@ -152,13 +151,14 @@ export class Plug extends deviceBase {
152
151
  // Start to monitor advertisement packets
153
152
  (async () => {
154
153
  // Start to monitor advertisement packets
155
- const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
154
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
156
155
  // Update HomeKit
157
- if (serviceData.model !== '' && serviceData.modelName !== '') {
156
+ if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
157
+ && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
158
158
  await this.BLEparseStatus(serviceData);
159
159
  await this.updateHomeKitCharacteristics();
160
160
  } else {
161
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
161
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
162
162
  await this.BLERefreshConnection(switchbot);
163
163
  }
164
164
  })();
@@ -166,41 +166,40 @@ export class Plug extends deviceBase {
166
166
  }
167
167
 
168
168
  async openAPIRefreshStatus(): Promise<void> {
169
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
169
+ await this.debugLog('openAPIRefreshStatus');
170
170
  try {
171
171
  const { body, statusCode } = await this.deviceRefreshStatus();
172
172
  const deviceStatus: any = await body.json();
173
- await this.refreshStatusCodes(statusCode, deviceStatus);;
174
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
175
- await this.successfulRefreshStatus(statusCode, deviceStatus);
176
- await this.openAPIparseStatus(deviceStatus);
173
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
174
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
175
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
176
+ await this.openAPIparseStatus(deviceStatus.body);
177
177
  await this.updateHomeKitCharacteristics();
178
178
  } else {
179
- await this.statusCodes(statusCode, deviceStatus);
179
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
180
+ await this.debugWarnLog(statusCode, deviceStatus);
180
181
  }
181
182
  } catch (e: any) {
182
183
  await this.apiError(e);
183
- await this.openAPIRefreshError(e);
184
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
184
185
  }
185
186
  }
186
187
 
187
- async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
188
- if (device.webhook) {
189
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
190
- this.platform.webhookEventHandler[device.deviceId] = async (context) => {
188
+ async registerWebhook() {
189
+ if (this.device.webhook) {
190
+ await this.debugLog('is listening webhook.');
191
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
192
+ | plugMiniJPWebhookContext) => {
191
193
  try {
192
- this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
193
- const { powerState } = context;
194
- const { On } = this.Outlet;
195
- this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState) = Webhook: (${powerState}), current:(${On})`);
196
- this.Outlet.On = powerState === 'ON' ? true : false;
197
- this.updateHomeKitCharacteristics();
194
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
195
+ await this.parseStatusWebhook(context);
196
+ await this.updateHomeKitCharacteristics();
198
197
  } catch (e: any) {
199
- this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
198
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
200
199
  }
201
200
  };
202
201
  } else {
203
- this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
202
+ await this.debugLog('is not listening webhook.');
204
203
  }
205
204
  }
206
205
 
@@ -216,14 +215,14 @@ export class Plug extends deviceBase {
216
215
 
217
216
  async pushChanges(): Promise<void> {
218
217
  if (!this.device.enableCloudService && this.OpenAPI) {
219
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
218
+ await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
220
219
  } else if (this.BLE) {
221
220
  await this.BLEpushChanges();
222
221
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
223
222
  await this.openAPIpushChanges();
224
223
  } else {
225
224
  await this.offlineOff();
226
- await this.pushChangeDisabled();
225
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
227
226
  }
228
227
  // Refresh the status from the API
229
228
  interval(15000)
@@ -235,83 +234,72 @@ export class Plug extends deviceBase {
235
234
  }
236
235
 
237
236
  async BLEpushChanges(): Promise<void> {
238
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
237
+ await this.debugLog('BLEpushChanges');
239
238
  if (this.Outlet.On !== this.accessory.context.On) {
240
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
241
- + ` On: ${this.Outlet.On} OnCached: ${this.accessory.context.On}`);
242
- const switchbot = await this.platform.connectBLE();
243
- // Convert to BLE Address
239
+ await this.debugLog(`BLEpushChanges On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
240
+ const switchbot = await this.platform.connectBLE(this.accessory, this.device);
244
241
  await this.convertBLEAddress();
245
- switchbot
246
- .discover({
247
- model: this.device.bleModel,
248
- id: this.device.bleMac,
249
- })
250
- .then(async (device_list: any) => {
251
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
252
- return await this.retryBLE({
253
- max: await this.maxRetryBLE(),
254
- fn: async () => {
255
- if (this.Outlet.On) {
256
- return await device_list[0].turnOn({ id: this.device.bleMac });
257
- } else {
258
- return await device_list[0].turnOff({ id: this.device.bleMac });
259
- }
260
- },
242
+ if (switchbot !== false) {
243
+ switchbot
244
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
245
+ .then(async (device_list: any) => {
246
+ await this.infoLog(`On: ${this.Outlet.On}`);
247
+ return await this.retryBLE({
248
+ max: await this.maxRetryBLE(),
249
+ fn: async () => {
250
+ if (this.Outlet.On) {
251
+ return await device_list[0].turnOn({ id: this.device.bleMac });
252
+ } else {
253
+ return await device_list[0].turnOff({ id: this.device.bleMac });
254
+ }
255
+ },
256
+ });
257
+ })
258
+ .then(async () => {
259
+ await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`);
260
+ await this.updateHomeKitCharacteristics();
261
+ })
262
+ .catch(async (e: any) => {
263
+ await this.apiError(e);
264
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
265
+ await this.BLEPushConnection();
261
266
  });
262
- })
263
- .then(() => {
264
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
265
- this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
266
- + `On: ${this.Outlet.On} sent over BLE, sent successfully`);
267
- this.Outlet.On = false;
268
- })
269
- .catch(async (e: any) => {
270
- this.apiError(e);
271
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
272
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
273
- await this.BLEPushConnection();
274
- });
267
+ } else {
268
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
269
+ await this.BLEPushConnection();
270
+ }
275
271
  } else {
276
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
277
- + ` On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
272
+ await this.debugLog(`No changes (BLEpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
278
273
  }
279
274
  }
280
275
 
281
276
  async openAPIpushChanges() {
282
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
277
+ await this.debugLog('openAPIpushChanges');
283
278
  if (this.Outlet.On !== this.accessory.context.On) {
284
- let command = '';
285
- if (this.Outlet.On) {
286
- command = 'turnOn';
287
- } else {
288
- command = 'turnOff';
289
- }
279
+ const command = this.Outlet.On ? 'turnOn' : 'turnOff';
290
280
  const bodyChange = JSON.stringify({
291
281
  command: `${command}`,
292
282
  parameter: 'default',
293
283
  commandType: 'command',
294
284
  });
295
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
285
+ await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
296
286
  try {
297
287
  const { body, statusCode } = await this.pushChangeRequest(bodyChange);
298
288
  const deviceStatus: any = await body.json();
299
- await this.pushStatusCodes(statusCode, deviceStatus);
300
- if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
301
- await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
289
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
290
+ if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
291
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
302
292
  await this.updateHomeKitCharacteristics();
303
293
  } else {
304
294
  await this.statusCode(statusCode);
305
295
  await this.statusCode(deviceStatus.statusCode);
306
296
  }
307
297
  } catch (e: any) {
308
- this.apiError(e);
309
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
310
- + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
298
+ await this.apiError(e);
299
+ await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
311
300
  }
312
301
  } else {
313
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
314
- + `On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
302
+ await this.debugLog(`No changes (openAPIpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
315
303
  }
316
304
  }
317
305
 
@@ -319,10 +307,10 @@ export class Plug extends deviceBase {
319
307
  * Handle requests to set the value of the "On" characteristic
320
308
  */
321
309
  async OnSet(value: CharacteristicValue): Promise<void> {
322
- if (this.Outlet.On === this.accessory.context.On) {
323
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
310
+ if (this.Outlet.On !== this.accessory.context.On) {
311
+ await this.infoLog(`Set On: ${value}`);
324
312
  } else {
325
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
313
+ await this.debugLog(`No Changes, On: ${value}`);
326
314
  }
327
315
 
328
316
  this.Outlet.On = value;
@@ -331,27 +319,21 @@ export class Plug extends deviceBase {
331
319
 
332
320
  async updateHomeKitCharacteristics(): Promise<void> {
333
321
  // On
334
- if (this.Outlet.On === undefined) {
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
336
- } else {
337
- this.accessory.context.On = this.Outlet.On;
338
- this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, this.Outlet.On);
339
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Outlet.On}`);
340
- }
322
+ await this.updateCharacteristic(this.Outlet.Service, this.hap.Characteristic.On,
323
+ this.Outlet.On, 'On');
341
324
  }
342
325
 
343
326
  async BLEPushConnection() {
344
327
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
345
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
328
+ await this.warnLog('Using OpenAPI Connection to Push Changes');
346
329
  await this.openAPIpushChanges();
347
330
  }
348
331
  }
349
332
 
350
333
  async BLERefreshConnection(switchbot: any): Promise<void> {
351
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
352
- + ` ${JSON.stringify(switchbot)}`);
334
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
353
335
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
354
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
336
+ await this.warnLog('Using OpenAPI Connection to Refresh Status');
355
337
  await this.openAPIRefreshStatus();
356
338
  }
357
339
  }