@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,17 +2,23 @@
2
2
  *
3
3
  * waterdetector.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
- import { deviceBase } from './device.js';
6
- import { Subject, interval, skipWhile } from 'rxjs';
7
- import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
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 { waterLeakDetectorServiceData } from '../types/bledevicestatus.js';
13
- import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
14
- import type { waterLeakDetectorStatus } from '../types/devicestatus.js';
15
- import type { waterLeakDetectorWebhookContext } 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 { waterLeakDetectorServiceData } from '../types/bledevicestatus.js'
10
+ import type { device } from '../types/devicelist.js'
11
+ import type { waterLeakDetectorStatus } from '../types/devicestatus.js'
12
+ import type { waterLeakDetectorWebhookContext } 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 { interval, skipWhile, Subject } from 'rxjs'
20
+
21
+ import { deviceBase } from './device.js'
16
22
 
17
23
  /**
18
24
  * Platform Accessory
@@ -23,209 +29,212 @@ export class WaterDetector extends deviceBase {
23
29
  // Services
24
30
  private Battery: {
25
31
  Name: CharacteristicValue
26
- Service: Service;
27
- BatteryLevel: CharacteristicValue;
28
- StatusLowBattery: CharacteristicValue;
29
- ChargingState: CharacteristicValue;
30
- };
32
+ Service: Service
33
+ BatteryLevel: CharacteristicValue
34
+ StatusLowBattery: CharacteristicValue
35
+ ChargingState: CharacteristicValue
36
+ }
31
37
 
32
38
  private LeakSensor?: {
33
- Name: CharacteristicValue;
34
- Service: Service;
35
- StatusActive: CharacteristicValue;
36
- LeakDetected: CharacteristicValue;
37
- };
39
+ Name: CharacteristicValue
40
+ Service: Service
41
+ StatusActive: CharacteristicValue
42
+ LeakDetected: CharacteristicValue
43
+ }
38
44
 
39
45
  // OpenAPI
40
- deviceStatus!: waterLeakDetectorStatus;
46
+ deviceStatus!: waterLeakDetectorStatus
41
47
 
42
- //Webhook
43
- webhookContext!: waterLeakDetectorWebhookContext;
48
+ // Webhook
49
+ webhookContext!: waterLeakDetectorWebhookContext
44
50
 
45
51
  // BLE
46
- serviceData!: waterLeakDetectorServiceData;
52
+ serviceData!: waterLeakDetectorServiceData
47
53
 
48
54
  // Updates
49
- WaterDetectorUpdateInProgress!: boolean;
50
- doWaterDetectorUpdate: Subject<void>;
55
+ WaterDetectorUpdateInProgress!: boolean
56
+ doWaterDetectorUpdate: Subject<void>
51
57
 
52
58
  constructor(
53
59
  readonly platform: SwitchBotPlatform,
54
60
  accessory: PlatformAccessory,
55
61
  device: device & devicesConfig,
56
62
  ) {
57
- super(platform, accessory, device);
63
+ super(platform, accessory, device)
58
64
  // Set category
59
- accessory.category = this.hap.Categories.SENSOR;
65
+ accessory.category = this.hap.Categories.SENSOR
60
66
 
61
67
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
- this.doWaterDetectorUpdate = new Subject();
63
- this.WaterDetectorUpdateInProgress = false;
68
+ this.doWaterDetectorUpdate = new Subject()
69
+ this.WaterDetectorUpdateInProgress = false
64
70
 
65
71
  // Initialize Battery Service
66
- accessory.context.Battery = accessory.context.Battery ?? {};
72
+ accessory.context.Battery = accessory.context.Battery ?? {}
67
73
  this.Battery = {
68
74
  Name: `${accessory.displayName} Battery`,
69
75
  Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
70
76
  BatteryLevel: accessory.context.BatteryLevel ?? 100,
71
77
  StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
72
78
  ChargingState: accessory.context.ChargingState ?? this.hap.Characteristic.ChargingState.NOT_CHARGEABLE,
73
- };
74
- accessory.context.Battery = this.Battery as object;
79
+ }
80
+ accessory.context.Battery = this.Battery as object
75
81
 
76
82
  // Initialize Battery Characteristic
77
- this.Battery.Service
78
- .setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name)
79
- .setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE)
80
- .getCharacteristic(this.hap.Characteristic.BatteryLevel)
81
- .onGet(() => {
82
- return this.Battery.StatusLowBattery;
83
- });
84
-
85
- this.Battery.Service
86
- .getCharacteristic(this.hap.Characteristic.StatusLowBattery)
87
- .onGet(() => {
88
- return this.Battery.StatusLowBattery;
89
- });
83
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name).setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE).getCharacteristic(this.hap.Characteristic.BatteryLevel).onGet(() => {
84
+ return this.Battery.StatusLowBattery
85
+ })
86
+
87
+ this.Battery.Service.getCharacteristic(this.hap.Characteristic.StatusLowBattery).onGet(() => {
88
+ return this.Battery.StatusLowBattery
89
+ })
90
90
 
91
91
  // Initialize Leak Sensor Service
92
92
  if (device.waterdetector?.hide_leak) {
93
93
  if (this.LeakSensor) {
94
- this.debugLog('Removing Leak Sensor Service');
95
- this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor) as Service;
96
- accessory.removeService(this.LeakSensor.Service);
94
+ this.debugLog('Removing Leak Sensor Service')
95
+ this.LeakSensor.Service = this.accessory.getService(this.hap.Service.LeakSensor) as Service
96
+ accessory.removeService(this.LeakSensor.Service)
97
97
  } else {
98
- this.debugLog('Leak Sensor Service Not Found');
98
+ this.debugLog('Leak Sensor Service Not Found')
99
99
  }
100
100
  } else {
101
- accessory.context.LeakSensor = accessory.context.LeakSensor ?? {};
101
+ accessory.context.LeakSensor = accessory.context.LeakSensor ?? {}
102
102
  this.LeakSensor = {
103
103
  Name: `${accessory.displayName} Leak Sensor`,
104
104
  Service: accessory.getService(this.hap.Service.LeakSensor) ?? this.accessory.addService(this.hap.Service.LeakSensor) as Service,
105
105
  StatusActive: accessory.context.StatusActive ?? false,
106
106
  LeakDetected: accessory.context.LeakDetected ?? this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED,
107
- };
108
- accessory.context.LeakSensor = this.LeakSensor as object;
107
+ }
108
+ accessory.context.LeakSensor = this.LeakSensor as object
109
109
 
110
110
  // Initialize LeakSensor Characteristic
111
- this.LeakSensor!.Service
112
- .setCharacteristic(this.hap.Characteristic.Name, this.LeakSensor.Name)
113
- .setCharacteristic(this.hap.Characteristic.StatusActive, true)
114
- .getCharacteristic(this.hap.Characteristic.LeakDetected)
115
- .onGet(() => {
116
- return this.LeakSensor!.LeakDetected;
117
- });
111
+ this.LeakSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, this.LeakSensor.Name).setCharacteristic(this.hap.Characteristic.StatusActive, true).getCharacteristic(this.hap.Characteristic.LeakDetected).onGet(() => {
112
+ return this.LeakSensor!.LeakDetected
113
+ })
118
114
  }
119
115
 
120
116
  // Retrieve initial values and updateHomekit
121
- this.debugLog('Retrieve initial values and update Homekit');
122
- this.refreshStatus();
117
+ try {
118
+ this.debugLog('Retrieve initial values and update Homekit')
119
+ this.refreshStatus()
120
+ } catch (e: any) {
121
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
122
+ }
123
123
 
124
- //regisiter webhook event handler
125
- this.debugLog('Registering Webhook Event Handler');
126
- this.registerWebhook();
124
+ // regisiter webhook event handler if enabled
125
+ try {
126
+ this.debugLog('Registering Webhook Event Handler')
127
+ this.registerWebhook()
128
+ } catch (e: any) {
129
+ this.errorLog(`failed to registerWebhook, Error: ${e}`)
130
+ }
131
+
132
+ // regisiter platform BLE event handler if enabled
133
+ try {
134
+ this.debugLog('Registering Platform BLE Event Handler')
135
+ this.registerPlatformBLE()
136
+ } catch (e: any) {
137
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
138
+ }
127
139
 
128
140
  // Start an update interval
129
141
  interval(this.deviceRefreshRate * 1000)
130
142
  .pipe(skipWhile(() => this.WaterDetectorUpdateInProgress))
131
143
  .subscribe(async () => {
132
- await this.debugLog(`update interval: ${this.deviceRefreshRate * 1000} seconds`);
133
- await this.refreshStatus();
134
- });
144
+ await this.debugLog(`update interval: ${this.deviceRefreshRate * 1000} seconds`)
145
+ await this.refreshStatus()
146
+ })
135
147
  }
136
148
 
137
149
  async BLEparseStatus(): Promise<void> {
138
- await this.debugLog('BLEparseStatus');
139
- await this.debugLog(`(state, status, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.status}, ${this.serviceData.battery}),`
140
- + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
150
+ await this.debugLog('BLEparseStatus')
151
+ await this.debugLog(`(state, status, battery) = BLE: (${this.serviceData.state}, ${this.serviceData.status}, ${this.serviceData.battery}), current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`)
141
152
 
142
- //LeakSensor
153
+ // LeakSensor
143
154
  if (this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
144
-
145
155
  // StatusActive
146
- this.LeakSensor.StatusActive = this.serviceData.state;
147
- await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
156
+ this.LeakSensor.StatusActive = this.serviceData.state
157
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`)
148
158
 
149
159
  // LeakDetected
150
- this.LeakSensor.LeakDetected = this.serviceData.status;
151
- this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
160
+ this.LeakSensor.LeakDetected = this.serviceData.status
161
+ this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`)
152
162
  }
153
163
  // BatteryLevel
154
- this.Battery.BatteryLevel = this.serviceData.battery;
155
- await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
164
+ this.Battery.BatteryLevel = this.serviceData.battery
165
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
156
166
  // StatusLowBattery
157
167
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
158
- ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
159
- await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
168
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
169
+ : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
170
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`)
160
171
  }
161
172
 
162
173
  async openAPIparseStatus(): Promise<void> {
163
- await this.debugLog('openAPIparseStatus');
164
- await this.debugLog(`(status, battery) = OpenAPI: (${this.deviceStatus.status}, ${this.deviceStatus.battery}),`
165
- + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
174
+ await this.debugLog('openAPIparseStatus')
175
+ await this.debugLog(`(status, battery) = OpenAPI: (${this.deviceStatus.status}, ${this.deviceStatus.battery}), current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`)
166
176
 
167
- //LeakSensor
177
+ // LeakSensor
168
178
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
169
-
170
179
  // StatusActive
171
- this.LeakSensor.StatusActive = this.deviceStatus.battery === 0 ? false : true;
172
- await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
180
+ this.LeakSensor.StatusActive = this.deviceStatus.battery !== 0
181
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`)
173
182
 
174
183
  // LeakDetected
175
- this.LeakSensor.LeakDetected = this.deviceStatus.status;
176
- this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
184
+ this.LeakSensor.LeakDetected = this.deviceStatus.status
185
+ this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`)
177
186
  }
178
187
 
179
188
  // BatteryLevel
180
- this.Battery.BatteryLevel = this.deviceStatus.battery;
181
- await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
189
+ this.Battery.BatteryLevel = this.deviceStatus.battery
190
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
182
191
 
183
192
  // StatusLowBattery
184
193
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
185
- ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
186
- await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
194
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
195
+ : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
196
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`)
187
197
 
188
198
  // FirmwareVersion
189
199
  if (this.deviceStatus.version) {
190
- const version = this.deviceStatus.version.toString();
191
- await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`);
192
- const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
200
+ const version = this.deviceStatus.version.toString()
201
+ await this.debugLog(`FirmwareVersion: ${version.replace(/^V|-.*$/g, '')}`)
202
+ const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
193
203
  this.accessory
194
204
  .getService(this.hap.Service.AccessoryInformation)!
195
205
  .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
196
206
  .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
197
207
  .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
198
- .updateValue(deviceVersion);
199
- this.accessory.context.version = deviceVersion;
200
- await this.debugSuccessLog(`version: ${this.accessory.context.version}`);
208
+ .updateValue(deviceVersion)
209
+ this.accessory.context.version = deviceVersion
210
+ await this.debugSuccessLog(`version: ${this.accessory.context.version}`)
201
211
  }
202
212
  }
203
213
 
204
214
  async parseStatusWebhook(): Promise<void> {
205
- await this.debugLog('parseStatusWebhook');
206
- await this.debugLog(`(detectionState, battery) = Webhook: (${this.webhookContext.detectionState}, ${this.webhookContext.battery}),`
207
- + ` current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`);
215
+ await this.debugLog('parseStatusWebhook')
216
+ await this.debugLog(`(detectionState, battery) = Webhook: (${this.webhookContext.detectionState}, ${this.webhookContext.battery}), current:(${this.LeakSensor?.LeakDetected}, ${this.Battery.BatteryLevel})`)
208
217
 
209
- //LeakSensor
218
+ // LeakSensor
210
219
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
211
-
212
220
  // StatusActive
213
- this.LeakSensor.StatusActive = this.webhookContext.detectionState ? true : false;
214
- await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`);
221
+ this.LeakSensor.StatusActive = !!this.webhookContext.detectionState
222
+ await this.debugLog(`StatusActive: ${this.LeakSensor.StatusActive}`)
215
223
 
216
224
  // LeakDetected
217
- this.LeakSensor.LeakDetected = this.webhookContext.detectionState;
218
- await this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`);
225
+ this.LeakSensor.LeakDetected = this.webhookContext.detectionState
226
+ await this.debugLog(`LeakDetected: ${this.LeakSensor.LeakDetected}`)
219
227
  }
220
228
 
221
229
  // BatteryLevel
222
- this.Battery.BatteryLevel = this.webhookContext.battery;
223
- await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
230
+ this.Battery.BatteryLevel = this.webhookContext.battery
231
+ await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
224
232
 
225
233
  // StatusLowBattery
226
234
  this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
227
- ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
228
- await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
235
+ ? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
236
+ : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
237
+ await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`)
229
238
  }
230
239
 
231
240
  /**
@@ -233,93 +242,97 @@ export class WaterDetector extends deviceBase {
233
242
  */
