@switchbot/homebridge-switchbot 3.7.1-beta.11 → 3.7.1-beta.13

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