@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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 (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,5 +1,9 @@
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * bot.ts: @switchbot/homebridge-switchbot.
4
+ */
1
5
  import { request } from 'undici';
2
- import { sleep } from '../utils.js';
6
+ import { deviceBase } from './device.js';
3
7
  import { interval, Subject } from 'rxjs';
4
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
5
9
  import { Devices } from '../settings.js';
@@ -8,96 +12,53 @@ import { Devices } from '../settings.js';
8
12
  * An instance of this class is created for each accessory your platform registers
9
13
  * Each accessory may expose multiple services of different service types.
10
14
  */
11
- export class Bot {
15
+ export class Bot extends deviceBase {
12
16
  platform;
13
- accessory;
14
- device;
15
- api;
16
- log;
17
- config;
18
- hap;
19
17
  // Services
20
- fanService;
21
- doorService;
22
- lockService;
23
- faucetService;
24
- windowService;
25
- switchService;
26
- outletService;
27
- batteryService;
28
- garageDoorService;
29
- windowCoveringService;
30
- statefulProgrammableSwitchService;
31
- // Characteristic Values
32
- On;
33
- BatteryLevel;
34
- FirmwareRevision;
35
- StatusLowBattery;
36
- // OpenAPI Status
37
- OpenAPI_On;
38
- OpenAPI_BatteryLevel;
39
- OpenAPI_FirmwareRevision;
40
- // BLE Status
41
- BLE_On;
42
- BLE_Mode;
43
- BLE_BatteryLevel;
44
- //BLE Others
45
- BLE_IsConnected;
18
+ Battery;
19
+ Switch;
20
+ GarageDoor;
21
+ Door;
22
+ Window;
23
+ WindowCovering;
24
+ Lock;
25
+ Faucet;
26
+ Fan;
27
+ StatefulProgrammableSwitch;
28
+ Outlet;
46
29
  // Config
47
30
  botMode;
48
31
  allowPush;
49
32
  doublePress;
50
- scanDuration;
51
33
  botDeviceType;
52
34
  pushRatePress;
53
- deviceLogging;
54
35
  multiPressCount;
55
- deviceRefreshRate;
56
- maxRetries;
57
- delayBetweenRetries;
58
36
  // Updates
59
37
  botUpdateInProgress;
60
38
  doBotUpdate;
61
- // Connection
62
- OpenAPI;
63
- BLE;
64
39
  constructor(platform, accessory, device) {
40
+ super(platform, accessory, device);
65
41
  this.platform = platform;
66
- this.accessory = accessory;
67
- this.device = device;
68
- this.api = this.platform.api;
69
- this.log = this.platform.log;
70
- this.config = this.platform.config;
71
- this.hap = this.api.hap;
72
- // Connection
73
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
74
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
75
42
  // default placeholders
76
- this.deviceLogs(device);
77
- this.deviceType(device);
78
- this.scan(device);
79
- this.refreshRate(device);
80
- this.PressOrSwitch(device);
81
- this.allowPushChanges(device);
82
- this.deviceContext();
83
- this.DoublePress(device);
84
- this.deviceRetry(device);
85
- this.deviceConfig(device);
86
- this.multiPressCount = 0;
43
+ this.getBotConfigSettings(device);
87
44
  // this is subject we use to track when we need to POST changes to the SwitchBot API
88
45
  this.doBotUpdate = new Subject();
89
46
  this.botUpdateInProgress = false;
90
47
  // Retrieve initial values and updateHomekit
91
48
  this.refreshStatus();
92
- // set accessory information
93
- accessory
94
- .getService(this.hap.Service.AccessoryInformation)
95
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
96
- .setCharacteristic(this.hap.Characteristic.Model, 'SWITCHBOT-BOT-S1')
97
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
98
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
49
+ // Initialize Battery property
50
+ this.Battery = {
51
+ Service: accessory.addService(this.hap.Service.Battery),
52
+ BatteryLevel: accessory.context.BatteryLevel || 100,
53
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
54
+ };
99
55
  // deviceType
100
56
  if (this.botDeviceType === 'switch') {
57
+ // Initialize Switch property
58
+ this.Switch = {
59
+ Service: this.accessory.addService(this.hap.Service.Switch),
60
+ On: accessory.context.On || false,
61
+ };
101
62
  this.removeFanService(accessory);
102
63
  this.removeLockService(accessory);
103
64
  this.removeDoorService(accessory);
@@ -109,16 +70,18 @@ export class Bot {
109
70
  this.removeStatefulProgrammableSwitchService(accessory);
110
71
  // Add switchService
111
72
  const switchService = `${accessory.displayName} Switch`;
112
- (this.switchService = accessory.getService(this.hap.Service.Switch)
73
+ (this.Switch.Service = accessory.getService(this.hap.Service.Switch)
113
74
  || accessory.addService(this.hap.Service.Switch)), switchService;
114
75
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Switch`);
115
- this.switchService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
116
- if (!this.switchService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
117
- this.switchService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
118
- }
119
- this.switchService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
76
+ this.Switch.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
77
+ this.Switch.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
120
78
  }
121
79
  else if (this.botDeviceType === 'garagedoor') {
80
+ // Initialize Switch property
81
+ this.GarageDoor = {
82
+ Service: this.accessory.addService(this.hap.Service.GarageDoorOpener),
83
+ On: accessory.context.On || false,
84
+ };
122
85
  this.removeFanService(accessory);
123
86
  this.removeLockService(accessory);
124
87
  this.removeDoorService(accessory);
@@ -130,17 +93,19 @@ export class Bot {
130
93
  this.removeStatefulProgrammableSwitchService(accessory);
131
94
  // Add garageDoorService
132
95
  const garageDoorService = `${accessory.displayName} Garage Door Opener`;
133
- (this.garageDoorService = accessory.getService(this.hap.Service.GarageDoorOpener)
96
+ (this.GarageDoor.Service = accessory.getService(this.hap.Service.GarageDoorOpener)
134
97
  || accessory.addService(this.hap.Service.GarageDoorOpener)), garageDoorService;
135
98
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Garage Door Opener`);
136
- this.garageDoorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
137
- if (!this.garageDoorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
138
- this.garageDoorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
139
- }
140
- this.garageDoorService.getCharacteristic(this.hap.Characteristic.TargetDoorState).onSet(this.OnSet.bind(this));
141
- this.garageDoorService.setCharacteristic(this.hap.Characteristic.ObstructionDetected, false);
99
+ this.GarageDoor.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
100
+ this.GarageDoor.Service.getCharacteristic(this.hap.Characteristic.TargetDoorState).onSet(this.OnSet.bind(this));
101
+ this.GarageDoor.Service.setCharacteristic(this.hap.Characteristic.ObstructionDetected, false);
142
102
  }
143
103
  else if (this.botDeviceType === 'door') {
104
+ // Initialize Switch property
105
+ this.Door = {
106
+ Service: this.accessory.addService(this.hap.Service.Door),
107
+ On: accessory.context.On || false,
108
+ };
144
109
  this.removeFanService(accessory);
145
110
  this.removeLockService(accessory);
146
111
  this.removeOutletService(accessory);
@@ -152,14 +117,11 @@ export class Bot {
152
117
  this.removeStatefulProgrammableSwitchService(accessory);
153
118
  // Add doorService
154
119
  const doorService = `${accessory.displayName} Door`;
155
- (this.doorService = accessory.getService(this.hap.Service.Door)
120
+ (this.Door.Service = accessory.getService(this.hap.Service.Door)
156
121
  || accessory.addService(this.hap.Service.Door)), doorService;
157
122
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Door`);
158
- this.doorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
159
- if (!this.doorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
160
- this.doorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
161
- }
162
- this.doorService
123
+ this.Door.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
124
+ this.Door.Service
163
125
  .getCharacteristic(this.hap.Characteristic.TargetPosition)
164
126
  .setProps({
165
127
  validValues: [0, 100],
@@ -168,9 +130,14 @@ export class Bot {
168
130
  minStep: 100,
169
131
  })
170
132
  .onSet(this.OnSet.bind(this));
171
- this.doorService.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
133
+ this.Door.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
172
134
  }
173
135
  else if (this.botDeviceType === 'window') {
136
+ // Initialize Switch property
137
+ this.Window = {
138
+ Service: this.accessory.addService(this.hap.Service.Window),
139
+ On: accessory.context.On || false,
140
+ };
174
141
  this.removeFanService(accessory);
175
142
  this.removeLockService(accessory);
176
143
  this.removeDoorService(accessory);
@@ -182,14 +149,11 @@ export class Bot {
182
149
  this.removeStatefulProgrammableSwitchService(accessory);
183
150
  // Add windowService
184
151
  const windowService = `${accessory.displayName} Window`;
185
- (this.windowService = accessory.getService(this.hap.Service.Window)
152
+ (this.Window.Service = accessory.getService(this.hap.Service.Window)
186
153
  || accessory.addService(this.hap.Service.Window)), windowService;
187
154
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Window`);
188
- this.windowService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
189
- if (!this.windowService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
190
- this.windowService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
191
- }
192
- this.windowService
155
+ this.Window.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
156
+ this.Window.Service
193
157
  .getCharacteristic(this.hap.Characteristic.TargetPosition)
194
158
  .setProps({
195
159
  validValues: [0, 100],
@@ -198,9 +162,14 @@ export class Bot {
198
162
  minStep: 100,
199
163
  })
200
164
  .onSet(this.OnSet.bind(this));
201
- this.windowService.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
165
+ this.Window.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
202
166
  }
203
167
  else if (this.botDeviceType === 'windowcovering') {
168
+ // Initialize Switch property
169
+ this.WindowCovering = {
170
+ Service: this.accessory.addService(this.hap.Service.WindowCovering),
171
+ On: accessory.context.On || false,
172
+ };
204
173
  this.removeFanService(accessory);
205
174
  this.removeLockService(accessory);
206
175
  this.removeDoorService(accessory);
@@ -212,14 +181,11 @@ export class Bot {
212
181
  this.removeStatefulProgrammableSwitchService(accessory);
213
182
  // Add windowCoveringService
214
183
  const windowCoveringService = `${accessory.displayName} Window Covering`;
215
- (this.windowCoveringService = accessory.getService(this.hap.Service.WindowCovering)
184
+ (this.WindowCovering.Service = accessory.getService(this.hap.Service.WindowCovering)
216
185
  || accessory.addService(this.hap.Service.WindowCovering)), windowCoveringService;
217
186
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Window Covering`);
218
- this.windowCoveringService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
219
- if (!this.windowCoveringService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
220
- this.windowCoveringService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
221
- }
222
- this.windowCoveringService
187
+ this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
188
+ this.WindowCovering.Service
223
189
  .getCharacteristic(this.hap.Characteristic.TargetPosition)
224
190
  .setProps({
225
191
  validValues: [0, 100],
@@ -228,9 +194,14 @@ export class Bot {
228
194
  minStep: 100,
229
195
  })
230
196
  .onSet(this.OnSet.bind(this));
231
- this.windowCoveringService.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
197
+ this.WindowCovering.Service.setCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
232
198
  }
233
199
  else if (this.botDeviceType === 'lock') {
200
+ // Initialize Switch property
201
+ this.Lock = {
202
+ Service: this.accessory.addService(this.hap.Service.LockMechanism),
203
+ On: accessory.context.On || false,
204
+ };
234
205
  this.removeFanService(accessory);
235
206
  this.removeDoorService(accessory);
236
207
  this.removeOutletService(accessory);
@@ -242,16 +213,18 @@ export class Bot {
242
213
  this.removeStatefulProgrammableSwitchService(accessory);
243
214
  // Add lockService
244
215
  const lockService = `${accessory.displayName} Lock`;
245
- (this.lockService = accessory.getService(this.hap.Service.LockMechanism)
216
+ (this.Lock.Service = accessory.getService(this.hap.Service.LockMechanism)
246
217
  || accessory.addService(this.hap.Service.LockMechanism)), lockService;
247
218
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Lock`);
248
- this.lockService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
249
- if (!this.lockService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
250
- this.lockService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
251
- }
252
- this.lockService.getCharacteristic(this.hap.Characteristic.LockTargetState).onSet(this.OnSet.bind(this));
219
+ this.Lock.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
220
+ this.Lock.Service.getCharacteristic(this.hap.Characteristic.LockTargetState).onSet(this.OnSet.bind(this));
253
221
  }
254
222
  else if (this.botDeviceType === 'faucet') {
223
+ // Initialize Switch property
224
+ this.Faucet = {
225
+ Service: this.accessory.addService(this.hap.Service.Faucet),
226
+ On: accessory.context.On || false,
227
+ };
255
228
  this.removeFanService(accessory);
256
229
  this.removeLockService(accessory);
257
230
  this.removeDoorService(accessory);
@@ -263,16 +236,18 @@ export class Bot {
263
236
  this.removeStatefulProgrammableSwitchService(accessory);
264
237
  // Add faucetService
265
238
  const faucetService = `${accessory.displayName} Faucet`;
266
- (this.faucetService = accessory.getService(this.hap.Service.Faucet)
239
+ (this.Faucet.Service = accessory.getService(this.hap.Service.Faucet)
267
240
  || accessory.addService(this.hap.Service.Faucet)), faucetService;
268
241
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Faucet`);
269
- this.faucetService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
270
- if (!this.faucetService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
271
- this.faucetService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
272
- }
273
- this.faucetService.getCharacteristic(this.hap.Characteristic.Active).onSet(this.OnSet.bind(this));
242
+ this.Faucet.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
243
+ this.Faucet.Service.getCharacteristic(this.hap.Characteristic.Active).onSet(this.OnSet.bind(this));
274
244
  }
275
245
  else if (this.botDeviceType === 'fan') {
246
+ // Initialize Switch property
247
+ this.Fan = {
248
+ Service: this.accessory.addService(this.hap.Service.Fanv2),
249
+ On: accessory.context.On || false,
250
+ };
276
251
  this.removeLockService(accessory);
277
252
  this.removeDoorService(accessory);
278
253
  this.removeFaucetService(accessory);
@@ -284,16 +259,18 @@ export class Bot {
284
259
  this.removeStatefulProgrammableSwitchService(accessory);
285
260
  // Add fanService
286
261
  const fanService = `${accessory.displayName} Fan`;
287
- (this.fanService = accessory.getService(this.hap.Service.Fan)
288
- || accessory.addService(this.hap.Service.Fan)), fanService;
262
+ (this.Fan.Service = accessory.getService(this.hap.Service.Fanv2)
263
+ || accessory.addService(this.hap.Service.Fanv2)), fanService;
289
264
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Fan`);
290
- this.fanService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
291
- if (!this.fanService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
292
- this.fanService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
293
- }
294
- this.fanService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
265
+ this.Fan.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
266
+ this.Fan.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
295
267
  }
296
268
  else if (this.botDeviceType === 'stateful') {
269
+ // Initialize Switch property
270
+ this.StatefulProgrammableSwitch = {
271
+ Service: this.accessory.addService(this.hap.Service.StatefulProgrammableSwitch),
272
+ On: accessory.context.On || false,
273
+ };
297
274
  this.removeFanService(accessory);
298
275
  this.removeLockService(accessory);
299
276
  this.removeDoorService(accessory);
@@ -305,18 +282,20 @@ export class Bot {
305
282
  this.removeWindowCoveringService(accessory);
306
283
  // Add statefulProgrammableSwitchService
307
284
  const statefulProgrammableSwitchService = `${accessory.displayName} Stateful Programmable Switch`;
308
- (this.statefulProgrammableSwitchService = accessory.getService(this.hap.Service.StatefulProgrammableSwitch) ||
285
+ (this.StatefulProgrammableSwitch.Service = accessory.getService(this.hap.Service.StatefulProgrammableSwitch) ||
309
286
  accessory.addService(this.hap.Service.StatefulProgrammableSwitch)), statefulProgrammableSwitchService;
310
287
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Stateful Programmable Switch`);
311
- this.statefulProgrammableSwitchService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
312
- if (!this.statefulProgrammableSwitchService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
313
- this.statefulProgrammableSwitchService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
314
- }
315
- this.statefulProgrammableSwitchService
288
+ this.StatefulProgrammableSwitch.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
289
+ this.StatefulProgrammableSwitch.Service
316
290
  .getCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState)
317
291
  .onSet(this.OnSet.bind(this));
318
292
  }
319
293
  else {
294
+ // Initialize Switch property
295
+ this.Outlet = {
296
+ Service: this.accessory.addService(this.hap.Service.Outlet),
297
+ On: accessory.context.On || false,
298
+ };
320
299
  this.removeFanService(accessory);
321
300
  this.removeLockService(accessory);
322
301
  this.removeDoorService(accessory);
@@ -328,24 +307,18 @@ export class Bot {
328
307
  this.removeStatefulProgrammableSwitchService(accessory);
329
308
  // Add outletService
330
309
  const outletService = `${accessory.displayName} Outlet`;
331
- (this.outletService = accessory.getService(this.hap.Service.Outlet)
310
+ (this.Outlet.Service = accessory.getService(this.hap.Service.Outlet)
332
311
  || accessory.addService(this.hap.Service.Outlet)), outletService;
333
312
  this.debugWarnLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Outlet`);
334
- this.outletService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
335
- if (!this.outletService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
336
- this.outletService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
337
- }
338
- this.outletService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
313
+ this.Outlet.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
314
+ this.Outlet.Service.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
339
315
  }
340
316
  // batteryService
341
317
  const batteryService = `${accessory.displayName} Battery`;
342
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
318
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
343
319
  || accessory.addService(this.hap.Service.Battery)), batteryService;
344
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
345
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
346
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
347
- }
348
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
320
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
321
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
349
322
  // Retrieve initial values and updateHomekit
350
323
  this.updateHomeKitCharacteristics();
351
324
  // Start an update interval
@@ -361,13 +334,15 @@ export class Bot {
361
334
  try {
362
335
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
363
336
  const { power, battery, deviceMode } = context;
364
- const { On, BatteryLevel, botMode } = this;
337
+ const { botMode } = this;
338
+ const On = await this.getOn();
339
+ const { BatteryLevel } = this.Battery;
365
340
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
366
341
  '(power, battery, deviceMode) = ' +
367
342
  `Webhook:(${power}, ${battery}, ${deviceMode}), ` +
368
343
  `current:(${On}, ${BatteryLevel}, ${botMode})`);
369
- this.On = power;
370
- this.BatteryLevel = battery;
344
+ await this.setOn(power);
345
+ this.Battery.BatteryLevel = battery;
371
346
  this.botMode = deviceMode;
372
347
  this.updateHomeKitCharacteristics();
373
348
  }
@@ -405,80 +380,69 @@ export class Bot {
405
380
  });
406
381
  }
407
382
  /**
408
- * Parse the device status from the SwitchBot api
383
+ * Parse the device status from the SwitchBotBLE API
409
384
  */
410
- async parseStatus() {
411
- if (!this.device.enableCloudService && this.OpenAPI) {
412
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
413
- }
414
- else if (this.BLE) {
415
- await this.BLEparseStatus();
416
- }
417
- else if (this.OpenAPI && this.platform.config.credentials?.token) {
418
- await this.openAPIparseStatus();
419
- }
420
- else {
421
- await this.offlineOff();
422
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
423
- }
424
- }
425
- async BLEparseStatus() {
385
+ async BLEparseStatus(serviceData) {
426
386
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
427
387
  // BLEmode (true if Switch Mode) | (false if Press Mode)
428
- if (this.BLE_Mode) {
429
- this.accessory.context.On = this.On;
430
- if (this.On === undefined) {
431
- this.On = Boolean(this.BLE_On);
388
+ if (serviceData.mode) {
389
+ this.accessory.context.On = await this.getOn();
390
+ if (this.getOn() === undefined) {
391
+ this.setOn(Boolean(serviceData.state));
432
392
  }
433
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, mode: ${this.BLE_Mode}, On: ${this.On}`);
393
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode,`
394
+ + ` mode: ${serviceData.mode}, On: ${this.accessory.context.On}`);
434
395
  }
435
396
  else {
436
- this.On = false;
437
- this.accessory.context.On = this.On;
438
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode, mode: ${this.BLE_Mode}, On: ${this.On}`);
397
+ this.setOn(false);
398
+ this.accessory.context.On = await this.getOn();
399
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode,`
400
+ + ` mode: ${serviceData.mode}, On: ${this.accessory.context.On}`);
439
401
  }
440
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
441
- if (this.BatteryLevel < 10) {
442
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
402
+ this.Battery.BatteryLevel = Number(serviceData.battery);
403
+ if (this.Battery.BatteryLevel < 10) {
404
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
443
405
  }
444
406
  else {
445
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
407
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
446
408
  }
447
- if (Number.isNaN(this.BatteryLevel)) {
448
- this.BatteryLevel = 100;
409
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
410
+ this.Battery.BatteryLevel = 100;
449
411
  }
450
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
451
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
412
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
413
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
452
414
  }
453
- async openAPIparseStatus() {
415
+ /**
416
+ * Parse the device status from the SwitchBot OpenAPI
417
+ */
418
+ async openAPIparseStatus(deviceStatus) {
454
419
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
455
420
  if (this.botMode === 'press') {
456
- this.On = false;
457
- this.accessory.context.On = this.On;
421
+ this.setOn(false);
422
+ this.accessory.context.On = await this.getOn();
458
423
  }
459
424
  else {
460
- this.accessory.context.On = this.On;
461
- if (this.On === undefined) {
462
- this.On = false;
425
+ this.accessory.context.On = await this.getOn();
426
+ if (this.getOn() === undefined) {
427
+ this.setOn(false);
463
428
  }
464
429
  }
465
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.accessory.context.On}`);
466
431
  // Battery
467
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
468
- if (this.BatteryLevel < 10) {
469
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
432
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
433
+ if (this.Battery.BatteryLevel < 10) {
434
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
470
435
  }
471
436
  else {
472
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
437
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
473
438
  }
474
- if (Number.isNaN(this.BatteryLevel)) {
475
- this.BatteryLevel = 100;
439
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
440
+ this.Battery.BatteryLevel = 100;
476
441
  }
477
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
478
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
442
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
443
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
479
444
  // FirmwareRevision
480
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
481
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
445
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
482
446
  }
483
447
  /**
484
448
  * Asks the SwitchBot API for the latest device information
@@ -512,88 +476,34 @@ export class Bot {
512
476
  // Start to monitor advertisement packets
513
477
  (async () => {
514
478
  // Start to monitor advertisement packets
515
- await switchbot.startScan({
516
- model: 'H',
517
- id: this.device.bleMac,
518
- });
479
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
519
480
  // Set an event handler
520
481
  switchbot.onadvertisement = (ad) => {
521
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
522
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
523
- if (this.device.bleMac === ad.address && ad.model === 'H') {
482
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
483
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
484
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
524
485
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
525
- this.BLE_Mode = ad.serviceData.mode;
526
- this.BLE_On = ad.serviceData.state;
527
- this.BLE_BatteryLevel = ad.serviceData.battery;
528
486
  }
529
487
  else {
530
488
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
531
489
  }
532
490
  };
533
- // Wait 1 seconds
491
+ // Wait 10 seconds
534
492
  await switchbot.wait(this.scanDuration * 1000);
535
493
  // Stop to monitor
536
494
  await switchbot.stopScan();
537
495
  // Update HomeKit
538
- await this.BLEparseStatus();
496
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
539
497
  await this.updateHomeKitCharacteristics();
540
498
  })();
541
- /*if (switchbot !== false) {
542
- switchbot
543
- .startScan({
544
- model: 'H',
545
- id: this.device.bleMac,
546
- })
547
- .then(async () => {
548
- // Set an event handler
549
- switchbot.onadvertisement = async (ad: ad) => {
550
- this.debugLog(
551
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
552
- ` BLE Address Found: ${ad.address}`,
553
- );
554
- this.BLE_Mode = ad.serviceData.mode;
555
- this.BLE_On = ad.serviceData.state;
556
- this.BLE_BatteryLevel = ad.serviceData.battery;
557
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
558
- this.debugLog(
559
- `${this.device.deviceType}: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ` +
560
- `${ad.serviceData.modelName}, mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`,
561
- );
562
-
563
- if (ad.serviceData) {
564
- this.BLE_IsConnected = true;
565
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
566
- await this.stopScanning(switchbot);
567
- } else {
568
- this.BLE_IsConnected = false;
569
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
570
- }
571
- };
572
- // Wait
573
- return await sleep(this.scanDuration * 1000);
574
- })
575
- .then(async () => {
576
- // Stop to monitor
577
- await this.stopScanning(switchbot);
578
- })
579
- .catch(async (e: any) => {
580
- this.apiError(e);
581
- this.errorLog(
582
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
583
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
584
- );
585
- await this.BLERefreshConnection(switchbot);
586
- });
587
- } else {
588
- await this.BLERefreshConnection(switchbot);
589
- }*/
499
+ if (switchbot === undefined) {
500
+ await this.BLERefreshConnection(switchbot);
501
+ }
590
502
  }
591
503
  async openAPIRefreshStatus() {
592
504
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
593
505
  try {
594
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, {
595
- headers: this.platform.generateHeaders(),
596
- });
506
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
597
507
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
598
508
  const deviceStatus = await body.json();
599
509
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -601,10 +511,7 @@ export class Bot {
601
511
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
602
512
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
603
513
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
604
- this.OpenAPI_On = deviceStatus.body.power;
605
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
606
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
607
- this.openAPIparseStatus();
514
+ this.openAPIparseStatus(deviceStatus);
608
515
  this.updateHomeKitCharacteristics();
609
516
  }
610
517
  else {
@@ -649,8 +556,9 @@ export class Bot {
649
556
  }
650
557
  async BLEpushChanges() {
651
558
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
652
- if (this.On !== this.accessory.context.On || this.allowPush) {
653
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
559
+ const On = await this.getOn();
560
+ if (On !== this.accessory.context.On || this.allowPush) {
561
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${On} OnCached: ${this.accessory.context.On}`);
654
562
  const switchbot = await this.platform.connectBLE();
655
563
  // Convert to BLE Address
656
564
  this.device.bleMac = this.device
@@ -664,22 +572,22 @@ export class Bot {
664
572
  switchbot
665
573
  .discover({ model: 'H', quick: true, id: this.device.bleMac })
666
574
  .then(async (device_list) => {
667
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
575
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${On}`);
668
576
  return await device_list[0].press({ id: this.device.bleMac });
669
577
  })
670
578
  .then(() => {
671
579
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
672
580
  this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
673
- + `On: ${this.On} sent over BLE, sent successfully`);
674
- this.accessory.context.On = this.On;
581
+ + `On: ${On} sent over BLE, sent successfully`);
582
+ this.accessory.context.On = On;
675
583
  setTimeout(() => {
676
584
  if (this.botDeviceType === 'switch') {
677
- this.switchService?.getCharacteristic(this.hap.Characteristic.On).updateValue(this.On);
585
+ this.Switch.Service.getCharacteristic(this.hap.Characteristic.On).updateValue(On);
678
586
  }
679
587
  else {
680
- this.outletService?.getCharacteristic(this.hap.Characteristic.On).updateValue(this.On);
588
+ this.Outlet.Service.getCharacteristic(this.hap.Characteristic.On).updateValue(On);
681
589
  }
682
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
590
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${On}, Switch Timeout`);
683
591
  }, 500);
684
592
  })
685
593
  .catch(async (e) => {
@@ -694,11 +602,11 @@ export class Bot {
694
602
  switchbot
695
603
  .discover({ model: 'H', quick: true, id: this.device.bleMac })
696
604
  .then(async (device_list) => {
697
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
698
- return await this.retry({
699
- max: this.maxRetry(),
605
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${On}`);
606
+ return await this.retryBLE({
607
+ max: await this.maxRetryBLE(),
700
608
  fn: async () => {
701
- if (this.On) {
609
+ if (On) {
702
610
  return await device_list[0].turnOn({ id: this.device.bleMac });
703
611
  }
704
612
  else {
@@ -709,12 +617,12 @@ export class Bot {
709
617
  })
710
618
  .then(() => {
711
619
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
712
- this.accessory.context.On = this.On;
620
+ this.accessory.context.On = On;
713
621
  })
714
622
  .catch(async (e) => {
715
623
  this.apiError(e);
716
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
717
- ` Connection & botMode: ${this.botMode}, Error Message: ${JSON.stringify(e.message)}`);
624
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
625
+ + ` Connection & botMode: ${this.botMode}, Error Message: ${JSON.stringify(e.message)}`);
718
626
  await this.BLEPushConnection();
719
627
  });
720
628
  }
@@ -723,31 +631,32 @@ export class Bot {
723
631
  }
724
632
  }
725
633
  else {
726
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, ` + `OnCached: ${this.accessory.context.On}`);
634
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges, On: ${On}, OnCached: ${this.accessory.context.On}`);
727
635
  }
728
636
  }
729
637
  async openAPIpushChanges() {
730
638
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
639
+ let On = await this.getOn();
731
640
  if (this.multiPressCount > 0) {
732
641
  this.debugLog(`${this.device.deviceType}: ${this.multiPressCount} request(s) queued.`);
733
- this.On = true;
642
+ On = true;
734
643
  }
735
- if (this.On !== this.accessory.context.On || this.allowPush || this.multiPressCount > 0) {
644
+ if (On !== this.accessory.context.On || this.allowPush || this.multiPressCount > 0) {
736
645
  let command = '';
737
- if (this.botMode === 'switch' && this.On) {
646
+ if (this.botMode === 'switch' && On) {
738
647
  command = 'turnOn';
739
- this.On = true;
740
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
648
+ On = true;
649
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${On}`);
741
650
  }
742
- else if (this.botMode === 'switch' && !this.On) {
651
+ else if (this.botMode === 'switch' && !On) {
743
652
  command = 'turnOff';
744
- this.On = false;
745
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
653
+ On = false;
654
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${On}`);
746
655
  }
747
656
  else if (this.botMode === 'press' || this.botMode === 'multipress') {
748
657
  command = 'press';
749
658
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode`);
750
- this.On = false;
659
+ On = false;
751
660
  }
752
661
  else {
753
662
  throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Device Parameters not set for this Bot.`);
@@ -783,21 +692,20 @@ export class Bot {
783
692
  this.multiPressCount--;
784
693
  if (this.multiPressCount > 0) {
785
694
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Multi Press Count: ${this.multiPressCount}`);
786
- this.On = true;
695
+ On = true;
787
696
  this.openAPIpushChanges();
788
697
  }
789
698
  }
790
699
  }
791
700
  catch (e) {
792
701
  this.apiError(e);
793
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
794
- ` Connection, Error Message: ${JSON.stringify(e.message)}`);
702
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
703
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
795
704
  }
796
705
  }
797
706
  else {
798
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` +
799
- `On: ${this.On}, ` +
800
- `OnCached: ${this.accessory.context.On}`);
707
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges,`
708
+ + ` On: ${On}, OnCached: ${this.accessory.context.On}`);
801
709
  }
802
710
  }
803
711
  /**
@@ -807,59 +715,101 @@ export class Bot {
807
715
  if (this.botDeviceType === 'garagedoor') {
808
716
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetDoorState: ${value}`);
809
717
  if (value === this.hap.Characteristic.TargetDoorState.CLOSED) {
810
- this.On = false;
718
+ await this.setOn(false);
719
+ this.GarageDoor.On = false;
811
720
  }
812
721
  else {
813
- this.On = true;
722
+ await this.setOn(true);
723
+ this.GarageDoor.On = true;
814
724
  }
815
725
  }
816
- else if (this.botDeviceType === 'door' ||
817
- this.botDeviceType === 'window' ||
818
- this.botDeviceType === 'windowcovering') {
726
+ else if (this.botDeviceType === 'door') {
727
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
728
+ if (value === 0) {
729
+ await this.setOn(false);
730
+ this.Door.On = false;
731
+ }
732
+ else {
733
+ await this.setOn(true);
734
+ this.Door.On = true;
735
+ }
736
+ }
737
+ else if (this.botDeviceType === 'window') {
738
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
739
+ if (value === 0) {
740
+ await this.setOn(false);
741
+ this.Window.On = false;
742
+ }
743
+ else {
744
+ await this.setOn(true);
745
+ this.Window.On = true;
746
+ }
747
+ }
748
+ else if (this.botDeviceType === 'windowcovering') {
819
749
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
820
750
  if (value === 0) {
821
- this.On = false;
751
+ await this.setOn(false);
752
+ this.WindowCovering.On = false;
822
753
  }
823
754
  else {
824
- this.On = true;
755
+ await this.setOn(true);
756
+ this.WindowCovering.On = true;
825
757
  }
826
758
  }
827
759
  else if (this.botDeviceType === 'lock') {
828
760
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set LockTargetState: ${value}`);
829
761
  if (value === this.hap.Characteristic.LockTargetState.SECURED) {
830
- this.On = false;
762
+ await this.setOn(false);
763
+ this.Lock.On = false;
831
764
  }
832
765
  else {
833
- this.On = true;
766
+ await this.setOn(true);
767
+ this.Lock.On = true;
834
768
  }
835
769
  }
836
770
  else if (this.botDeviceType === 'faucet') {
837
771
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
838
772
  if (value === this.hap.Characteristic.Active.INACTIVE) {
839
- this.On = false;
773
+ await this.setOn(false);
774
+ this.Faucet.On = false;
840
775
  }
841
776
  else {
842
- this.On = true;
777
+ await this.setOn(true);
778
+ this.Faucet.On = true;
843
779
  }
844
780
  }
845
781
  else if (this.botDeviceType === 'stateful') {
846
782
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ProgrammableSwitchOutputState: ${value}`);
847
783
  if (value === 0) {
848
- this.On = false;
784
+ await this.setOn(false);
785
+ this.StatefulProgrammableSwitch.On = false;
849
786
  }
850
787
  else {
851
- this.On = true;
788
+ await this.setOn(true);
789
+ this.StatefulProgrammableSwitch.On = true;
790
+ }
791
+ }
792
+ else if (this.botDeviceType === 'switch') {
793
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ProgrammableSwitchOutputState: ${value}`);
794
+ if (value === 0) {
795
+ await this.setOn(false);
796
+ this.Switch.On = false;
797
+ }
798
+ else {
799
+ await this.setOn(true);
800
+ this.Switch.On = true;
852
801
  }
853
802
  }
854
803
  else {
855
804
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
856
- if (this.device.bot?.mode === 'multipress') {
857
- if (value === true) {
858
- this.multiPressCount++;
859
- this.debugLog(`${this.device.deviceType} set to Multi-Press. Multi-Press count: ${this.multiPressCount}`);
860
- }
805
+ await this.setOn(Boolean(value));
806
+ this.Outlet.On = value;
807
+ }
808
+ if (this.device.bot?.mode === 'multipress') {
809
+ if (value === true) {
810
+ this.multiPressCount++;
811
+ this.debugLog(`${this.device.deviceType} set to Multi-Press. Multi-Press count: ${this.multiPressCount}`);
861
812
  }
862
- this.On = value;
863
813
  }
864
814
  this.doBotUpdate.next();
865
815
  }
@@ -869,508 +819,360 @@ export class Bot {
869
819
  async updateHomeKitCharacteristics() {
870
820
  // State
871
821
  if (this.botDeviceType === 'garagedoor') {
872
- if (this.On === undefined) {
873
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
822
+ if (this.GarageDoor.On === undefined) {
823
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.GarageDoor.On}`);
874
824
  }
875
825
  else {
876
- if (this.On) {
877
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.TargetDoorState, this.hap.Characteristic.TargetDoorState.OPEN);
878
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, this.hap.Characteristic.CurrentDoorState.OPEN);
879
- this.debugLog(`${this.device.deviceType}: ` + `${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
826
+ if (this.GarageDoor.On) {
827
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.TargetDoorState, this.hap.Characteristic.TargetDoorState.OPEN);
828
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, this.hap.Characteristic.CurrentDoorState.OPEN);
829
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
830
+ + ` TargetDoorState: Open, CurrentDoorState: Open (${this.GarageDoor.On})`);
880
831
  }
881
832
  else {
882
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.TargetDoorState, this.hap.Characteristic.TargetDoorState.CLOSED);
883
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, this.hap.Characteristic.CurrentDoorState.CLOSED);
884
- this.debugLog(`${this.device.deviceType}: ` + `${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
833
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.TargetDoorState, this.hap.Characteristic.TargetDoorState.CLOSED);
834
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, this.hap.Characteristic.CurrentDoorState.CLOSED);
835
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
836
+ + ` TargetDoorState: Open, CurrentDoorState: Open (${this.GarageDoor.On})`);
885
837
  }
886
838
  }
887
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Garage Door On: ${this.On}`);
839
+ await this.setOn(Boolean(this.GarageDoor.On));
840
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Garage Door On: ${this.GarageDoor.On}`);
888
841
  }
889
842
  else if (this.botDeviceType === 'door') {
890
- if (this.On === undefined) {
891
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
843
+ if (this.Door.On === undefined) {
844
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Door.On}`);
892
845
  }
893
846
  else {
894
- if (this.On) {
895
- this.doorService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
896
- this.doorService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
897
- this.doorService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
898
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
847
+ if (this.Door.On) {
848
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
849
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
850
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
851
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
852
+ + ` TargetPosition: 100, CurrentPosition: 100 (${this.Door.On})`);
899
853
  }
900
854
  else {
901
- this.doorService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
902
- this.doorService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
903
- this.doorService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
904
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
855
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
856
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
857
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
858
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
859
+ + ` TargetPosition: 0, CurrentPosition: 0 (${this.Door.On})`);
905
860
  }
906
861
  }
907
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Door On: ${this.On}`);
862
+ await this.setOn(Boolean(this.Door.On));
863
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Door On: ${this.Door.On}`);
908
864
  }
909
865
  else if (this.botDeviceType === 'window') {
910
- if (this.On === undefined) {
911
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
866
+ if (this.Window.On === undefined) {
867
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Window.On}`);
912
868
  }
913
869
  else {
914
- if (this.On) {
915
- this.windowService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
916
- this.windowService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
917
- this.windowService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
918
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
870
+ if (this.Window.On) {
871
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
872
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
873
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
874
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
875
+ + ` TargetPosition: 100, CurrentPosition: 100 (${this.Window.On})`);
919
876
  }
920
877
  else {
921
- this.windowService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
922
- this.windowService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
923
- this.windowService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
924
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
878
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
879
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
880
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
881
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
882
+ + ` TargetPosition: 0, CurrentPosition: 0 (${this.Window.On})`);
925
883
  }
926
884
  }
927
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Window On: ${this.On}`);
885
+ await this.setOn(Boolean(this.Window.On));
886
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Window On: ${this.Window.On}`);
928
887
  }
929
888
  else if (this.botDeviceType === 'windowcovering') {
930
- if (this.On === undefined) {
931
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
889
+ if (this.WindowCovering.On === undefined) {
890
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.WindowCovering.On}`);
932
891
  }
933
892
  else {
934
- if (this.On) {
935
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
936
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
937
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
938
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
893
+ if (this.WindowCovering.On) {
894
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 100);
895
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 100);
896
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
897
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
898
+ + ` TargetPosition: 100, CurrentPosition: 100 (${this.WindowCovering.On})`);
939
899
  }
940
900
  else {
941
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
942
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
943
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
944
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
901
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
902
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
903
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
904
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
905
+ + ` TargetPosition: 0, CurrentPosition: 0 (${this.WindowCovering.On})`);
945
906
  }
946
907
  }
947
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Window Covering On: ${this.On}`);
908
+ await this.setOn(Boolean(this.WindowCovering.On));
909
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Window Covering On: ${this.WindowCovering.On}`);
948
910
  }
949
911
  else if (this.botDeviceType === 'lock') {
950
- if (this.On === undefined) {
951
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
912
+ if (this.Lock.On === undefined) {
913
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Lock.On}`);
952
914
  }
953
915
  else {
954
- if (this.On) {
955
- this.lockService?.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.UNSECURED);
956
- this.lockService?.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.UNSECURED);
957
- this.debugLog(`${this.device.deviceType}: ` +
958
- `${this.accessory.displayName} updateCharacteristic LockTargetState: UNSECURED, LockCurrentState: UNSECURED`);
916
+ if (this.Lock.On) {
917
+ this.Lock.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.UNSECURED);
918
+ this.Lock.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.UNSECURED);
919
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristicc`
920
+ + ` LockTargetState: UNSECURED, LockCurrentState: UNSECURED (${this.Lock.On})`);
959
921
  }
960
922
  else {
961
- this.lockService?.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.SECURED);
962
- this.lockService?.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.SECURED);
963
- this.debugLog(`${this.device.deviceType}: ` + `${this.accessory.displayName} updateCharacteristic LockTargetState: SECURED, LockCurrentState: SECURED`);
923
+ this.Lock.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.SECURED);
924
+ this.Lock.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.SECURED);
925
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
926
+ + ` LockTargetState: SECURED, LockCurrentState: SECURED (${this.Lock.On})`);
964
927
  }
965
928
  }
966
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Lock On: ${this.On}`);
929
+ await this.setOn(Boolean(this.Lock.On));
930
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Lock On: ${this.Lock.On}`);
967
931
  }
968
932
  else if (this.botDeviceType === 'faucet') {
969
- if (this.On === undefined) {
970
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
933
+ if (this.Faucet.On === undefined) {
934
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Faucet.On}`);
971
935
  }
972
936
  else {
973
- if (this.On) {
974
- this.faucetService?.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.ACTIVE);
975
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.On}`);
937
+ if (this.Faucet.On) {
938
+ this.Faucet.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.ACTIVE);
939
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Faucet.On}`);
976
940
  }
977
941
  else {
978
- this.faucetService?.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
979
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.On}`);
942
+ this.Faucet.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
943
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Faucet.On}`);
980
944
  }
981
945
  }
982
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Faucet On: ${this.On}`);
946
+ await this.setOn(Boolean(this.Faucet.On));
947
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Faucet On: ${this.Faucet.On}`);
983
948
  }
984
949
  else if (this.botDeviceType === 'fan') {
985
- if (this.On === undefined) {
986
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
950
+ if (this.Fan.On === undefined) {
951
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Fan.On}`);
987
952
  }
988
953
  else {
989
- if (this.On) {
990
- this.fanService?.updateCharacteristic(this.hap.Characteristic.On, this.On);
991
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
954
+ if (this.Fan.On) {
955
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.On, this.Fan.On);
956
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Fan.On}`);
992
957
  }
993
958
  else {
994
- this.fanService?.updateCharacteristic(this.hap.Characteristic.On, this.On);
995
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
959
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.On, this.Fan.On);
960
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Fan.On}`);
996
961
  }
997
962
  }
998
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Fan On: ${this.On}`);
963
+ await this.setOn(Boolean(this.Fan.On));
964
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Fan On: ${this.Fan.On}`);
999
965
  }
1000
966
  else if (this.botDeviceType === 'stateful') {
1001
- if (this.On === undefined) {
1002
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
967
+ if (this.StatefulProgrammableSwitch.On === undefined) {
968
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.StatefulProgrammableSwitch.On}`);
1003
969
  }
1004
970
  else {
1005
- if (this.On) {
1006
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
1007
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, 1);
1008
- this.debugLog(`${this.device.deviceType}: ` +
1009
- `${this.accessory.displayName} updateCharacteristic ProgrammableSwitchEvent: SINGLE, ProgrammableSwitchOutputState: 1`);
971
+ if (this.StatefulProgrammableSwitch.On) {
972
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
973
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, 1);
974
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
975
+ + ` ProgrammableSwitchEvent: SINGLE, ProgrammableSwitchOutputState: 1 (${this.StatefulProgrammableSwitch.On})`);
1010
976
  }
1011
977
  else {
1012
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
1013
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, 0);
1014
- this.debugLog(`${this.device.deviceType}: ` +
1015
- `${this.accessory.displayName} updateCharacteristic ProgrammableSwitchEvent: SINGLE, ProgrammableSwitchOutputState: 0`);
978
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
979
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, 0);
980
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
981
+ + ` ProgrammableSwitchEvent: SINGLE, ProgrammableSwitchOutputState: 0 (${this.StatefulProgrammableSwitch.On})`);
1016
982
  }
1017
983
  }
1018
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatefulProgrammableSwitch On: ${this.On}`);
984
+ await this.setOn(Boolean(this.StatefulProgrammableSwitch.On));
985
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatefulProgrammableSwitch On: ${this.StatefulProgrammableSwitch.On}`);
1019
986
  }
1020
987
  else if (this.botDeviceType === 'switch') {
1021
- if (this.On === undefined) {
1022
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
988
+ if (this.Switch.On === undefined) {
989
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Switch.On}`);
1023
990
  }
1024
991
  else {
1025
- this.switchService?.updateCharacteristic(this.hap.Characteristic.On, this.On);
1026
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
992
+ this.Switch.Service.updateCharacteristic(this.hap.Characteristic.On, this.Switch.On);
993
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Switch.On}`);
994
+ await this.setOn(Boolean(this.Switch.On));
1027
995
  }
1028
996
  }
1029
997
  else {
1030
- if (this.On === undefined) {
1031
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
998
+ if (this.Outlet.On === undefined) {
999
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
1032
1000
  }
1033
1001
  else {
1034
- this.outletService?.updateCharacteristic(this.hap.Characteristic.On, this.On);
1035
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
1002
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, this.Outlet.On);
1003
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Outlet.On}`);
1036
1004
  }
1037
1005
  }
1038
- this.accessory.context.On = this.On;
1006
+ await this.setOn(Boolean(this.Outlet.On));
1007
+ this.accessory.context.On = await this.getOn();
1039
1008
  // BatteryLevel
1040
- if (this.BatteryLevel === undefined) {
1041
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
1009
+ if (this.Battery.BatteryLevel === undefined) {
1010
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
1042
1011
  }
1043
1012
  else {
1044
- this.accessory.context.BatteryLevel = this.BatteryLevel;
1045
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
1046
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
1013
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
1014
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
1015
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
1047
1016
  }
1048
1017
  // StatusLowBattery
1049
- if (this.StatusLowBattery === undefined) {
1050
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
1018
+ if (this.Battery.StatusLowBattery === undefined) {
1019
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
1051
1020
  }
1052
1021
  else {
1053
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
1054
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
1055
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
1022
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
1023
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
1024
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
1025
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
1056
1026
  }
1057
1027
  }
1058
1028
  async removeOutletService(accessory) {
1059
1029
  // If outletService still present, then remove first
1060
- this.outletService = this.accessory.getService(this.hap.Service.Outlet);
1061
- if (this.outletService) {
1030
+ this.Outlet.Service = this.accessory.getService(this.hap.Service.Outlet);
1031
+ if (this.Outlet?.Service) {
1062
1032
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Outlet Service`);
1063
1033
  }
1064
- accessory.removeService(this.outletService);
1034
+ accessory.removeService(this.Outlet.Service);
1065
1035
  }
1066
1036
  async removeGarageDoorService(accessory) {
1067
1037
  // If garageDoorService still present, then remove first
1068
- this.garageDoorService = this.accessory.getService(this.hap.Service.GarageDoorOpener);
1069
- if (this.garageDoorService) {
1038
+ this.GarageDoor.Service = this.accessory.getService(this.hap.Service.GarageDoorOpener);
1039
+ if (this.GarageDoor?.Service) {
1070
1040
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Garage Door Service`);
1071
1041
  }
1072
- accessory.removeService(this.garageDoorService);
1042
+ accessory.removeService(this.GarageDoor.Service);
1073
1043
  }
1074
1044
  async removeDoorService(accessory) {
1075
1045
  // If doorService still present, then remove first
1076
- this.doorService = this.accessory.getService(this.hap.Service.Door);
1077
- if (this.doorService) {
1046
+ this.Door.Service = this.accessory.getService(this.hap.Service.Door);
1047
+ if (this.Door?.Service) {
1078
1048
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Door Service`);
1079
1049
  }
1080
- accessory.removeService(this.doorService);
1050
+ accessory.removeService(this.Door.Service);
1081
1051
  }
1082
1052
  async removeLockService(accessory) {
1083
1053
  // If lockService still present, then remove first
1084
- this.lockService = this.accessory.getService(this.hap.Service.LockMechanism);
1085
- if (this.lockService) {
1054
+ this.Lock.Service = this.accessory.getService(this.hap.Service.LockMechanism);
1055
+ if (this.Lock?.Service) {
1086
1056
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Lock Service`);
1087
1057
  }
1088
- accessory.removeService(this.lockService);
1058
+ accessory.removeService(this.Lock.Service);
1089
1059
  }
1090
1060
  async removeFaucetService(accessory) {
1091
1061
  // If faucetService still present, then remove first
1092
- this.faucetService = this.accessory.getService(this.hap.Service.Faucet);
1093
- if (this.faucetService) {
1062
+ this.Faucet.Service = this.accessory.getService(this.hap.Service.Faucet);
1063
+ if (this.Faucet?.Service) {
1094
1064
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Faucet Service`);
1095
1065
  }
1096
- accessory.removeService(this.faucetService);
1066
+ accessory.removeService(this.Faucet.Service);
1097
1067
  }
1098
1068
  async removeFanService(accessory) {
1099
1069
  // If fanService still present, then remove first
1100
- this.fanService = this.accessory.getService(this.hap.Service.Fan);
1101
- if (this.fanService) {
1070
+ this.Fan.Service = this.accessory.getService(this.hap.Service.Fanv2);
1071
+ if (this.Fan?.Service) {
1102
1072
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Fan Service`);
1103
1073
  }
1104
- accessory.removeService(this.fanService);
1074
+ accessory.removeService(this.Fan.Service);
1105
1075
  }
1106
1076
  async removeWindowService(accessory) {
1107
1077
  // If windowService still present, then remove first
1108
- this.windowService = this.accessory.getService(this.hap.Service.Window);
1109
- if (this.windowService) {
1078
+ this.Window.Service = this.accessory.getService(this.hap.Service.Window);
1079
+ if (this.Window?.Service) {
1110
1080
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Service`);
1111
1081
  }
1112
- accessory.removeService(this.windowService);
1082
+ accessory.removeService(this.Window.Service);
1113
1083
  }
1114
1084
  async removeWindowCoveringService(accessory) {
1115
1085
  // If windowCoveringService still present, then remove first
1116
- this.windowCoveringService = this.accessory.getService(this.hap.Service.WindowCovering);
1117
- if (this.windowCoveringService) {
1086
+ this.WindowCovering.Service = this.accessory.getService(this.hap.Service.WindowCovering);
1087
+ if (this.WindowCovering?.Service) {
1118
1088
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Covering Service`);
1119
1089
  }
1120
- accessory.removeService(this.windowCoveringService);
1090
+ accessory.removeService(this.WindowCovering.Service);
1121
1091
  }
1122
1092
  async removeStatefulProgrammableSwitchService(accessory) {
1123
1093
  // If statefulProgrammableSwitchService still present, then remove first
1124
- this.statefulProgrammableSwitchService = this.accessory.getService(this.hap.Service.StatefulProgrammableSwitch);
1125
- if (this.statefulProgrammableSwitchService) {
1094
+ this.StatefulProgrammableSwitch.Service = this.accessory.getService(this.hap.Service.StatefulProgrammableSwitch);
1095
+ if (this.StatefulProgrammableSwitch?.Service) {
1126
1096
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Stateful Programmable Switch Service`);
1127
1097
  }
1128
- accessory.removeService(this.statefulProgrammableSwitchService);
1098
+ accessory.removeService(this.StatefulProgrammableSwitch.Service);
1129
1099
  }
1130
1100
  async removeSwitchService(accessory) {
1131
1101
  // If switchService still present, then remove first
1132
- this.switchService = this.accessory.getService(this.hap.Service.Switch);
1133
- if (this.switchService) {
1102
+ this.Switch.Service = this.accessory.getService(this.hap.Service.Switch);
1103
+ if (this.Switch?.Service) {
1134
1104
  this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Switch Service`);
1135
1105
  }
1136
- accessory.removeService(this.switchService);
1137
- }
1138
- DoublePress(device) {
1139
- if (device.bot?.doublePress) {
1140
- this.doublePress = device.bot?.doublePress;
1141
- this.accessory.context.doublePress = this.doublePress;
1142
- }
1143
- else {
1144
- this.doublePress = 1;
1145
- }
1146
- }
1147
- async stopScanning(switchbot) {
1148
- switchbot.stopScan();
1149
- if (this.BLE_IsConnected) {
1150
- await this.BLEparseStatus();
1151
- await this.updateHomeKitCharacteristics();
1152
- }
1153
- else {
1154
- await this.BLERefreshConnection(switchbot);
1155
- }
1156
- }
1157
- async getCustomBLEAddress(switchbot) {
1158
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
1159
- (async () => {
1160
- // Start to monitor advertisement packets
1161
- await switchbot.startScan({
1162
- model: 'H',
1163
- });
1164
- // Set an event handler
1165
- switchbot.onadvertisement = (ad) => {
1166
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
1167
- };
1168
- await sleep(10000);
1169
- // Stop to monitor
1170
- switchbot.stopScan();
1171
- })();
1172
- }
1173
- }
1174
- async BLEPushConnection() {
1175
- if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
1176
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
1177
- await this.openAPIpushChanges();
1178
- }
1106
+ accessory.removeService(this.Switch.Service);
1179
1107
  }
1180
- async BLERefreshConnection(switchbot) {
1181
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
1182
- + ` ${JSON.stringify(switchbot)}`);
1183
- if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
1184
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
1185
- await this.openAPIRefreshStatus();
1186
- }
1187
- }
1188
- async retry({ max, fn }) {
1189
- return fn().catch(async (e) => {
1190
- if (max === 0) {
1191
- throw e;
1192
- }
1193
- this.infoLog(e);
1194
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
1195
- await sleep(1000);
1196
- return this.retry({ max: max - 1, fn });
1197
- });
1198
- }
1199
- maxRetry() {
1200
- if (this.device.maxRetry) {
1201
- return this.device.maxRetry;
1202
- }
1203
- else {
1204
- return 5;
1108
+ async getOn() {
1109
+ let On;
1110
+ if (this.botDeviceType === 'garagedoor') {
1111
+ On = this.GarageDoor.On ? true : false;
1205
1112
  }
1206
- }
1207
- async PressOrSwitch(device) {
1208
- if (!device.bot?.mode) {
1209
- this.botMode = 'switch';
1210
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
1211
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
1113
+ else if (this.botDeviceType === 'door') {
1114
+ On = this.Door.On ? true : false;
1212
1115
  }
1213
- else if (device.bot?.mode === 'switch') {
1214
- this.botMode = 'switch';
1215
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1116
+ else if (this.botDeviceType === 'window') {
1117
+ On = this.Window.On ? true : false;
1216
1118
  }
1217
- else if (device.bot?.mode === 'press') {
1218
- this.botMode = 'press';
1219
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1119
+ else if (this.botDeviceType === 'windowcovering') {
1120
+ On = this.WindowCovering.On ? true : false;
1220
1121
  }
1221
- else if (device.bot?.mode === 'multipress') {
1222
- this.botMode = 'multipress';
1223
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1122
+ else if (this.botDeviceType === 'lock') {
1123
+ On = this.Lock.On ? true : false;
1224
1124
  }
1225
- else {
1226
- throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
1125
+ else if (this.botDeviceType === 'faucet') {
1126
+ On = this.Faucet.On ? true : false;
1227
1127
  }
1228
- }
1229
- async allowPushChanges(device) {
1230
- if (device.bot?.allowPush) {
1231
- this.allowPush = true;
1128
+ else if (this.botDeviceType === 'fan') {
1129
+ On = this.Fan.On ? true : false;
1232
1130
  }
1233
- else {
1234
- this.allowPush = false;
1131
+ else if (this.botDeviceType === 'stateful') {
1132
+ On = this.StatefulProgrammableSwitch.On ? true : false;
1235
1133
  }
1236
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
1237
- }
1238
- async scan(device) {
1239
- if (device.scanDuration) {
1240
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
1241
- if (this.BLE) {
1242
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
1243
- }
1134
+ else if (this.botDeviceType === 'switch') {
1135
+ On = this.Switch.On ? true : false;
1244
1136
  }
1245
1137
  else {
1246
- this.scanDuration = this.accessory.context.scanDuration = 1;
1247
- if (this.BLE) {
1248
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
1249
- }
1250
- }
1251
- }
1252
- async statusCode(statusCode) {
1253
- switch (statusCode) {
1254
- case 151:
1255
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
1256
- break;
1257
- case 152:
1258
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
1259
- break;
1260
- case 160:
1261
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
1262
- break;
1263
- case 161:
1264
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
1265
- this.offlineOff();
1266
- break;
1267
- case 171:
1268
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
1269
- `Hub: ${this.device.hubDeviceId}`);
1270
- this.offlineOff();
1271
- break;
1272
- case 190:
1273
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
1274
- ` Or command format is invalid, statusCode: ${statusCode}`);
1275
- break;
1276
- case 100:
1277
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
1278
- break;
1279
- case 200:
1280
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
1281
- break;
1282
- case 400:
1283
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
1284
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
1285
- break;
1286
- case 401:
1287
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
1288
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
1289
- break;
1290
- case 403:
1291
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
1292
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
1293
- break;
1294
- case 404:
1295
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
1296
- + `statusCode: ${statusCode}`);
1297
- break;
1298
- case 406:
1299
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
1300
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
1301
- break;
1302
- case 415:
1303
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
1304
- + `header that is not supported by the server, statusCode: ${statusCode}`);
1305
- break;
1306
- case 422:
1307
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
1308
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
1309
- break;
1310
- case 429:
1311
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
1312
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
1313
- break;
1314
- case 500:
1315
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
1316
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
1317
- break;
1318
- default:
1319
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
1320
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
1138
+ On = this.Outlet.On ? true : false;
1321
1139
  }
1140
+ return On;
1322
1141
  }
1323
- async offlineOff() {
1324
- if (this.device.offline) {
1325
- await this.deviceContext();
1326
- await this.updateHomeKitCharacteristics();
1327
- }
1328
- }
1329
- async apiError(e) {
1142
+ async setOn(On) {
1330
1143
  if (this.botDeviceType === 'garagedoor') {
1331
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.TargetDoorState, e);
1332
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, e);
1333
- this.garageDoorService?.updateCharacteristic(this.hap.Characteristic.ObstructionDetected, e);
1144
+ this.GarageDoor.On = On;
1334
1145
  }
1335
1146
  else if (this.botDeviceType === 'door') {
1336
- this.doorService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1337
- this.doorService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1338
- this.doorService?.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1147
+ this.Door.On = On;
1339
1148
  }
1340
1149
  else if (this.botDeviceType === 'window') {
1341
- this.windowService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1342
- this.windowService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1343
- this.windowService?.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1150
+ this.Window.On = On;
1344
1151
  }
1345
1152
  else if (this.botDeviceType === 'windowcovering') {
1346
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1347
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1348
- this.windowCoveringService?.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1153
+ this.WindowCovering.On = On;
1349
1154
  }
1350
1155
  else if (this.botDeviceType === 'lock') {
1351
- this.doorService?.updateCharacteristic(this.hap.Characteristic.LockTargetState, e);
1352
- this.doorService?.updateCharacteristic(this.hap.Characteristic.LockCurrentState, e);
1156
+ this.Lock.On = On;
1353
1157
  }
1354
1158
  else if (this.botDeviceType === 'faucet') {
1355
- this.faucetService?.updateCharacteristic(this.hap.Characteristic.Active, e);
1159
+ this.Faucet.On = On;
1356
1160
  }
1357
1161
  else if (this.botDeviceType === 'fan') {
1358
- this.fanService?.updateCharacteristic(this.hap.Characteristic.On, e);
1162
+ this.Fan.On = On;
1359
1163
  }
1360
1164
  else if (this.botDeviceType === 'stateful') {
1361
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
1362
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
1165
+ this.StatefulProgrammableSwitch.On = On;
1363
1166
  }
1364
1167
  else if (this.botDeviceType === 'switch') {
1365
- this.switchService?.updateCharacteristic(this.hap.Characteristic.On, e);
1168
+ this.Switch.On = On;
1366
1169
  }
1367
1170
  else {
1368
- this.outletService?.updateCharacteristic(this.hap.Characteristic.On, e);
1171
+ this.Outlet.On = On;
1369
1172
  }
1370
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
1371
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
1372
1173
  }
1373
- async deviceType(device) {
1174
+ async getBotConfigSettings(device) {
1175
+ //Bot Device Type
1374
1176
  if (!device.bot?.deviceType && this.accessory.context.deviceType) {
1375
1177
  this.botDeviceType = this.accessory.context.deviceType;
1376
1178
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Type: ${this.botDeviceType}, from Accessory Cache.`);
@@ -1386,43 +1188,36 @@ export class Bot {
1386
1188
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} No Device Type Set, deviceType: ${this.device.bot?.deviceType}`);
1387
1189
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using default deviceType: ${this.botDeviceType}`);
1388
1190
  }
1389
- }
1390
- async deviceContext() {
1391
- if (this.On === undefined) {
1392
- this.On = false;
1393
- this.accessory.context.On = this.On;
1394
- }
1395
- else {
1396
- this.On = this.accessory.context.On;
1191
+ // Bot Mode
1192
+ if (!device.bot?.mode) {
1193
+ this.botMode = 'switch';
1194
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
1195
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
1397
1196
  }
1398
- if (this.BatteryLevel === undefined) {
1399
- this.BatteryLevel = 100;
1197
+ else if (device.bot?.mode === 'switch') {
1198
+ this.botMode = 'switch';
1199
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1400
1200
  }
1401
- else {
1402
- this.BatteryLevel = this.accessory.context.BatteryLevel;
1201
+ else if (device.bot?.mode === 'press') {
1202
+ this.botMode = 'press';
1203
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1403
1204
  }
1404
- if (this.StatusLowBattery === undefined) {
1405
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
1406
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
1205
+ else if (device.bot?.mode === 'multipress') {
1206
+ this.botMode = 'multipress';
1207
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1407
1208
  }
1408
1209
  else {
1409
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
1410
- }
1411
- if (this.FirmwareRevision === undefined) {
1412
- this.FirmwareRevision = this.platform.version;
1413
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
1210
+ throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
1414
1211
  }
1415
- }
1416
- async refreshRate(device) {
1417
- if (device.refreshRate) {
1418
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1419
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1212
+ // Bot Double Press
1213
+ if (device.bot?.doublePress) {
1214
+ this.doublePress = device.bot?.doublePress;
1215
+ this.accessory.context.doublePress = this.doublePress;
1420
1216
  }
1421
- else if (this.platform.config.options.refreshRate) {
1422
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1423
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1217
+ else {
1218
+ this.doublePress = 1;
1424
1219
  }
1425
- // pushRatePress
1220
+ // Bot Press PushRate
1426
1221
  if (device?.bot?.pushRatePress) {
1427
1222
  this.pushRatePress = device?.bot?.pushRatePress;
1428
1223
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Bot pushRatePress: ${this.pushRatePress}`);
@@ -1431,125 +1226,118 @@ export class Bot {
1431
1226
  this.pushRatePress = 15;
1432
1227
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default Bot pushRatePress: ${this.pushRatePress}`);
1433
1228
  }
1434
- }
1435
- async deviceRetry(device) {
1436
- if (!device.maxRetries) {
1437
- this.maxRetries = 5; // Maximum number of retries
1438
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
1439
- }
1440
- else {
1441
- this.maxRetries = device.maxRetries;
1442
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
1443
- }
1444
- if (!device.delayBetweenRetries) {
1445
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
1446
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
1229
+ // Bot Allow Push
1230
+ if (device.bot?.allowPush) {
1231
+ this.allowPush = true;
1447
1232
  }
1448
1233
  else {
1449
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
1450
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
1234
+ this.allowPush = false;
1451
1235
  }
1236
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
1237
+ // Bot Multi Press Count
1238
+ this.multiPressCount = 0;
1452
1239
  }
1453
- async deviceConfig(device) {
1454
- let config = {};
1455
- if (device.bot) {
1456
- config = device.bot;
1457
- }
1458
- if (device.connectionType !== undefined) {
1459
- config['connectionType'] = device.connectionType;
1460
- }
1461
- if (device.external !== undefined) {
1462
- config['external'] = device.external;
1463
- }
1464
- if (device.logging !== undefined) {
1465
- config['logging'] = device.logging;
1466
- }
1467
- if (device.refreshRate !== undefined) {
1468
- config['refreshRate'] = device.refreshRate;
1469
- }
1470
- if (device.scanDuration !== undefined) {
1471
- config['scanDuration'] = device.scanDuration;
1472
- }
1473
- if (device.offline !== undefined) {
1474
- config['offline'] = device.offline;
1475
- }
1476
- if (device.maxRetry !== undefined) {
1477
- config['maxRetry'] = device.maxRetry;
1478
- }
1479
- if (device.webhook !== undefined) {
1480
- config['webhook'] = device.webhook;
1240
+ async BLEPushConnection() {
1241
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
1242
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
1243
+ await this.openAPIpushChanges();
1481
1244
  }
1482
- if (Object.entries(config).length !== 0) {
1483
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1245
+ }
1246
+ async BLERefreshConnection(switchbot) {
1247
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
1248
+ + ` ${JSON.stringify(switchbot)}`);
1249
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
1250
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
1251
+ await this.openAPIRefreshStatus();
1484
1252
  }
1485
1253
  }
1486
- async deviceLogs(device) {
1487
- if (this.platform.debugMode) {
1488
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
1489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1254
+ async offlineOff() {
1255
+ if (this.device.offline) {
1256
+ if (this.botDeviceType === 'garagedoor') {
1257
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.TargetDoorState, this.hap.Characteristic.TargetDoorState.CLOSED);
1258
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, this.hap.Characteristic.CurrentDoorState.CLOSED);
1259
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.ObstructionDetected, false);
1260
+ }
1261
+ else if (this.botDeviceType === 'door') {
1262
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
1263
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
1264
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
1265
+ }
1266
+ else if (this.botDeviceType === 'window') {
1267
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
1268
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
1269
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
1270
+ }
1271
+ else if (this.botDeviceType === 'windowcovering') {
1272
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, 0);
1273
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, 0);
1274
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, this.hap.Characteristic.PositionState.STOPPED);
1275
+ }
1276
+ else if (this.botDeviceType === 'lock') {
1277
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, this.hap.Characteristic.LockTargetState.SECURED);
1278
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, this.hap.Characteristic.LockCurrentState.SECURED);
1279
+ }
1280
+ else if (this.botDeviceType === 'faucet') {
1281
+ this.Faucet.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
1282
+ }
1283
+ else if (this.botDeviceType === 'fan') {
1284
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.On, false);
1285
+ }
1286
+ else if (this.botDeviceType === 'stateful') {
1287
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
1288
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, 0);
1289
+ }
1290
+ else if (this.botDeviceType === 'switch') {
1291
+ this.Switch.Service.updateCharacteristic(this.hap.Characteristic.On, false);
1292
+ }
1293
+ else {
1294
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, false);
1295
+ }
1490
1296
  }
1491
- else if (device.logging) {
1492
- this.deviceLogging = this.accessory.context.logging = device.logging;
1493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1297
+ }
1298
+ async apiError(e) {
1299
+ if (this.botDeviceType === 'garagedoor') {
1300
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.TargetDoorState, e);
1301
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.CurrentDoorState, e);
1302
+ this.GarageDoor.Service.updateCharacteristic(this.hap.Characteristic.ObstructionDetected, e);
1494
1303
  }
1495
- else if (this.platform.config.options?.logging) {
1496
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
1497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1304
+ else if (this.botDeviceType === 'door') {
1305
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1306
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1307
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1498
1308
  }
1499
- else {
1500
- this.deviceLogging = this.accessory.context.logging = 'standard';
1501
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1309
+ else if (this.botDeviceType === 'window') {
1310
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1311
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1312
+ this.Window.Service.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1502
1313
  }
1503
- }
1504
- /**
1505
- * Logging for Device
1506
- */
1507
- successLog(...log) {
1508
- if (this.enablingDeviceLogging()) {
1509
- this.platform.log.success(String(...log));
1314
+ else if (this.botDeviceType === 'windowcovering') {
1315
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.TargetPosition, e);
1316
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.CurrentPosition, e);
1317
+ this.WindowCovering.Service.updateCharacteristic(this.hap.Characteristic.PositionState, e);
1510
1318
  }
1511
- }
1512
- infoLog(...log) {
1513
- if (this.enablingDeviceLogging()) {
1514
- this.platform.log.info(String(...log));
1319
+ else if (this.botDeviceType === 'lock') {
1320
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.LockTargetState, e);
1321
+ this.Door.Service.updateCharacteristic(this.hap.Characteristic.LockCurrentState, e);
1515
1322
  }
1516
- }
1517
- warnLog(...log) {
1518
- if (this.enablingDeviceLogging()) {
1519
- this.platform.log.warn(String(...log));
1323
+ else if (this.botDeviceType === 'faucet') {
1324
+ this.Faucet.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
1520
1325
  }
1521
- }
1522
- debugWarnLog(...log) {
1523
- if (this.enablingDeviceLogging()) {
1524
- if (this.deviceLogging?.includes('debug')) {
1525
- this.platform.log.warn('[DEBUG]', String(...log));
1526
- }
1326
+ else if (this.botDeviceType === 'fan') {
1327
+ this.Fan.Service.updateCharacteristic(this.hap.Characteristic.On, e);
1527
1328
  }
1528
- }
1529
- errorLog(...log) {
1530
- if (this.enablingDeviceLogging()) {
1531
- this.platform.log.error(String(...log));
1329
+ else if (this.botDeviceType === 'stateful') {
1330
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchEvent, e);
1331
+ this.StatefulProgrammableSwitch.Service.updateCharacteristic(this.hap.Characteristic.ProgrammableSwitchOutputState, e);
1532
1332
  }
1533
- }
1534
- debugErrorLog(...log) {
1535
- if (this.enablingDeviceLogging()) {
1536
- if (this.deviceLogging?.includes('debug')) {
1537
- this.platform.log.error('[DEBUG]', String(...log));
1538
- }
1333
+ else if (this.botDeviceType === 'switch') {
1334
+ this.Switch.Service.updateCharacteristic(this.hap.Characteristic.On, e);
1539
1335
  }
1540
- }
1541
- debugLog(...log) {
1542
- if (this.enablingDeviceLogging()) {
1543
- if (this.deviceLogging === 'debug') {
1544
- this.platform.log.info('[DEBUG]', String(...log));
1545
- }
1546
- else {
1547
- this.platform.log.debug(String(...log));
1548
- }
1336
+ else {
1337
+ this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, e);
1549
1338
  }
1550
- }
1551
- enablingDeviceLogging() {
1552
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
1339
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
1340
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
1553
1341
  }
1554
1342
  }
1555
1343
  //# sourceMappingURL=bot.js.map