234
243
  async refreshStatus(): Promise<void> {
235
244
  if (!this.device.enableCloudService && this.OpenAPI) {
236
- await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
237
- } else if (this.BLE || this.config.options?.BLE) {
238
- await this.BLERefreshStatus();
245
+ await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`)
246
+ } else if (this.BLE) {
247
+ await this.BLERefreshStatus()
239
248
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
240
- await this.openAPIRefreshStatus();
249
+ await this.openAPIRefreshStatus()
241
250
  } else {
242
- await this.offlineOff();
243
- await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
251
+ await this.offlineOff()
252
+ await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`)
244
253
  }
245
254
  }
246
255
 
247
256
  async BLERefreshStatus(): Promise<void> {
248
- await this.debugLog('BLERefreshStatus');
257
+ await this.debugLog('BLERefreshStatus')
258
+ const switchbot = await this.switchbotBLE()
259
+ if (switchbot === undefined) {
260
+ await this.BLERefreshConnection(switchbot)
261
+ } else {
262
+ // Start to monitor advertisement packets
263
+ (async () => {
264
+ // Start to monitor advertisement packets
265
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData
266
+ // Update HomeKit
267
+ if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
268
+ this.serviceData = serviceData
269
+ await this.BLEparseStatus()
270
+ await this.updateHomeKitCharacteristics()
271
+ } else {
272
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
273
+ await this.BLERefreshConnection(switchbot)
274
+ }
275
+ })()
276
+ }
277
+ }
278
+
279
+ async registerPlatformBLE(): Promise<void> {
280
+ await this.debugLog('registerPlatformBLE')
249
281
  if (this.config.options?.BLE) {
250
- await this.debugLog('is listening to Platform BLE.');
251
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
252
- await this.debugLog(`bleMac: ${this.device.bleMac}`);
282
+ await this.debugLog('is listening to Platform BLE.')
283
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
284
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
253
285
  this.platform.bleEventHandler[this.device.bleMac] = async (context: waterLeakDetectorServiceData) => {
254
286
  try {
255
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
256
- this.serviceData = context;
257
- await this.BLEparseStatus();
258
- await this.updateHomeKitCharacteristics();
287
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
288
+ this.serviceData = context
289
+ await this.BLEparseStatus()
290
+ await this.updateHomeKitCharacteristics()
259
291
  } catch (e: any) {
260
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
292
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
261
293
  }
262
- };
263
- } else {
264
- await this.debugLog('is using Device BLE Scanning.');
265
- const switchbot = await this.switchbotBLE();
266
- if (switchbot === undefined) {
267
- await this.BLERefreshConnection(switchbot);
268
- } else {
269
- // Start to monitor advertisement packets
270
- (async () => {
271
- // Start to monitor advertisement packets
272
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData;
273
- // Update HomeKit
274
- if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
275
- this.serviceData = serviceData;
276
- await this.BLEparseStatus();
277
- await this.updateHomeKitCharacteristics();
278
- } else {
279
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
280
- await this.BLERefreshConnection(switchbot);
281
- }
282
- })();
283
294
  }
295
+ } else {
296
+ await this.debugLog('is not listening to Platform BLE')
284
297
  }
