@switchbot/homebridge-switchbot 1.8.3-beta.0 → 1.10.1-beta.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 (122) hide show
  1. package/.eslintrc +14 -45
  2. package/.github/ISSUE_TEMPLATE/bug-report.yml +3 -3
  3. package/.github/ISSUE_TEMPLATE/feature-request.yml +2 -2
  4. package/.github/ISSUE_TEMPLATE/support-request.yml +3 -3
  5. package/.github/labeler.yml +1 -1
  6. package/.github/workflows/codeql-analysis.yml +29 -29
  7. package/.github/workflows/nodejs-beta.yml +1 -1
  8. package/.github/workflows/prerelease-beta.js +1 -1
  9. package/.github/workflows/stale.yml +1 -1
  10. package/.prettierrc +9 -0
  11. package/.vscode/extensions.json +2 -4
  12. package/.vscode/settings.json +14 -2
  13. package/CHANGELOG.md +16 -0
  14. package/README.md +100 -99
  15. package/SECURITY.md +2 -2
  16. package/config.schema.json +17 -0
  17. package/dist/devices/bots.d.ts +35 -32
  18. package/dist/devices/bots.d.ts.map +1 -1
  19. package/dist/devices/bots.js +168 -210
  20. package/dist/devices/bots.js.map +1 -1
  21. package/dist/devices/colorbulb.d.ts +35 -35
  22. package/dist/devices/colorbulb.d.ts.map +1 -1
  23. package/dist/devices/colorbulb.js +83 -108
  24. package/dist/devices/colorbulb.js.map +1 -1
  25. package/dist/devices/contact.d.ts +27 -25
  26. package/dist/devices/contact.d.ts.map +1 -1
  27. package/dist/devices/contact.js +110 -131
  28. package/dist/devices/contact.js.map +1 -1
  29. package/dist/devices/curtains.d.ts +29 -27
  30. package/dist/devices/curtains.d.ts.map +1 -1
  31. package/dist/devices/curtains.js +147 -182
  32. package/dist/devices/curtains.js.map +1 -1
  33. package/dist/devices/humidifiers.d.ts +31 -31
  34. package/dist/devices/humidifiers.d.ts.map +1 -1
  35. package/dist/devices/humidifiers.js +154 -181
  36. package/dist/devices/humidifiers.js.map +1 -1
  37. package/dist/devices/meters.d.ts +29 -28
  38. package/dist/devices/meters.d.ts.map +1 -1
  39. package/dist/devices/meters.js +98 -120
  40. package/dist/devices/meters.js.map +1 -1
  41. package/dist/devices/motion.d.ts +24 -23
  42. package/dist/devices/motion.d.ts.map +1 -1
  43. package/dist/devices/motion.js +97 -113
  44. package/dist/devices/motion.js.map +1 -1
  45. package/dist/devices/plugs.d.ts +16 -16
  46. package/dist/devices/plugs.d.ts.map +1 -1
  47. package/dist/devices/plugs.js +48 -71
  48. package/dist/devices/plugs.js.map +1 -1
  49. package/dist/index.d.ts +2 -2
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +3 -22
  52. package/dist/index.js.map +1 -1
  53. package/dist/irdevices/airconditioners.d.ts +7 -4
  54. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  55. package/dist/irdevices/airconditioners.js +72 -93
  56. package/dist/irdevices/airconditioners.js.map +1 -1
  57. package/dist/irdevices/airpurifiers.d.ts +3 -3
  58. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  59. package/dist/irdevices/airpurifiers.js +32 -51
  60. package/dist/irdevices/airpurifiers.js.map +1 -1
  61. package/dist/irdevices/cameras.d.ts +3 -3
  62. package/dist/irdevices/cameras.d.ts.map +1 -1
  63. package/dist/irdevices/cameras.js +19 -26
  64. package/dist/irdevices/cameras.js.map +1 -1
  65. package/dist/irdevices/fans.d.ts +3 -3
  66. package/dist/irdevices/fans.d.ts.map +1 -1
  67. package/dist/irdevices/fans.js +35 -44
  68. package/dist/irdevices/fans.js.map +1 -1
  69. package/dist/irdevices/lights.d.ts +3 -3
  70. package/dist/irdevices/lights.d.ts.map +1 -1
  71. package/dist/irdevices/lights.js +25 -32
  72. package/dist/irdevices/lights.js.map +1 -1
  73. package/dist/irdevices/others.d.ts +3 -3
  74. package/dist/irdevices/others.d.ts.map +1 -1
  75. package/dist/irdevices/others.js +21 -29
  76. package/dist/irdevices/others.js.map +1 -1
  77. package/dist/irdevices/tvs.d.ts +3 -3
  78. package/dist/irdevices/tvs.d.ts.map +1 -1
  79. package/dist/irdevices/tvs.js +66 -89
  80. package/dist/irdevices/tvs.js.map +1 -1
  81. package/dist/irdevices/vacuumcleaners.d.ts +3 -3
  82. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  83. package/dist/irdevices/vacuumcleaners.js +18 -24
  84. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  85. package/dist/irdevices/waterheaters.d.ts +3 -3
  86. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  87. package/dist/irdevices/waterheaters.js +19 -26
  88. package/dist/irdevices/waterheaters.js.map +1 -1
  89. package/dist/platform.d.ts +10 -10
  90. package/dist/platform.d.ts.map +1 -1
  91. package/dist/platform.js +163 -233
  92. package/dist/platform.js.map +1 -1
  93. package/dist/settings.d.ts +3 -1
  94. package/dist/settings.d.ts.map +1 -1
  95. package/dist/settings.js +4 -4
  96. package/dist/settings.js.map +1 -1
  97. package/homebridge-ui/public/index.html +231 -245
  98. package/homebridge-ui/server.js +2 -4
  99. package/nodemon.json +2 -4
  100. package/package.json +17 -12
  101. package/src/devices/bots.ts +411 -854
  102. package/src/devices/colorbulb.ts +187 -408
  103. package/src/devices/contact.ts +210 -376
  104. package/src/devices/curtains.ts +288 -563
  105. package/src/devices/humidifiers.ts +291 -537
  106. package/src/devices/meters.ts +189 -331
  107. package/src/devices/motion.ts +188 -322
  108. package/src/devices/plugs.ts +88 -160
  109. package/src/index.ts +5 -5
  110. package/src/irdevices/airconditioners.ts +139 -318
  111. package/src/irdevices/airpurifiers.ts +71 -170
  112. package/src/irdevices/cameras.ts +44 -103
  113. package/src/irdevices/fans.ts +70 -165
  114. package/src/irdevices/lights.ts +49 -104
  115. package/src/irdevices/others.ts +55 -131
  116. package/src/irdevices/tvs.ts +114 -247
  117. package/src/irdevices/vacuumcleaners.ts +44 -104
  118. package/src/irdevices/waterheaters.ts +49 -121
  119. package/src/platform.ts +341 -753
  120. package/src/settings.ts +8 -11
  121. package/tsconfig.json +5 -14
  122. package/.github/PULL_REQUEST_TEMPLATE/pull_requests.md +0 -27
