@switchbot/homebridge-switchbot 1.6.4-beta.2 → 1.7.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.
@@ -52,9 +52,14 @@ class Bot {
52
52
  // get the LightBulb service if it exists, otherwise create a new LightBulb service
53
53
  // you can create multiple services for each accessory
54
54
  if (device.bot?.deviceType === 'switch') {
55
+ this.removeFanService(accessory);
56
+ this.removeLockService(accessory);
55
57
  this.removeDoorService(accessory);
58
+ this.removeFaucetService(accessory);
56
59
  this.removeOutletService(accessory);
60
+ this.removeWindowService(accessory);
57
61
  this.removeGarageDoorService(accessory);
62
+ this.removeWindowCoveringService(accessory);
58
63
  // Add switchService
59
64
  (this.switchService =
60
65
  accessory.getService(this.platform.Service.Switch) ||
@@ -64,9 +69,14 @@ class Bot {
64
69
  this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
65
70
  }
66
71
  else if (device.bot?.deviceType === 'garagedoor') {
72
+ this.removeFanService(accessory);
73
+ this.removeLockService(accessory);
67
74
  this.removeDoorService(accessory);
75
+ this.removeFaucetService(accessory);
68
76
  this.removeOutletService(accessory);
69
77
  this.removeSwitchService(accessory);
78
+ this.removeWindowService(accessory);
79
+ this.removeWindowCoveringService(accessory);
70
80
  // Add switchService
71
81
  (this.garageDoorService =
72
82
  accessory.getService(this.platform.Service.GarageDoorOpener) ||
@@ -77,14 +87,19 @@ class Bot {
77
87
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
78
88
  }
79
89
  else if (device.bot?.deviceType === 'door') {
90
+ this.removeFanService(accessory);
91
+ this.removeLockService(accessory);
80
92
  this.removeOutletService(accessory);
93
+ this.removeFaucetService(accessory);
81
94
  this.removeSwitchService(accessory);
95
+ this.removeWindowService(accessory);
82
96
  this.removeGarageDoorService(accessory);
97
+ this.removeWindowCoveringService(accessory);
83
98
  // Add switchService
84
99
  (this.doorService =
85
100
  accessory.getService(this.platform.Service.Door) ||
86
101
  accessory.addService(this.platform.Service.Door)), `${accessory.displayName} Door`;
87
- this.infoLog(`Bot: ${accessory.displayName} Displaying as Garage Door Opener`);
102
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Door`);
88
103
  this.doorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
89
104
  this.doorService.getCharacteristic(this.platform.Characteristic.TargetPosition)
90
105
  .setProps({
@@ -95,10 +110,114 @@ class Bot {
95
110
  }).onSet(this.handleOnSet.bind(this));
96
111
  this.doorService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
97
112
  }
113
+ else if (device.bot?.deviceType === 'window') {
114
+ this.removeFanService(accessory);
115
+ this.removeLockService(accessory);
116
+ this.removeDoorService(accessory);
117
+ this.removeOutletService(accessory);
118
+ this.removeFaucetService(accessory);
119
+ this.removeSwitchService(accessory);
120
+ this.removeGarageDoorService(accessory);
121
+ this.removeWindowCoveringService(accessory);
122
+ // Add switchService
123
+ (this.windowService =
124
+ accessory.getService(this.platform.Service.Window) ||
125
+ accessory.addService(this.platform.Service.Window)), `${accessory.displayName} Window`;
126
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Window`);
127
+ this.windowService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
128
+ this.windowService.getCharacteristic(this.platform.Characteristic.TargetPosition)
129
+ .setProps({
130
+ validValues: [0, 100],
131
+ minValue: 0,
132
+ maxValue: 100,
133
+ minStep: 100,
134
+ }).onSet(this.handleOnSet.bind(this));
135
+ this.windowService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
136
+ }
137
+ else if (device.bot?.deviceType === 'windowcovering') {
138
+ this.removeFanService(accessory);
139
+ this.removeLockService(accessory);
140
+ this.removeDoorService(accessory);
141
+ this.removeOutletService(accessory);
142
+ this.removeFaucetService(accessory);
143
+ this.removeSwitchService(accessory);
144
+ this.removeWindowService(accessory);
145
+ this.removeGarageDoorService(accessory);
146
+ // Add switchService
147
+ (this.windowCoveringService =
148
+ accessory.getService(this.platform.Service.WindowCovering) ||
149
+ accessory.addService(this.platform.Service.WindowCovering)), `${accessory.displayName} Window Covering`;
150
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Window Covering`);
151
+ this.windowCoveringService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
152
+ this.windowCoveringService.getCharacteristic(this.platform.Characteristic.TargetPosition)
153
+ .setProps({
154
+ validValues: [0, 100],
155
+ minValue: 0,
156
+ maxValue: 100,
157
+ minStep: 100,
158
+ }).onSet(this.handleOnSet.bind(this));
159
+ this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
160
+ }
161
+ else if (device.bot?.deviceType === 'lock') {
162
+ this.removeFanService(accessory);
163
+ this.removeDoorService(accessory);
164
+ this.removeOutletService(accessory);
165
+ this.removeSwitchService(accessory);
166
+ this.removeFaucetService(accessory);
167
+ this.removeWindowService(accessory);
168
+ this.removeGarageDoorService(accessory);
169
+ this.removeWindowCoveringService(accessory);
170
+ // Add switchService
171
+ (this.lockService =
172
+ accessory.getService(this.platform.Service.LockMechanism) ||
173
+ accessory.addService(this.platform.Service.LockMechanism)), `${accessory.displayName} Lock`;
174
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Lock`);
175
+ this.lockService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
176
+ this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.handleOnSet.bind(this));
177
+ }
178
+ else if (device.bot?.deviceType === 'faucet') {
179
+ this.removeFanService(accessory);
180
+ this.removeLockService(accessory);
181
+ this.removeDoorService(accessory);
182
+ this.removeOutletService(accessory);
183
+ this.removeSwitchService(accessory);
184
+ this.removeWindowService(accessory);
185
+ this.removeGarageDoorService(accessory);
186
+ this.removeWindowCoveringService(accessory);
187
+ // Add switchService
188
+ (this.faucetService =
189
+ accessory.getService(this.platform.Service.Faucet) ||
190
+ accessory.addService(this.platform.Service.Faucet)), `${accessory.displayName} Faucet`;
191
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Faucet`);
192
+ this.faucetService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
193
+ this.faucetService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.handleOnSet.bind(this));
194
+ }
195
+ else if (device.bot?.deviceType === 'fan') {
196
+ this.removeLockService(accessory);
197
+ this.removeDoorService(accessory);
198
+ this.removeFaucetService(accessory);
199
+ this.removeOutletService(accessory);
200
+ this.removeSwitchService(accessory);
201
+ this.removeWindowService(accessory);
202
+ this.removeGarageDoorService(accessory);
203
+ this.removeWindowCoveringService(accessory);
204
+ // Add switchService
205
+ (this.fanService =
206
+ accessory.getService(this.platform.Service.Fan) ||
207
+ accessory.addService(this.platform.Service.Fan)), `${accessory.displayName} Fan`;
208
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Fan`);
209
+ this.fanService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
210
+ this.fanService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
211
+ }
98
212
  else {
213
+ this.removeFanService(accessory);
214
+ this.removeLockService(accessory);
99
215
  this.removeDoorService(accessory);
216
+ this.removeFaucetService(accessory);
100
217
  this.removeSwitchService(accessory);
218
+ this.removeWindowService(accessory);
101
219
  this.removeGarageDoorService(accessory);
220
+ this.removeWindowCoveringService(accessory);
102
221
  // Add outletService
103
222
  (this.outletService =
104
223
  accessory.getService(this.platform.Service.Outlet) ||
@@ -151,34 +270,74 @@ class Bot {
151
270
  }
152
271
  removeOutletService(accessory) {
153
272
  // If outletService still pressent, then remove first
273
+ this.outletService = this.accessory.getService(this.platform.Service.Outlet);
154
274
  if (this.outletService) {
155
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
275
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Outlet Service`);
156
276
  }
157
- this.outletService = this.accessory.getService(this.platform.Service.Outlet);
158
277
  accessory.removeService(this.outletService);
159
278
  }
160
279
  removeGarageDoorService(accessory) {
161
280
  // If garageDoorService still pressent, then remove first
281
+ this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
162
282
  if (this.garageDoorService) {
163
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover garageDoorService first`);
283
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Garage Door Service`);
164
284
  }
165
- this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
166
285
  accessory.removeService(this.garageDoorService);
167
286
  }
168
287
  removeDoorService(accessory) {
169
288
  // If doorService still pressent, then remove first
289
+ this.doorService = this.accessory.getService(this.platform.Service.Door);
170
290
  if (this.doorService) {
171
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover doorService first`);
291
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Door Service`);
172
292
  }
173
- this.doorService = this.accessory.getService(this.platform.Service.Door);
174
293
  accessory.removeService(this.doorService);
175
294
  }
295
+ removeLockService(accessory) {
296
+ // If lockService still pressent, then remove first
297
+ this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
298
+ if (this.lockService) {
299
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Lock Service`);
300
+ }
301
+ accessory.removeService(this.lockService);
302
+ }
303
+ removeFaucetService(accessory) {
304
+ // If faucetService still pressent, then remove first
305
+ this.faucetService = this.accessory.getService(this.platform.Service.Faucet);
306
+ if (this.faucetService) {
307
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Faucet Service`);
308
+ }
309
+ accessory.removeService(this.faucetService);
310
+ }
311
+ removeFanService(accessory) {
312
+ // If fanService still pressent, then remove first
313
+ this.fanService = this.accessory.getService(this.platform.Service.Fan);
314
+ if (this.fanService) {
315
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Fan Service`);
316
+ }
317
+ accessory.removeService(this.fanService);
318
+ }
319
+ removeWindowService(accessory) {
320
+ // If windowService still pressent, then remove first
321
+ this.windowService = this.accessory.getService(this.platform.Service.Window);
322
+ if (this.windowService) {
323
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Window Service`);
324
+ }
325
+ accessory.removeService(this.windowService);
326
+ }
327
+ removeWindowCoveringService(accessory) {
328
+ // If windowCoveringService still pressent, then remove first
329
+ this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering);
330
+ if (this.windowCoveringService) {
331
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Window Covering Service`);
332
+ }
333
+ accessory.removeService(this.windowCoveringService);
334
+ }
176
335
  removeSwitchService(accessory) {
177
336
  // If switchService still pressent, then remove first
337
+ this.switchService = this.accessory.getService(this.platform.Service.Switch);
178
338
  if (this.switchService) {
179
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
339
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Switch Service`);
180
340
  }
181
- this.switchService = this.accessory.getService(this.platform.Service.Switch);
182
341
  accessory.removeService(this.switchService);
183
342
  }
184
343
  refreshRate() {
@@ -322,80 +481,92 @@ class Bot {
322
481
  await this.openAPIRefreshStatus();
323
482
  }
324
483
  }
325
- connectBLE() {
326
- // Convert to BLE Address
327
- this.device.bleMac = ((this.device.deviceId.match(/.{1,2}/g)).join(':')).toLowerCase();
328
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
329
- const switchbot = this.platform.connectBLE();
484
+ async connectBLE() {
485
+ let switchbot;
486
+ try {
487
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
488
+ const Switchbot = require('node-switchbot');
489
+ switchbot = new Switchbot();
490
+ // Convert to BLE Address
491
+ this.device.bleMac = ((this.device.deviceId.match(/.{1,2}/g)).join(':')).toLowerCase();
492
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
493
+ }
494
+ catch (e) {
495
+ switchbot = false;
496
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
497
+ if (this.deviceLogging === 'debug') {
498
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
499
+ + ` Error Message: ${JSON.stringify(e.message)}`);
500
+ }
501
+ if (this.platform.debugMode) {
502
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
503
+ + ` Error: ${JSON.stringify(e)}`);
504
+ }
505
+ }
330
506
  return switchbot;
331
507
  }
332
508
  async BLERefreshStatus() {
333
509
  this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
334
510
  // Convert to BLE Address
335
- const switchbot = this.connectBLE();
511
+ const switchbot = await this.connectBLE();
336
512
  // Start to monitor advertisement packets
337
- if (switchbot !== false) {
338
- switchbot.startScan({
339
- model: 'H',
340
- id: this.device.bleMac,
341
- }).then(() => {
342
- // Set an event hander
343
- switchbot.onadvertisement = (ad) => {
344
- this.serviceData = ad.serviceData;
345
- this.mode = ad.serviceData.mode;
346
- this.state = ad.serviceData.state;
347
- this.battery = ad.serviceData.battery;
348
- this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
349
- this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
350
- + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
351
- if (this.serviceData) {
352
- this.connected = true;
353
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
354
- }
355
- else {
356
- this.connected = false;
357
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
358
- }
359
- };
360
- // Wait 2 seconds
361
- return switchbot.wait(this.scanDuration * 1000);
362
- }).then(async () => {
363
- // Stop to monitor
364
- switchbot.stopScan();
365
- if (this.connected) {
366
- this.parseStatus();
367
- this.updateHomeKitCharacteristics();
513
+ if (switchbot === false) {
514
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection: ${switchbot}`);
515
+ }
516
+ switchbot.startScan({
517
+ model: 'H',
518
+ id: this.device.bleMac,
519
+ }).then(() => {
520
+ // Set an event hander
521
+ switchbot.onadvertisement = (ad) => {
522
+ this.serviceData = ad.serviceData;
523
+ this.mode = ad.serviceData.mode;
524
+ this.state = ad.serviceData.state;
525
+ this.battery = ad.serviceData.battery;
526
+ this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
527
+ this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
528
+ + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
529
+ if (this.serviceData) {
530
+ this.connected = true;
531
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
368
532
  }
369
533
  else {
370
- await this.BLEconnection();
371
- }
372
- }).catch(async (e) => {
373
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
374
- if (this.deviceLogging === 'debug') {
375
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
376
- + ` Error Message: ${JSON.stringify(e.message)}`);
377
- }
378
- if (this.platform.debugMode) {
379
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
380
- + ` Error: ${JSON.stringify(e)}`);
534
+ this.connected = false;
535
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
381
536
  }
537
+ };
538
+ // Wait 2 seconds
539
+ return switchbot.wait(this.scanDuration * 1000);
540
+ }).then(async () => {
541
+ // Stop to monitor
542
+ switchbot.stopScan();
543
+ if (this.connected) {
544
+ this.parseStatus();
545
+ this.updateHomeKitCharacteristics();
546
+ }
547
+ else {
548
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
382
549
  if (this.platform.config.credentials?.openToken) {
383
550
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
384
551
  await this.openAPIRefreshStatus();
385
552
  }
386
- this.apiError(e);
387
- });
388
- }
389
- else {
390
- await this.BLEconnection();
391
- }
392
- }
393
- async BLEconnection() {
394
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
395
- if (this.platform.config.credentials?.openToken) {
396
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
397
- await this.openAPIRefreshStatus();
398
- }
553
+ }
554
+ }).catch(async (e) => {
555
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
556
+ if (this.deviceLogging === 'debug') {
557
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
558
+ + ` Error Message: ${JSON.stringify(e.message)}`);
559
+ }
560
+ if (this.platform.debugMode) {
561
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
562
+ + ` Error: ${JSON.stringify(e)}`);
563
+ }
564
+ if (this.platform.config.credentials?.openToken) {
565
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
566
+ await this.openAPIRefreshStatus();
567
+ }
568
+ this.apiError(e);
569
+ });
399
570
  }