285
298
  }
286
299
 
287
300
  async openAPIRefreshStatus(): Promise<void> {
288
- await this.debugLog('openAPIRefreshStatus');
301
+ await this.debugLog('openAPIRefreshStatus')
289
302
  try {
290
- const { body, statusCode } = await this.deviceRefreshStatus();
291
- const deviceStatus: any = await body.json();
292
- await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
303
+ const { body, statusCode } = await this.deviceRefreshStatus()
304
+ const deviceStatus: any = await body.json()
305
+ await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
293
306
  if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
294
- await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
295
- this.deviceStatus = deviceStatus.body;
296
- await this.openAPIparseStatus();
297
- await this.updateHomeKitCharacteristics();
307
+ await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
308
+ this.deviceStatus = deviceStatus.body
309
+ await this.openAPIparseStatus()
310
+ await this.updateHomeKitCharacteristics()
298
311
  } else {
299
- await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
300
- await this.debugWarnLog(statusCode, deviceStatus);
312
+ await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
313
+ await this.debugWarnLog(statusCode, deviceStatus)
301
314
  }
302
315
  } catch (e: any) {
303
- await this.apiError(e);
304
- await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
316
+ await this.apiError(e)
317
+ await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
305
318
  }
306
319
  }
307
320
 
308
321
  async registerWebhook() {
309
322
  if (this.device.webhook) {
310
- await this.debugLog('is listening webhook.');
323
+ await this.debugLog('is listening webhook.')
311
324
  this.platform.webhookEventHandler[this.device.deviceId] = async (context: waterLeakDetectorWebhookContext) => {
312
325
  try {
313
- await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
314
- this.webhookContext = context;
315
- await this.parseStatusWebhook();
316
- await this.updateHomeKitCharacteristics();
326
+ await this.debugLog(`received Webhook: ${JSON.stringify(context)}`)
327
+ this.webhookContext = context
328
+ await this.parseStatusWebhook()
329
+ await this.updateHomeKitCharacteristics()
317
330
  } catch (e: any) {
318
- await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
331
+ await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
319
332
  }
320
- };
333
+ }
321
334
  } else {
322
- await this.debugLog('is not listening webhook.');
335
+ await this.debugLog('is not listening webhook.')
323
336
  }
