@switchbot/homebridge-switchbot 3.7.1-beta.9 → 3.8.0

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