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