324
337
  }
325
338
 
@@ -329,43 +342,39 @@ export class WaterDetector extends deviceBase {
329
342
  async updateHomeKitCharacteristics(): Promise<void> {
330
343
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
331
344
  // StatusActive
332
- await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.StatusActive,
333
- this.LeakSensor.StatusActive, 'StatusActive');
345
+ await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.StatusActive, this.LeakSensor.StatusActive, 'StatusActive')
334
346
  // LeakDetected
335
- await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.LeakDetected,
336
- this.LeakSensor.LeakDetected, 'LeakDetected');
347
+ await this.updateCharacteristic(this.LeakSensor.Service, this.hap.Characteristic.LeakDetected, this.LeakSensor.LeakDetected, 'LeakDetected')
337
348
  }
338
349
  // BatteryLevel
339
- await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
340
- this.Battery.BatteryLevel, 'BatteryLevel');
350
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel')
341
351
  // StatusLowBattery
342
- await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
343
- this.Battery.StatusLowBattery, 'StatusLowBattery');
352
+ await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery')
344
353
  }
345
354
 
346
355
  async BLERefreshConnection(switchbot: any): Promise<void> {
347
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
356
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
348
357
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
349
- await this.warnLog('Using OpenAPI Connection to Refresh Status');
350
- await this.openAPIRefreshStatus();
358
+ await this.warnLog('Using OpenAPI Connection to Refresh Status')
359
+ await this.openAPIRefreshStatus()
351
360
  }