@@ -1,8 +1,8 @@
1
- import * as rxjs from "rxjs";
2
- import * as operators from "rxjs/operators";
3
- import * as platform from "../platform";
4
- import * as homebridge from "homebridge";
5
- import * as settings from "../settings";
1
+ import { interval, Subject } from 'rxjs';
2
+ import { skipWhile } from 'rxjs/operators';
3
+ import { SwitchBotPlatform } from '../platform';
4
+ import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
5
+ import { DeviceURL, device, devicesConfig, serviceData, switchbot, deviceStatusResponse, deviceStatus } from '../settings';
6
6
 
7
7
  /**
8
8
  * Platform Accessory
@@ -11,29 +11,29 @@ import * as settings from "../settings";
11
11
  */
12
12
  export class Motion {
13
13
  // Services
14
- private service: homebridge.Service;
15
- private lightSensorService?: homebridge.Service;
16
- private batteryService?: homebridge.Service;
14
+ private service: Service;
15
+ private lightSensorService?: Service;
16
+ private batteryService?: Service;
17
17
 
18
18
  // Characteristic Values
19
- MotionDetected!: homebridge.CharacteristicValue;
20
- CurrentAmbientLightLevel!: homebridge.CharacteristicValue;
21
- BatteryLevel?: homebridge.CharacteristicValue;
22
- StatusLowBattery?: homebridge.CharacteristicValue;
19
+ MotionDetected!: CharacteristicValue;
20
+ CurrentAmbientLightLevel!: CharacteristicValue;
21
+ BatteryLevel?: CharacteristicValue;
22
+ StatusLowBattery?: CharacteristicValue;
23
23
 
24
24
  // OpenAPI Others
25
- deviceStatus!: settings.deviceStatusResponse;
26
- moveDetected: settings.deviceStatus["moveDetected"];
27
- brightness: settings.deviceStatus["brightness"];
25
+ deviceStatus!: deviceStatusResponse;
26
+ moveDetected: deviceStatus['moveDetected'];
27
+ brightness: deviceStatus['brightness'];
28
28
 
29
29
  // BLE Others
30
30
  connected?: boolean;
31
- switchbot!: settings.switchbot;
31
+ switchbot!: switchbot;
32
32
  SwitchToOpenAPI?: boolean;
33
- serviceData!: settings.serviceData;
34
- battery!: settings.serviceData["battery"];
35
- movement!: settings.serviceData["movement"];
36
- lightLevel!: settings.serviceData["lightLevel"];
33
+ serviceData!: serviceData;
34
+ battery!: serviceData['battery'];
35
+ movement!: serviceData['movement'];
36
+ lightLevel!: serviceData['lightLevel'];
37
37
 
38
38
  // Config
39
39
  set_minLux!: number;
@@ -44,13 +44,9 @@ export class Motion {
44
44
 
45
45
  // Updates
46
46
  motionUbpdateInProgress!: boolean;
47
- doMotionUpdate!: rxjs.Subject<void>;
47
+ doMotionUpdate!: Subject<void>;
48
48
 
49
- constructor(
50
- private readonly platform: platform.SwitchBotPlatform,
51
- private accessory: homebridge.PlatformAccessory,
52
- public device: settings.device & settings.devicesConfig,
53
- ) {
49
+ constructor(private readonly platform: SwitchBotPlatform, private accessory: PlatformAccessory, public device: device & devicesConfig) {
54
50
  // default placeholders
55
51
  this.logs(device);
56
52
  this.scan(device);
@@ -58,8 +54,15 @@ export class Motion {
58
54
  this.config(device);
59
55
  this.MotionDetected = false;
60
56
 
57
+ // Motion Config
58
+ this.debugLog(
59
+ `Motion Sensor: ${this.accessory.displayName} Config: (ble: ${device.ble}, offline: ${device.offline},` +
60
+ ` hide_lightsensor: ${device.motion?.hide_lightsensor},` +
61
+ ` set_minLux: ${this.device.motion?.set_minLux}, set_maxLux: ${this.device.motion?.set_maxLux})`,
62
+ );
63
+
61
64
  // this is subject we use to track when we need to POST changes to the SwitchBot API
62
- this.doMotionUpdate = new rxjs.Subject();
65
+ this.doMotionUpdate = new Subject();
63
66
  this.motionUbpdateInProgress = false;
64
67
 
65
68
  // Retrieve initial values and updateHomekit
@@ -68,21 +71,13 @@ export class Motion {
68
71
  // set accessory information
69
72
  accessory
70
73
  .getService(this.platform.Service.AccessoryInformation)!
71
- .setCharacteristic(this.platform.Characteristic.Manufacturer, "SwitchBot")
72
- .setCharacteristic(
73
- this.platform.Characteristic.Model,
74
- "SWITCHBOT-WOMOTION-W1101500",
75
- )
76
- .setCharacteristic(
77
- this.platform.Characteristic.SerialNumber,
78
- device.deviceId!,
79
- );
74
+ .setCharacteristic(this.platform.Characteristic.Manufacturer, 'SwitchBot')
75
+ .setCharacteristic(this.platform.Characteristic.Model, 'SWITCHBOT-WOMOTION-W1101500')
76
+ .setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId!);
80
77
 
81
78
  // get the Battery service if it exists, otherwise create a new Motion service
82
79
  // you can create multiple services for each accessory
83
- (this.service =
84
- accessory.getService(this.platform.Service.MotionSensor) ||
85
- accessory.addService(this.platform.Service.MotionSensor)),
80
+ (this.service = accessory.getService(this.platform.Service.MotionSensor) || accessory.addService(this.platform.Service.MotionSensor)),
86
81
  `${accessory.displayName} Motion Sensor`;
87
82
 
88
83
  // To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
@@ -91,77 +86,48 @@ export class Motion {
91
86
 
92
87
  // set the service name, this is what is displayed as the default name on the Home app
93
88
  // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
94
- this.service.setCharacteristic(
95
- this.platform.Characteristic.Name,
96
- accessory.displayName,
97
- );
89
+ this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
98
90
 
99
91
  // each service must implement at-minimum the "required characteristics" for the given service type
100
92
  // see https://developers.homebridge.io/#/service/MotionSensor
101
93
 
102
94
  // Light Sensor Service
103
95
  if (device.motion?.hide_lightsensor) {
104
- this.debugLog(
105
- `Motion Sensor: ${accessory.displayName} Removing Light Sensor Service`,
106
- );
107
- this.lightSensorService = this.accessory.getService(
108
- this.platform.Service.LightSensor,
109
- );
96
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Light Sensor Service`);
97
+ this.lightSensorService = this.accessory.getService(this.platform.Service.LightSensor);
110
98
  accessory.removeService(this.lightSensorService!);
111
99
  } else if (!this.lightSensorService) {
112
- this.debugLog(
113
- `Motion Sensor: ${accessory.displayName} Add Light Sensor Service`,
114
- );
100
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Light Sensor Service`);
115
101
  (this.lightSensorService =
116
- this.accessory.getService(this.platform.Service.LightSensor) ||
117
- this.accessory.addService(this.platform.Service.LightSensor)),
102
+ this.accessory.getService(this.platform.Service.LightSensor) || this.accessory.addService(this.platform.Service.LightSensor)),
118
103
  `${accessory.displayName} Light Sensor`;
119
104
 
120
- this.lightSensorService.setCharacteristic(
121
- this.platform.Characteristic.Name,
122
- `${accessory.displayName} Light Sensor`,
123
- );
105
+ this.lightSensorService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Light Sensor`);
124
106
  } else {
125
- this.debugLog(
126
- `Motion Sensor: ${accessory.displayName} Light Sensor Service Not Added`,
127
- );
107
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Light Sensor Service Not Added`);
128
108
  }
129
109
 
130
110
  // Battery Service
131
111
  if (!device.ble) {
132
- this.debugLog(
133
- `Motion Sensor: ${accessory.displayName} Removing Battery Service`,
134
- );
135
- this.batteryService = this.accessory.getService(
136
- this.platform.Service.Battery,
137
- );
112
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Removing Battery Service`);
113
+ this.batteryService = this.accessory.getService(this.platform.Service.Battery);
138
114
  accessory.removeService(this.batteryService!);
139
115
  } else if (device.ble && !this.batteryService) {
140
- this.debugLog(
141
- `Motion Sensor: ${accessory.displayName} Add Battery Service`,
142
- );
143
- (this.batteryService =
144
- this.accessory.getService(this.platform.Service.Battery) ||
145
- this.accessory.addService(this.platform.Service.Battery)),
116
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Add Battery Service`);
117
+ (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
146
118
  `${accessory.displayName} Battery`;
147
119
 
148
- this.batteryService.setCharacteristic(
149
- this.platform.Characteristic.Name,
150
- `${accessory.displayName} Battery`,
151
- );
120
+ this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
152
121
  } else {
153
- this.debugLog(
154
- `Motion Sensor: ${accessory.displayName} Battery Service Not Added`,
155
- );
122
+ this.debugLog(`Motion Sensor: ${accessory.displayName} Battery Service Not Added`);
156
123
  }
157
124
 
158
125
  // Retrieve initial values and updateHomekit
159
126
  this.updateHomeKitCharacteristics();
160
127
 
161
128
  // Start an update interval
162
- rxjs
163
- .interval(this.deviceRefreshRate * 1000)
164
- .pipe(operators.skipWhile(() => this.motionUbpdateInProgress))
129
+ interval(this.deviceRefreshRate * 1000)
130
+ .pipe(skipWhile(() => this.motionUbpdateInProgress))
165
131
  .subscribe(() => {
166
132
  this.refreshStatus();
167
133
  });
@@ -179,20 +145,16 @@ export class Motion {
179
145
  }
180
146
 
181
147
  private async BLEparseStatus() {
182
- this.debugLog(
183
- `Motion Sensor: ${this.accessory.displayName} BLE parseStatus`,
184
- );
148
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE parseStatus`);
185
149
  // Movement
186
150
  this.MotionDetected = this.movement!;
187
- this.debugLog(
188
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
189
- );
151
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
190
152
  // Light Level
191
153
  if (!this.device.motion?.hide_lightsensor) {
192
154
  this.set_minLux = this.minLux();
193
155
  this.set_maxLux = this.maxLux();
194
156
  switch (this.lightLevel) {
195
- case "dark":
157
+ case 'dark':
196
158
  case 1:
197
159
  this.CurrentAmbientLightLevel = this.set_minLux;
198
160
  break;
@@ -210,15 +172,11 @@ export class Motion {
210
172
  }
211
173
  this.BatteryLevel = this.battery!;
212
174
  if (this.BatteryLevel < 10) {
213
- this.StatusLowBattery =
214
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
175
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
215
176
  } else {
216
- this.StatusLowBattery =
217
- this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
177
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
218
178
  }
219
- this.debugLog(
220
- `Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`,
221
- );
179
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
222
180
  }
223
181
 
224
182
  private async openAPIparseStatus() {
@@ -226,29 +184,23 @@ export class Motion {
226
184
  this.SwitchToOpenAPI = false;
227
185
  }
228
186
  if (this.platform.config.credentials?.openToken) {
229
- this.debugLog(
230
- `Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`,
231
- );
187
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI parseStatus`);
232
188
  // Motion State
233
189
  this.MotionDetected = this.moveDetected!;
234
- this.debugLog(
235
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
236
- );
190
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
237
191
  // Light Level
238
192
  if (!this.device.motion?.hide_lightsensor) {
239
193
  this.set_minLux = this.minLux();
240
194
  this.set_maxLux = this.maxLux();
241
195
  switch (this.brightness) {
242
- case "dim":
196
+ case 'dim':
243
197
  this.CurrentAmbientLightLevel = this.set_minLux;
244
198
  break;
245
- case "bright":
199
+ case 'bright':
246
200
  default:
247
201
  this.CurrentAmbientLightLevel = this.set_maxLux;
248
202
  }
249
- this.debugLog(
250
- `Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
251
- );
203
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
252
204
  }
253
205
  }
254
206
  }
@@ -265,134 +217,113 @@ export class Motion {
265
217
  }
266
218
 
267
219
  private async BLERefreshStatus() {
268
- this.debugLog(
269
- `Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`,
270
- );
220
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
271
221
  const switchbot = await this.platform.connectBLE();
272
222
  // Convert to BLE Address
273
223
  this.device.bleMac = this.device
274
224
  .deviceId!.match(/.{1,2}/g)!
275
- .join(":")
225
+ .join(':')
276
226
  .toLowerCase();
277
- this.debugLog(
278
- `Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`,
279
- );
227
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
280
228
  // Start to monitor advertisement packets
281
- try {
282
- await switchbot.discover({
283
- model: "s",
284
- quick: false,
285
- });
286
- // Start to monitor advertisement packets
287
- await switchbot.startScan({
288
- model: "s",
289
- id: this.device.bleMac,
290
- });
291
- // Set an event hander
292
- switchbot.onadvertisement = (ad: settings.ad) => {
293
- this.serviceData = ad.serviceData;
294
- this.movement = ad.serviceData.movement;
295
- this.battery = ad.serviceData.battery;
296
- this.lightLevel = ad.serviceData.lightLevel;
297
- this.debugLog(
298
- `Motion Sensor: ${
299
- this.accessory.displayName
300
- } serviceData: ${JSON.stringify(ad.serviceData)}`,
301
- );
302
- this.debugLog(
303
- `Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: ` +
304
- `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
305
- );
229
+ if (switchbot !== false) {
230
+ switchbot
231
+ .startScan({
232
+ model: 's',
233
+ id: this.device.bleMac,
234
+ })
235
+ .then(() => {
236
+ // Set an event hander
237
+ switchbot.onadvertisement = (ad: any) => {
238
+ this.serviceData = ad.serviceData;
239
+ this.movement = ad.serviceData.movement;
240
+ this.battery = ad.serviceData.battery;
241
+ this.lightLevel = ad.serviceData.lightLevel;
242
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
243
+ this.debugLog(
244
+ `Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: ` +
245
+ `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
246
+ );
247
+
248
+ if (this.serviceData) {
249
+ this.connected = true;
250
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
251
+ } else {
252
+ this.connected = false;
253
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
254
+ }
255
+ };
256
+ // Wait 10 seconds
257
+ return switchbot.wait(this.scanDuration * 1000);
258
+ })
259
+ .then(async () => {
260
+ // Stop to monitor
261
+ switchbot.stopScan();
262
+ if (this.connected) {
263
+ this.parseStatus();
264
+ this.updateHomeKitCharacteristics();
265
+ } else {
266
+ await this.BLEconnection(switchbot);
267
+ }
268
+ })
269
+ .catch(async (e: any) => {
270
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
271
+ if (this.deviceLogging === 'debug') {
272
+ this.errorLog(
273
+ `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
274
+ ` Error Message: ${JSON.stringify(e.message)}`,
275
+ );
276
+ }
277
+ if (this.platform.debugMode) {
278
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` + ` Error: ${JSON.stringify(e)}`);
279
+ }
280
+ if (this.platform.config.credentials?.openToken) {
281
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
282
+ this.SwitchToOpenAPI = true;
283
+ await this.openAPIRefreshStatus();
284
+ }
285
+ this.apiError(e);
286
+ });
287
+ } else {
288
+ await this.BLEconnection(switchbot);
289
+ }
290
+ }
306
291
 
307
- if (this.serviceData) {
308
- this.connected = true;
309
- this.debugLog(
310
- `Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`,
311
- );
312
- } else {
313
- this.connected = false;
314
- this.debugLog(
315
- `Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`,
316
- );
317
- }
318
- };
319
- // Wait 10 seconds
320
- await switchbot.wait(this.scanDuration * 1000);
321
- // Stop to monitor
322
- switchbot.stopScan();
323
- if (this.connected) {
324
- this.parseStatus();
325
- this.updateHomeKitCharacteristics();
326
- } else {
327
- this.errorLog(
328
- `Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`,
329
- );
330
- if (this.platform.config.credentials?.openToken) {
331
- this.warnLog(
332
- `Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`,
333
- );
334
- this.SwitchToOpenAPI = true;
335
- await this.openAPIRefreshStatus();
336
- }
337
- }
338
- } catch (e: any) {
339
- this.errorLog(
340
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`,
341
- );
342
- if (this.deviceLogging.includes("debug")) {
343
- this.errorLog(
344
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
345
- ` Error Message: ${JSON.stringify(e.message)}`,
346
- );
347
- }
348
- if (this.platform.config.credentials?.openToken) {
349
- this.warnLog(
350
- `Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`,
351
- );
352
- this.SwitchToOpenAPI = true;
353
- await this.openAPIRefreshStatus();
354
- }
355
- this.apiError(e);
292
+ public async BLEconnection(switchbot: any) {
293
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
294
+ if (this.platform.config.credentials?.openToken) {
295
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
296
+ this.SwitchToOpenAPI = true;
297
+ await this.openAPIRefreshStatus();
356
298
  }
357
299
  }
358
300
 
359
301
  private async openAPIRefreshStatus() {
360
302
  if (this.platform.config.credentials?.openToken) {
361
- this.debugLog(
362
- `Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`,
363
- );
303
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} OpenAPI RefreshStatus`);
364
304
  try {
365
- this.deviceStatus = (
366
- await this.platform.axios.get(
367
- `${settings.DeviceURL}/${this.device.deviceId}/status`,
368
- )
369
- ).data;
370
- this.debugLog(
371
- `Motion Sensor: ${
372
- this.accessory.displayName
373
- } refreshStatus: ${JSON.stringify(this.deviceStatus)}`,
374
- );
305
+ this.deviceStatus = (await this.platform.axios.get(`${DeviceURL}/${this.device.deviceId}/status`)).data;
306
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
375
307
  if (this.deviceStatus.body.moveDetected === undefined) {
376
- this.errorLog(
377
- `Motion Sensor: ${
378
- this.accessory.displayName
379
- } refreshStatus: ${JSON.stringify(this.deviceStatus)}`,
380
- );
308
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} refreshStatus: ${JSON.stringify(this.deviceStatus)}`);
381
309
  }
382
310
  this.moveDetected = this.deviceStatus.body.moveDetected;
383
311
  this.brightness = this.deviceStatus.body.brightness;
384
312
  this.parseStatus();
385
313
  this.updateHomeKitCharacteristics();
386
314
  } catch (e: any) {
387
- this.errorLog(
388
- `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`,
389
- );
390
- if (this.deviceLogging.includes("debug")) {
315
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
316
+ if (this.deviceLogging === 'debug') {
391
317
  this.errorLog(
392
318
  `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
393
319
  ` Error Message: ${JSON.stringify(e.message)}`,
394
320
  );
395
321
  }
322
+ if (this.platform.debugMode) {
323
+ this.errorLog(
324
+ `Motion Sensor: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` + ` Error: ${JSON.stringify(e)}`,
325
+ );
326
+ }
396
327
  this.apiError(e);
397
328
  }
398
329
  }
@@ -403,167 +334,104 @@ export class Motion {
403
334
  */
404
335
  updateHomeKitCharacteristics() {
405
336
  if (this.MotionDetected === undefined) {
406
- this.debugLog(
407
- `Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`,
408
- );
337
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
409
338
  } else {
410
- this.service.updateCharacteristic(
411
- this.platform.Characteristic.MotionDetected,
412
- this.MotionDetected,
413
- );
414
- this.debugLog(
415
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`,
416
- );
339
+ this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
340
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
417
341
  }
418
342
  if (this.device.ble) {
419
343
  if (this.BatteryLevel === undefined) {
420
- this.debugLog(
421
- `Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`,
422
- );
344
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
423
345
  } else {
424
- this.batteryService?.updateCharacteristic(
425
- this.platform.Characteristic.BatteryLevel,
426
- this.BatteryLevel,
427
- );
428
- this.debugLog(
429
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`,
430
- );
346
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
347
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
431
348
  }
432
349
  if (this.StatusLowBattery === undefined) {
433
- this.debugLog(
434
- `Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`,
435
- );
350
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
436
351
  } else {
437
- this.batteryService?.updateCharacteristic(
438
- this.platform.Characteristic.StatusLowBattery,
439
- this.StatusLowBattery,
440
- );
441
- this.debugLog(
442
- `Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`,
443
- );
352
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
353
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
444
354
  }
445
355
  if (this.CurrentAmbientLightLevel === undefined) {
446
- this.debugLog(
447
- `Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
448
- );
356
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
449
357
  } else {
450
- this.lightSensorService?.updateCharacteristic(
451
- this.platform.Characteristic.CurrentAmbientLightLevel,
452
- this.CurrentAmbientLightLevel,
453
- );
358
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
454
359
  this.debugLog(
455
- `Motion Sensor: ${this.accessory.displayName}` +
456
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
360
+ `Motion Sensor: ${this.accessory.displayName}` + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
457
361
  );
458
362
  }
459
363
  }
460
364
  }
461
365
 
462
366
  public apiError(e: any) {
463
- this.service.updateCharacteristic(
464
- this.platform.Characteristic.MotionDetected,
465
- e,
466
- );
367
+ this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
467
368
  if (this.device.ble) {
468
- this.batteryService?.updateCharacteristic(
469
- this.platform.Characteristic.BatteryLevel,
470
- e,
471
- );
472
- this.batteryService?.updateCharacteristic(
473
- this.platform.Characteristic.StatusLowBattery,
474
- e,
475
- );
476
- this.lightSensorService?.updateCharacteristic(
477
- this.platform.Characteristic.CurrentAmbientLightLevel,
478
- e,
479
- );
369
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
370
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
371
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
480
372
  }
481
373
  //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
482
374
  }
483
375
 
484
- config(device: settings.device & settings.devicesConfig) {
376
+ config(device: device & devicesConfig) {
485
377
  let config = {};
486
378
  if (device.motion) {
487
379
  config = device.motion;
488
380
  }
489
381
  if (device.ble !== undefined) {
490
- config["ble"] = device.ble;
382
+ config['ble'] = device.ble;
491
383
  }
492
384
  if (device.logging !== undefined) {
493
- config["logging"] = device.logging;
385
+ config['logging'] = device.logging;
494
386
  }
495
387
  if (device.refreshRate !== undefined) {
496
- config["refreshRate"] = device.refreshRate;
388
+ config['refreshRate'] = device.refreshRate;
497
389
  }
498
390
  if (device.scanDuration !== undefined) {
499
- config["scanDuration"] = device.scanDuration;
391
+ config['scanDuration'] = device.scanDuration;
500
392
  }
501
393
  if (Object.entries(config).length !== 0) {
502
- this.warnLog(
503
- `Motion Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(
504
- config,
505
- )}`,
506
- );
394
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
507
395
  }
508
396
  }
509
397
 
510
- refreshRate(device: settings.device & settings.devicesConfig) {
398
+ refreshRate(device: device & devicesConfig) {
511
399
  if (device.refreshRate) {
512
- this.deviceRefreshRate = this.accessory.context.refreshRate =
513
- device.refreshRate;
514
- this.debugLog(
515
- `Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`,
516
- );
400
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
401
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
517
402
  } else if (this.platform.config.options!.refreshRate) {
518
- this.deviceRefreshRate = this.accessory.context.refreshRate =
519
- this.platform.config.options!.refreshRate;
520
- this.debugLog(
521
- `Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`,
522
- );
403
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
404
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
523
405
  }
524
406
  }
525
407
 
526
- scan(device: settings.device & settings.devicesConfig) {
408
+ scan(device: device & devicesConfig) {
527
409
  if (device.scanDuration) {
528
- this.scanDuration = this.accessory.context.scanDuration =
529
- device.scanDuration;
410
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
530
411
  if (device.ble) {
531
- this.debugLog(
532
- `Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`,
533
- );
412
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
534
413
  }
535
414
  } else {
536
415
  this.scanDuration = this.accessory.context.scanDuration = 1;
537
416
  if (this.device.ble) {
538
- this.debugLog(
539
- `Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`,
540
- );
417
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
541
418
  }
542
419
  }
543
420
  }
544
421
 
545
- logs(device: settings.device & settings.devicesConfig) {
422
+ logs(device: device & devicesConfig) {
546
423
  if (this.platform.debugMode) {
547
- this.deviceLogging = this.accessory.context.logging = "debugMode";
548
- this.debugLog(
549
- `Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`,
550
- );
424
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
425
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
551
426
  } else if (device.logging) {
552
427
  this.deviceLogging = this.accessory.context.logging = device.logging;
553
- this.debugLog(
554
- `Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`,
555
- );
428
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
556
429
  } else if (this.platform.config.options?.logging) {
557
- this.deviceLogging = this.accessory.context.logging =
558
- this.platform.config.options?.logging;
559
- this.debugLog(
560
- `Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`,
561
- );
430
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
431
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
562
432
  } else {
563
- this.deviceLogging = this.accessory.context.logging = "standard";
564
- this.debugLog(
565
- `Motion Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`,
566
- );
433
+ this.deviceLogging = this.accessory.context.logging = 'standard';
434
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
567
435
  }
568
436
  }
569
437
 
@@ -588,28 +456,28 @@ export class Motion {
588
456
  /**
589
457
  * Logging for Device
590
458
  */
591
- infoLog(...log: any[]): void {
459
+ infoLog(...log: any[]) {
592
460
  if (this.enablingDeviceLogging()) {
593
461
  this.platform.log.info(String(...log));
594
462
  }
595
463
  }
596
464
 
597
- warnLog(...log: any[]): void {
465
+ warnLog(...log: any[]) {
598
466
  if (this.enablingDeviceLogging()) {
599
467
  this.platform.log.warn(String(...log));
600
468
  }
601
469
  }
602
470
 
603
- errorLog(...log: any[]): void {
471
+ errorLog(...log: any[]) {
604
472
  if (this.enablingDeviceLogging()) {
605
473
  this.platform.log.error(String(...log));
606
474
  }
607
475
  }
608
476
 
609
- debugLog(...log: any[]): void {
477
+ debugLog(...log: any[]) {
610
478
  if (this.enablingDeviceLogging()) {
611
- if (this.deviceLogging === "debug") {
612
- this.platform.log.info("[DEBUG]", String(...log));
479
+ if (this.deviceLogging === 'debug') {
480
+ this.platform.log.info('[DEBUG]', String(...log));
613
481
  } else {
614
482
  this.platform.log.debug(String(...log));
615
483
  }
@@ -617,8 +485,6 @@ export class Motion {
617
485
  }
618
486
 
619
487
  enablingDeviceLogging(): boolean {
620
- return (
621
- this.deviceLogging.includes("debug") || this.deviceLogging === "standard"
622
- );
488
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
623
489
  }
624
490
  }