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