352
361
  }
353
362
 
354
363
  async offlineOff(): Promise<void> {
355
364
  if (this.device.offline) {
356
365
  if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
357
- this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, false);
358
- this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED);
366
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, false)
367
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, this.hap.Characteristic.LeakDetected.LEAK_NOT_DETECTED)
359
368
  }
360
369
  }
361
370
  }
362
371
 
363
- async apiError(e: any): Promise < void> {
364
- if(!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
365
- this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
366
- this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e);
372
+ async apiError(e: any): Promise<void> {
373
+ if (!this.device.waterdetector?.hide_leak && this.LeakSensor?.Service) {
374
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
375
+ this.LeakSensor.Service.updateCharacteristic(this.hap.Characteristic.LeakDetected, e)
367
376
  }
368
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
369
- this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
377
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
378
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e)
370
379
  }
371
380
  }
@@ -1,47 +1,48 @@
1
- import { HomebridgePluginUiServer } from '@homebridge/plugin-ui-utils';
2
- import fs from 'fs';
1
+ import fs from 'node:fs'
2
+
3
+ import { HomebridgePluginUiServer } from '@homebridge/plugin-ui-utils'
3
4
 
4
5
  class PluginUiServer extends HomebridgePluginUiServer {
5
6
  constructor() {
6
- super();
7
+ super()
7
8
  /*
8
9
  A native method getCachedAccessories() was introduced in config-ui-x v4.37.0
9
10
  The following is for users who have a lower version of config-ui-x
10
11
  */
11
12
  this.onRequest('getCachedAccessories', () => {
12
13
  try {
13
- const plugin = 'homebridge-switchbot';
14
- const devicesToReturn = [];
14
+ const plugin = 'homebridge-switchbot'
15
+ const devicesToReturn = []
15
16
 
16
17
  // The path and file of the cached accessories
17
- const accFile = this.homebridgeStoragePath + '/accessories/cachedAccessories';
18
+ const accFile = `${this.homebridgeStoragePath}/accessories/cachedAccessories`
18
19
 
19
20
  // Check the file exists
20
21
  if (fs.existsSync(accFile)) {
21
22
  // read the cached accessories file
22
- const cachedAccessories: any[] = JSON.parse(fs.readFileSync(accFile, 'utf8'));
23
+ const cachedAccessories: any[] = JSON.parse(fs.readFileSync(accFile, 'utf8'))
23
24
 
24
25
  cachedAccessories.forEach((accessory: any) => {
25
26
  // Check the accessory is from this plugin
26
27
  if (accessory.plugin === plugin) {
27
28
  // Add the cached accessory to the array
28
- devicesToReturn.push(accessory.accessory as never);
29
+ devicesToReturn.push(accessory.accessory as never)
29
30
  }
30
- });
31
+ })
31
32
  }
32
33
  // Return the array
33
- return devicesToReturn;
34
+ return devicesToReturn
34
35
  } catch {
35
36
  // Just return an empty accessory list in case of any errors
36
- return [];
37
+ return []
37
38
  }
38
- });
39
- this.ready();
39
+ })
40
+ this.ready()
40
41
  }
41
42
  }
42
43
 
43
44
  function startPluginUiServer(): PluginUiServer {
44
- return new PluginUiServer();
45
+ return new PluginUiServer()
45
46
  }
46
47
 
47
- startPluginUiServer();
48
+ startPluginUiServer()
package/src/index.ts CHANGED
@@ -2,13 +2,12 @@
2
2
  *
3
3
  * index.ts: @switchbot/homebridge-switchbot plugin registration.
4
4
  */
5
- import { SwitchBotPlatform } from './platform.js';
6
- import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
5
+ import type { API } from 'homebridge'
7
6
 
8
- import type { API } from 'homebridge';
7
+ import { SwitchBotPlatform } from './platform.js'
8
+ import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
9
9
 
10
10
  // Register our platform with homebridge.
11
11
  export default (api: API): void => {
12
-
13
- api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SwitchBotPlatform);
14
- };
12
+ api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, SwitchBotPlatform)
13
+ }