400
571
  async openAPIRefreshStatus() {
401
572
  if (this.platform.config.credentials?.openToken) {
@@ -442,84 +613,84 @@ class Bot {
442
613
  });
443
614
  }
444
615
  async BLEpushChanges() {
616
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.OnCached}`);
445
617
  if (this.On !== this.OnCached) {
446
618
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
447
- const switchbot = this.connectBLE();
448
- if (switchbot !== false) {
449
- if (this.botMode === 'press') {
450
- this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
451
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
452
- .then((device_list) => {
453
- this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
454
- return device_list[0].press({ id: this.device.bleMac });
455
- }).then(() => {
456
- this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
457
- this.On = false;
458
- this.OnCached = this.On;
459
- this.accessory.context.On = this.OnCached;
460
- setTimeout(() => {
461
- if (this.device.bot?.deviceType === 'switch') {
462
- this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
463
- }
464
- else {
465
- this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
466
- }
467
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
468
- }, 500);
469
- }).catch(async (e) => {
470
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
471
- if (this.deviceLogging === 'debug') {
472
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
473
- + ` Error Message: ${JSON.stringify(e.message)}`);
474
- }
475
- if (this.platform.debugMode) {
476
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
477
- + ` Error: ${JSON.stringify(e)}`);
478
- }
479
- if (this.platform.config.credentials?.openToken) {
480
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
481
- await this.openAPIpushChanges();
482
- }
483
- this.apiError(e);
484
- });
485
- }
486
- else if (this.botMode === 'switch') {
487
- this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
488
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list) => {
489
- this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
490
- return this.turnOnOff(device_list);
491
- }).then(() => {
492
- this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
493
- this.OnCached = this.On;
494
- this.accessory.context.On = this.OnCached;
495
- }).catch(async (e) => {
496
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
497
- if (this.deviceLogging === 'debug') {
498
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
499
- + ` Error Message: ${JSON.stringify(e.message)}`);
500
- }
501
- if (this.platform.debugMode) {
502
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
503
- + ` Error: ${JSON.stringify(e)}`);
619
+ const switchbot = await this.connectBLE();
620
+ //if (switchbot !== false) {
621
+ if (this.botMode === 'press') {
622
+ this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
623
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
624
+ .then((device_list) => {
625
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
626
+ return device_list[0].press({ id: this.device.bleMac });
627
+ }).then(() => {
628
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
629
+ this.On = false;
630
+ this.OnCached = this.On;
631
+ this.accessory.context.On = this.OnCached;
632
+ setTimeout(() => {
633
+ if (this.device.bot?.deviceType === 'switch') {
634
+ this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
504
635
  }
505
- if (this.platform.config.credentials?.openToken) {
506
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
507
- await this.openAPIpushChanges();
636
+ else {
637
+ this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
508
638
  }
509
- this.apiError(e);
510
- });
511
- }
512
- else {
513
- throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
514
- }
639
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
640
+ }, 500);
641
+ }).catch(async (e) => {
642
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
643
+ if (this.deviceLogging === 'debug') {
644
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
645
+ + ` Error Message: ${JSON.stringify(e.message)}`);
646
+ }
647
+ if (this.platform.debugMode) {
648
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
649
+ + ` Error: ${JSON.stringify(e)}`);
650
+ }
651
+ if (this.platform.config.credentials?.openToken) {
652
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
653
+ await this.openAPIpushChanges();
654
+ }
655
+ this.apiError(e);
656
+ });
657
+ }
658
+ else if (this.botMode === 'switch') {
659
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
660
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list) => {
661
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
662
+ return this.turnOnOff(device_list);
663
+ }).then(() => {
664
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
665
+ this.OnCached = this.On;
666
+ this.accessory.context.On = this.OnCached;
667
+ }).catch(async (e) => {
668
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
669
+ if (this.deviceLogging === 'debug') {
670
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
671
+ + ` Error Message: ${JSON.stringify(e.message)}`);
672
+ }
673
+ if (this.platform.debugMode) {
674
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
675
+ + ` Error: ${JSON.stringify(e)}`);
676
+ }
677
+ if (this.platform.config.credentials?.openToken) {
678
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
679
+ await this.openAPIpushChanges();
680
+ }
681
+ this.apiError(e);
682
+ });
515
683
  }
516
684
  else {
517
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
518
- if (this.platform.config.credentials?.openToken) {
519
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
520
- await this.openAPIpushChanges();
521
- }
685
+ throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
522
686
  }
687
+ /* } else {
688
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
689
+ if (this.platform.config.credentials?.openToken) {
690
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
691
+ await this.openAPIpushChanges();
692
+ }
693
+ }*/
523
694
  }
524
695
  this.OnCached = this.On;
525
696
  this.accessory.context.On = this.OnCached;
@@ -603,7 +774,7 @@ class Bot {
603
774
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
604
775
  }
605
776
  }
606
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
777
+ this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
607
778
  }
608
779
  else if (this.device.bot?.deviceType === 'door') {
609
780
  if (this.On === undefined) {
@@ -623,7 +794,97 @@ class Bot {
623
794
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
624
795
  }
625
796
  }
626
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
797
+ this.debugLog(`Bot: ${this.accessory.displayName} Door On: ${this.On}`);
798
+ }
799
+ else if (this.device.bot?.deviceType === 'window') {
800
+ if (this.On === undefined) {
801
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
802
+ }
803
+ else {
804
+ if (this.On) {
805
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
806
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
807
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
808
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
809
+ }
810
+ else {
811
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
812
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
813
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
814
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
815
+ }
816
+ }
817
+ this.debugLog(`Bot: ${this.accessory.displayName} Window On: ${this.On}`);
818
+ }
819
+ else if (this.device.bot?.deviceType === 'windowcovering') {
820
+ if (this.On === undefined) {
821
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
822
+ }
823
+ else {
824
+ if (this.On) {
825
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
826
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
827
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
828
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
829
+ }
830
+ else {
831
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
832
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
833
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
834
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
835
+ }
836
+ }
837
+ this.debugLog(`Bot: ${this.accessory.displayName} Window Covering On: ${this.On}`);
838
+ }
839
+ else if (this.device.bot?.deviceType === 'lock') {
840
+ if (this.On === undefined) {
841
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
842
+ }
843
+ else {
844
+ if (this.On) {
845
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, this.platform.Characteristic.LockTargetState.UNSECURED);
846
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, this.platform.Characteristic.LockCurrentState.UNSECURED);
847
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: UNSECURED, LockCurrentState: UNSECURED`);
848
+ }
849
+ else {
850
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, this.platform.Characteristic.LockTargetState.SECURED);
851
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, this.platform.Characteristic.LockCurrentState.SECURED);
852
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: SECURED, LockCurrentState: SECURED`);
853
+ }
854
+ }
855
+ this.debugLog(`Bot: ${this.accessory.displayName} Lock On: ${this.On}`);
856
+ }
857
+ else if (this.device.bot?.deviceType === 'faucet') {
858
+ if (this.On === undefined) {
859
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
860
+ }
861
+ else {
862
+ if (this.On) {
863
+ this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, this.platform.Characteristic.Active.ACTIVE);
864
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic Active: ${this.On}`);
865
+ }
866
+ else {
867
+ this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, this.platform.Characteristic.Active.INACTIVE);
868
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic Active: ${this.On}`);
869
+ }
870
+ }
871
+ this.debugLog(`Bot: ${this.accessory.displayName} Faucet On: ${this.On}`);
872
+ }
873
+ else if (this.device.bot?.deviceType === 'fan') {
874
+ if (this.On === undefined) {
875
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
876
+ }
877
+ else {
878
+ if (this.On) {
879
+ this.fanService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
880
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
881
+ }
882
+ else {
883
+ this.fanService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
884
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
885
+ }
886
+ }
887
+ this.debugLog(`Bot: ${this.accessory.displayName} Fan On: ${this.On}`);
627
888
  }
628
889
  else if (this.device.bot?.deviceType === 'switch') {
629
890
  if (this.On === undefined) {
@@ -671,6 +932,26 @@ class Bot {
671
932
  this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
672
933
  this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
673
934
  }
935
+ else if (this.device.bot?.deviceType === 'window') {
936
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
937
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
938
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
939
+ }
940
+ else if (this.device.bot?.deviceType === 'windowcovering') {
941
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
942
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
943
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
944
+ }
945
+ else if (this.device.bot?.deviceType === 'lock') {
946
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
947
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
948
+ }
949
+ else if (this.device.bot?.deviceType === 'faucet') {
950
+ this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, e);
951
+ }
952
+ else if (this.device.bot?.deviceType === 'fan') {
953
+ this.fanService?.updateCharacteristic(this.platform.Characteristic.On, e);
954
+ }
674
955
  else if (this.device.bot?.deviceType === 'switch') {
675
956
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
676
957
  }
@@ -737,9 +1018,29 @@ class Bot {
737
1018
  this.On = true;
738
1019
  }
739
1020
  }
740
- else if (this.device.bot?.deviceType === 'door') {
1021
+ else if (this.device.bot?.deviceType === 'door'
1022
+ || this.device.bot?.deviceType === 'window'
1023
+ || this.device.bot?.deviceType === 'windowcovering') {
741
1024
  this.debugLog(`Bot: ${this.accessory.displayName} TargetPosition: ${value}`);
742
- if (value === 100) {
1025
+ if (value === 0) {
1026
+ this.On = false;
1027
+ }
1028
+ else {
1029
+ this.On = true;
1030
+ }
1031
+ }
1032
+ else if (this.device.bot?.deviceType === 'lock') {
1033
+ this.debugLog(`Bot: ${this.accessory.displayName} LockTargetState: ${value}`);
1034
+ if (value === this.platform.Characteristic.LockTargetState.SECURED) {
1035
+ this.On = false;
1036
+ }
1037
+ else {
1038
+ this.On = true;
1039
+ }
1040
+ }
1041
+ else if (this.device.bot?.deviceType === 'faucet') {
1042
+ this.debugLog(`Bot: ${this.accessory.displayName} Active: ${value}`);
1043
+ if (value === this.platform.Characteristic.Active.INACTIVE) {
743
1044
  this.On = false;
744
1045
  }
745
1046
  else {