@switchbot/homebridge-switchbot 3.5.0-beta.4 → 3.5.0-beta.40

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