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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,84 +1,47 @@
1
- import { sleep } from '../utils.js';
2
- import { interval, Subject } from 'rxjs';
3
- import { skipWhile } from 'rxjs/operators';
1
+
2
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
3
+ *
4
+ * motion.ts: @switchbot/homebridge-switchbot.
5
+ */
6
+ import { deviceBase } from './device.js';
4
7
  import { SwitchBotPlatform } from '../platform.js';
5
- import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
6
- import { device, devicesConfig, serviceData, deviceStatus, Devices, SwitchBotPlatformConfig } from '../settings.js';
8
+ import { Subject, interval, skipWhile } from 'rxjs';
9
+ import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
10
+ import { device, devicesConfig, serviceData, deviceStatus, Devices } from '../settings.js';
7
11
 
8
12
  /**
9
13
  * Platform Accessory
10
14
  * An instance of this class is created for each accessory your platform registers
11
15
  * Each accessory may expose multiple services of different service types.
12
16
  */
13
- export class Motion {
14
- public readonly api: API;
15
- public readonly log: Logging;
16
- public readonly config!: SwitchBotPlatformConfig;
17
- protected readonly hap: HAP;
17
+ export class Motion extends deviceBase {
18
18
  // Services
19
- batteryService: Service;
20
- motionSensorService!: Service;
21
- lightSensorService?: Service;
22
-
23
- // Characteristic Values
24
- BatteryLevel!: CharacteristicValue;
25
- MotionDetected!: CharacteristicValue;
26
- StatusLowBattery!: CharacteristicValue;
27
- FirmwareRevision!: CharacteristicValue;
28
- CurrentAmbientLightLevel!: CharacteristicValue;
29
-
30
- // OpenAPI Others
31
- OpenAPI_BatteryLevel: deviceStatus['battery'];
32
- OpenAPI_FirmwareRevision: deviceStatus['version'];
33
- OpenAPI_MotionDetected: deviceStatus['moveDetected'];
34
- OpenAPI_CurrentAmbientLightLevel: deviceStatus['brightness'];
35
-
36
- // Status
37
- BLE_BatteryLevel!: serviceData['battery'];
38
- BLE_MotionDetected!: serviceData['movement'];
39
- BLE_CurrentAmbientLightLevel!: serviceData['lightLevel'];
40
-
41
- // BLE Others
42
- scanning!: boolean;
43
- BLE_IsConnected?: boolean;
44
-
45
- // Config
46
- set_minLux!: number;
47
- set_maxLux!: number;
48
- scanDuration!: number;
49
- deviceLogging!: string;
50
- deviceRefreshRate!: number;
51
- maxRetries!: number;
52
- delayBetweenRetries!: number;
19
+ private Battery!: {
20
+ Service: Service;
21
+ BatteryLevel: CharacteristicValue;
22
+ StatusLowBattery: CharacteristicValue;
23
+ };
24
+
25
+ private MotionSensor!: {
26
+ Service: Service;
27
+ MotionDetected: CharacteristicValue;
28
+ };
29
+
30
+ private LightSensor?: {
31
+ Service: Service;
32
+ CurrentAmbientLightLevel: CharacteristicValue;
33
+ };
53
34
 
54
35
  // Updates
55
36
  motionUbpdateInProgress!: boolean;
56
37
  doMotionUpdate!: Subject<void>;
57
38
 
58
- // Connection
59
- private readonly OpenAPI: boolean;
60
- private readonly BLE: boolean;
61
-
62
39
  constructor(
63
- private readonly platform: SwitchBotPlatform,
64
- private accessory: PlatformAccessory,
65
- public device: device & devicesConfig,
40
+ readonly platform: SwitchBotPlatform,
41
+ accessory: PlatformAccessory,
42
+ device: device & devicesConfig,
66
43
  ) {
67
- this.api = this.platform.api;
68
- this.log = this.platform.log;
69
- this.config = this.platform.config;
70
- this.hap = this.api.hap;
71
- // Connection
72
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
73
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
74
- // default placeholders
75
- this.deviceLogs(device);
76
- this.scan(device);
77
- this.refreshRate(device);
78
- this.deviceContext();
79
- this.deviceRetry(device);
80
- this.deviceConfig(device);
81
-
44
+ super(platform, accessory, device);
82
45
  // this is subject we use to track when we need to POST changes to the SwitchBot API
83
46
  this.doMotionUpdate = new Subject();
84
47
  this.motionUbpdateInProgress = false;
@@ -86,54 +49,45 @@ export class Motion {
86
49
  // Retrieve initial values and updateHomekit
87
50
  this.refreshStatus();
88
51
 
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, 'W1101500')
94
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
95
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
52
+ if (this.MotionSensor.MotionDetected === undefined) {
53
+ this.MotionSensor.MotionDetected = false;
54
+ } else {
55
+ this.MotionSensor.MotionDetected = this.accessory.context.MotionDetected;
56
+ }
96
57
 
97
58
  // get the Battery service if it exists, otherwise create a new Motion service
98
59
  // you can create multiple services for each accessory
99
- const motionSensorService = `${accessory.displayName} Motion Sensor`;
100
- (this.motionSensorService = accessory.getService(this.hap.Service.MotionSensor)
101
- || accessory.addService(this.hap.Service.MotionSensor)), motionSensorService;
60
+ const MotionSensorService = `${accessory.displayName} Motion Sensor`;
61
+ (this.MotionSensor.Service = accessory.getService(this.hap.Service.MotionSensor)
62
+ || accessory.addService(this.hap.Service.MotionSensor)), MotionSensorService;
102
63
 
103
- this.motionSensorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
104
- if (!this.motionSensorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
105
- this.motionSensorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
106
- }
64
+ this.MotionSensor.Service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
107
65
  // each service must implement at-minimum the "required characteristics" for the given service type
108
66
  // see https://developers.homebridge.io/#/service/MotionSensor
109
67
 
110
68
  // Light Sensor Service
111
69
  if (device.motion?.hide_lightsensor) {
112
70
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
113
- this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor);
114
- accessory.removeService(this.lightSensorService!);
115
- } else if (!this.lightSensorService) {
71
+ this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service;
72
+ accessory.removeService(this.LightSensor!.Service);
73
+ } else if (!this.LightSensor!.Service) {
116
74
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
117
- const lightSensorService = `${accessory.displayName} Light Sensor`;
118
- (this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
119
- || this.accessory.addService(this.hap.Service.LightSensor)), lightSensorService;
75
+ const LightSensorService = `${accessory.displayName} Light Sensor`;
76
+ (this.LightSensor!.Service = this.accessory.getService(this.hap.Service.LightSensor)
77
+ || this.accessory.addService(this.hap.Service.LightSensor)), LightSensorService;
120
78
 
121
- this.lightSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Light Sensor`);
122
- this.lightSensorService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
79
+ this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, LightSensorService);
123
80
  } else {
124
81
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
125
82
  }
126
83
 
127
84
  // Battery Service
128
- const batteryService = `${accessory.displayName} Battery`;
129
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
130
- || accessory.addService(this.hap.Service.Battery)), batteryService;
85
+ const BatteryService = `${accessory.displayName} Battery`;
86
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
87
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
131
88
 
132
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
133
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
134
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
135
- }
136
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
89
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
90
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
137
91
 
138
92
  // Retrieve initial values and updateHomekit
139
93
  this.updateHomeKitCharacteristics();
@@ -152,12 +106,12 @@ export class Motion {
152
106
  try {
153
107
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
154
108
  const { detectionState } = context;
155
- const { MotionDetected } = this;
109
+ const { MotionDetected } = this.MotionSensor;
156
110
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
157
111
  '(detectionState) = ' +
158
112
  `Webhook:(${detectionState}), ` +
159
113
  `current:(${MotionDetected})`);
160
- this.MotionDetected = detectionState === 'DETECTED' ? true : false;
114
+ this.MotionSensor.MotionDetected = detectionState === 'DETECTED' ? true : false;
161
115
  this.updateHomeKitCharacteristics();
162
116
  } catch (e: any) {
163
117
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
@@ -167,103 +121,86 @@ export class Motion {
167
121
  }
168
122
  }
169
123
 
170
- /**
171
- * Parse the device status from the SwitchBot api
172
- */
173
- async parseStatus(): Promise<void> {
174
- if (!this.device.enableCloudService && this.OpenAPI) {
175
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
176
- } else if (this.BLE) {
177
- await this.BLEparseStatus();
178
- } else if (this.OpenAPI && this.platform.config.credentials?.token) {
179
- await this.openAPIparseStatus();
180
- } else {
181
- await this.offlineOff();
182
- this.debugWarnLog(
183
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
184
- );
185
- }
186
- }
187
-
188
- async BLEparseStatus(): Promise<void> {
124
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
189
125
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
190
126
  // Movement
191
- this.MotionDetected = this.BLE_MotionDetected!;
192
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
193
- if (this.MotionDetected !== this.accessory.context.MotionDetected && this.MotionDetected) {
127
+ this.MotionSensor.MotionDetected = serviceData.movement!;
128
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
129
+ if (this.MotionSensor.MotionDetected !== this.accessory.context.MotionDetected && this.MotionSensor.MotionDetected) {
194
130
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Detected Motion`);
195
131
  }
196
132
  // Light Level
197
133
  if (!this.device.motion?.hide_lightsensor) {
198
- this.set_minLux = this.minLux();
199
- this.set_maxLux = this.maxLux();
200
- switch (this.BLE_CurrentAmbientLightLevel) {
134
+ const set_minLux = await this.minLux();
135
+ const set_maxLux = await this.maxLux();
136
+ switch (serviceData.lightLevel) {
201
137
  case 'dark':
202
138
  case 1:
203
- this.CurrentAmbientLightLevel = this.set_minLux;
139
+ this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
204
140
  break;
205
141
  default:
206
- this.CurrentAmbientLightLevel = this.set_maxLux;
142
+ this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
207
143
  }
208
- this.debugLog(
209
- `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.BLE_CurrentAmbientLightLevel},` +
210
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
144
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},`
145
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`,
211
146
  );
212
- if (this.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
213
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
147
+ if (this.LightSensor!.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
148
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName}`
149
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
214
150
  }
215
151
  }
216
152
  // Battery
217
- if (this.BLE_BatteryLevel === undefined) {
218
- this.BLE_BatteryLevel = 100;
153
+ if (serviceData.battery === undefined) {
154
+ serviceData.battery = 100;
219
155
  }
220
- this.BatteryLevel = this.BLE_BatteryLevel!;
221
- if (this.BatteryLevel < 10) {
222
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
156
+ this.Battery.BatteryLevel = serviceData.battery!;
157
+ if (this.Battery.BatteryLevel < 10) {
158
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
223
159
  } else {
224
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
160
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
225
161
  }
226
162
  this.debugLog(
227
- `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
163
+ `${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
164
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`,
228
165
  );
229
166
  }
230
167
 
231
- async openAPIparseStatus(): Promise<void> {
168
+ async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
232
169
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
233
170
  // Motion State
234
- this.MotionDetected = this.OpenAPI_MotionDetected!;
235
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
171
+ this.MotionSensor.MotionDetected = deviceStatus.body.moveDetected!;
172
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
236
173
  // Light Level
237
174
  if (!this.device.motion?.hide_lightsensor) {
238
- this.set_minLux = this.minLux();
239
- this.set_maxLux = this.maxLux();
240
- switch (this.OpenAPI_CurrentAmbientLightLevel) {
175
+ const set_minLux = await this.minLux();
176
+ const set_maxLux = await this.maxLux();
177
+ switch (deviceStatus.body.brightness) {
241
178
  case 'dim':
242
- this.CurrentAmbientLightLevel = this.set_minLux;
179
+ this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
243
180
  break;
244
181
  case 'bright':
245
182
  default:
246
- this.CurrentAmbientLightLevel = this.set_maxLux;
183
+ this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
247
184
  }
248
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
185
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
186
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
249
187
  }
250
188
 
251
189
  // Battery
252
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
253
- if (this.BatteryLevel < 10) {
254
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
190
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
191
+ if (this.Battery.BatteryLevel < 10) {
192
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
255
193
  } else {
256
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
194
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
257
195
  }
258
- if (Number.isNaN(this.BatteryLevel)) {
259
- this.BatteryLevel = 100;
196
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
197
+ this.Battery.BatteryLevel = 100;
260
198
  }
261
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
262
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
199
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
200
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
263
201
 
264
202
  // FirmwareRevision
265
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
266
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
203
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
267
204
  }
268
205
 
269
206
  /**
@@ -289,115 +226,44 @@ export class Motion {
289
226
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
290
227
  const switchbot = await this.platform.connectBLE();
291
228
  // Convert to BLE Address
292
- this.device.bleMac =
293
- this.device.customBLEaddress ||
294
- this.device
295
- .deviceId!.match(/.{1,2}/g)!
296
- .join(':')
297
- .toLowerCase();
229
+ this.device.bleMac = this.device
230
+ .deviceId!.match(/.{1,2}/g)!
231
+ .join(':')
232
+ .toLowerCase();
298
233
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
299
234
  this.getCustomBLEAddress(switchbot);
300
235
  // Start to monitor advertisement packets
301
236
  (async () => {
302
237
  // Start to monitor advertisement packets
303
- await switchbot.startScan({
304
- model: 's',
305
- id: this.device.bleMac,
306
- });
238
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
307
239
  // Set an event handler
308
240
  switchbot.onadvertisement = (ad: any) => {
309
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
310
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
311
- if (this.device.bleMac === ad.address && ad.serviceData.model === 's') {
241
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
242
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
243
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
312
244
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
313
- this.BLE_MotionDetected = ad.serviceData.movement;
314
- this.BLE_BatteryLevel = ad.serviceData.battery;
315
- this.BLE_CurrentAmbientLightLevel = ad.serviceData.lightLevel;
316
245
  } else {
317
246
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
318
247
  }
319
248
  };
320
- // Wait 1 seconds
249
+ // Wait 10 seconds
321
250
  await switchbot.wait(this.scanDuration * 1000);
322
251
  // Stop to monitor
323
252
  await switchbot.stopScan();
324
253
  // Update HomeKit
325
- await this.BLEparseStatus();
254
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
326
255
  await this.updateHomeKitCharacteristics();
327
256
  })();
328
- /*if (switchbot !== false) {
329
- await switchbot
330
- .startScan({
331
- model: 's',
332
- id: this.device.bleMac,
333
- })
334
- .then(async () => {
335
- return await this.retry({
336
- max: this.maxRetry(),
337
- fn: async () => {
338
- // Set an event handler
339
- this.scanning = true;
340
- switchbot.onadvertisement = async (ad: ad) => {
341
- this.debugLog(
342
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
343
- ` BLE Address Found: ${ad.address}`,
344
- );
345
- this.BLE_MotionDetected = ad.serviceData.movement;
346
- this.BLE_BatteryLevel = ad.serviceData.battery;
347
- this.BLE_CurrentAmbientLightLevel = ad.serviceData.lightLevel;
348
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
349
- this.debugLog(
350
- `${this.device.deviceType}: ${this.accessory.displayName} movement: ${ad.serviceData.movement},`
351
- + ` lightLevel: ${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`,
352
- );
353
-
354
- if (ad.serviceData) {
355
- this.BLE_IsConnected = true;
356
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
357
- this.debugErrorLog('1');
358
- await this.stopScanning(switchbot);
359
- this.scanning = false;
360
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} scanning: ${this.scanning}`);
361
- } else {
362
- this.BLE_IsConnected = false;
363
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
364
- }
365
- };
366
- // Wait
367
- return await sleep(this.scanDuration * 1000);
368
- },
369
- });
370
- })
371
- .then(async () => {
372
- // Stop to monitor
373
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} scanning: ${this.scanning}`);
374
- if (this.scanning) {
375
- this.debugErrorLog('2');
376
- await this.stopScanning(switchbot);
377
- }
378
- })
379
- .catch(async (e: any) => {
380
- this.apiError(e);
381
- this.errorLog(
382
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
383
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
384
- );
385
- this.debugErrorLog('3');
386
- await this.BLERefreshConnection(switchbot);
387
- });
388
- } else {
389
- this.debugErrorLog('4');
257
+ if (switchbot === undefined) {
390
258
  await this.BLERefreshConnection(switchbot);
391
- }*/
259
+ }
392
260
  }
393
261
 
394
262
  async openAPIRefreshStatus(): Promise<void> {
395
263
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
396
264
  try {
397
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries,
398
- `${Devices}/${this.device.deviceId}/status`, {
399
- headers: this.platform.generateHeaders(),
400
- });
265
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
266
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
401
267
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
402
268
  const deviceStatus: any = await body.json();
403
269
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -405,11 +271,7 @@ export class Motion {
405
271
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
406
272
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
407
273
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
408
- this.OpenAPI_MotionDetected = deviceStatus.body.moveDetected;
409
- this.OpenAPI_CurrentAmbientLightLevel = deviceStatus.body.brightness;
410
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
411
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
412
- this.openAPIparseStatus();
274
+ this.openAPIparseStatus(deviceStatus);
413
275
  this.updateHomeKitCharacteristics();
414
276
  } else {
415
277
  this.statusCode(statusCode);
@@ -424,73 +286,49 @@ export class Motion {
424
286
  }
425
287
  }
426
288
 
289
+
427
290
  /**
428
291
  * Updates the status for each of the HomeKit Characteristics
429
292
  */
430
293
  async updateHomeKitCharacteristics(): Promise<void> {
431
- if (this.MotionDetected === undefined) {
432
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
294
+ if (this.MotionSensor.MotionDetected === undefined) {
295
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor.MotionDetected}`);
433
296
  } else {
434
- this.accessory.context.MotionDetected = this.MotionDetected;
435
- this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionDetected);
436
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
297
+ this.accessory.context.MotionDetected = this.MotionSensor.MotionDetected;
298
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionSensor.MotionDetected);
299
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
300
+ + ` MotionDetected: ${this.MotionSensor.MotionDetected}`);
437
301
  }
438
- if (this.BatteryLevel === undefined) {
439
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
302
+ if (this.Battery.BatteryLevel === undefined) {
303
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
440
304
  } else {
441
- this.accessory.context.BatteryLevel = this.BatteryLevel;
442
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
443
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
305
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
306
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
307
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
444
308
  }
445
- if (this.StatusLowBattery === undefined) {
446
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
309
+ if (this.Battery.StatusLowBattery === undefined) {
310
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
447
311
  } else {
448
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
449
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
450
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
312
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
313
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
314
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
315
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
451
316
  }
452
317
  if (this.BLE) {
453
- if (this.CurrentAmbientLightLevel === undefined) {
454
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
318
+ if (this.LightSensor!.CurrentAmbientLightLevel === undefined) {
319
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
320
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
455
321
  } else {
456
- this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
457
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
322
+ this.accessory.context.CurrentAmbientLightLevel = this.LightSensor!.CurrentAmbientLightLevel;
323
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
458
324
  this.debugLog(
459
325
  `${this.device.deviceType}: ${this.accessory.displayName}` +
460
- ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
326
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`,
461
327
  );
462
328
  }
463
329
  }
464
330
  }
465
331
 
466
- async stopScanning(switchbot: any) {
467
- switchbot.stopScan();
468
- if (this.BLE_IsConnected) {
469
- await this.BLEparseStatus();
470
- await this.updateHomeKitCharacteristics();
471
- } else {
472
- await this.BLERefreshConnection(switchbot);
473
- }
474
- }
475
-
476
- async getCustomBLEAddress(switchbot: any) {
477
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
478
- (async () => {
479
- // Start to monitor advertisement packets
480
- await switchbot.startScan({
481
- model: 's',
482
- });
483
- // Set an event handler
484
- switchbot.onadvertisement = (ad: any) => {
485
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
486
- };
487
- await sleep(10000);
488
- // Stop to monitor
489
- switchbot.stopScan();
490
- })();
491
- }
492
- }
493
-
494
332
  async BLERefreshConnection(switchbot: any): Promise<void> {
495
333
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
496
334
  + ` ${JSON.stringify(switchbot)}`);
@@ -500,292 +338,38 @@ export class Motion {
500
338
  }
501
339
  }
502
340
 
503
- async retry({ max, fn }: { max: number; fn: { (): any; (): Promise<any> } }): Promise<null> {
504
- return fn().catch(async (e: any) => {
505
- if (max === 0) {
506
- throw e;
507
- }
508
- this.infoLog(e);
509
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
510
- await sleep(1000);
511
- return this.retry({ max: max - 1, fn });
512
- });
513
- }
514
-
515
- maxRetry(): number {
516
- if (this.device.maxRetry) {
517
- return this.device.maxRetry;
518
- } else {
519
- return 5;
520
- }
521
- }
522
-
523
- minLux(): number {
341
+ async minLux(): Promise<number> {
342
+ let set_minLux: number;
524
343
  if (this.device.motion?.set_minLux) {
525
- this.set_minLux = this.device.motion!.set_minLux!;
344
+ set_minLux = this.device.motion!.set_minLux!;
526
345
  } else {
527
- this.set_minLux = 1;
346
+ set_minLux = 1;
528
347
  }
529
- return this.set_minLux;
348
+ return set_minLux;
530
349
  }
531
350
 
532
- maxLux(): number {
351
+ async maxLux(): Promise<number> {
352
+ let set_maxLux: number;
533
353
  if (this.device.motion?.set_maxLux) {
534
- this.set_maxLux = this.device.motion!.set_maxLux!;
354
+ set_maxLux = this.device.motion!.set_maxLux!;
535
355
  } else {
536
- this.set_maxLux = 6001;
537
- }
538
- return this.set_maxLux;
539
- }
540
-
541
- async scan(device: device & devicesConfig): Promise<void> {
542
- if (device.scanDuration) {
543
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
544
- if (this.BLE) {
545
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
546
- }
547
- } else {
548
- this.scanDuration = this.accessory.context.scanDuration = 1;
549
- if (this.BLE) {
550
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
551
- }
552
- }
553
- }
554
-
555
- async statusCode(statusCode: number): Promise<void> {
556
- switch (statusCode) {
557
- case 151:
558
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
559
- break;
560
- case 152:
561
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
562
- break;
563
- case 160:
564
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
565
- break;
566
- case 161:
567
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
568
- this.offlineOff();
569
- break;
570
- case 171:
571
- this.errorLog(
572
- `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
573
- `Hub: ${this.device.hubDeviceId}`,
574
- );
575
- this.offlineOff();
576
- break;
577
- case 190:
578
- this.errorLog(
579
- `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
580
- ` Or command format is invalid, statusCode: ${statusCode}`,
581
- );
582
- break;
583
- case 100:
584
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
585
- break;
586
- case 200:
587
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
588
- break;
589
- case 400:
590
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
591
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
592
- break;
593
- case 401:
594
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
595
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
596
- break;
597
- case 403:
598
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
599
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
600
- break;
601
- case 404:
602
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
603
- + `statusCode: ${statusCode}`);
604
- break;
605
- case 406:
606
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
607
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
608
- break;
609
- case 415:
610
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
611
- + `header that is not supported by the server, statusCode: ${statusCode}`);
612
- break;
613
- case 422:
614
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
615
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
616
- break;
617
- case 429:
618
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
619
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
620
- break;
621
- case 500:
622
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
623
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
624
- break;
625
- default:
626
- this.infoLog(
627
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
628
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
629
- );
356
+ set_maxLux = 6001;
630
357
  }
358
+ return set_maxLux;
631
359
  }
632
360
 
633
361
  async offlineOff(): Promise<void> {
634
362
  if (this.device.offline) {
635
- await this.deviceContext();
636
- await this.updateHomeKitCharacteristics();
363
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false);
637
364
  }
638
365
  }
639
366
 
640
367
  async apiError(e: any): Promise<void> {
641
- this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
642
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
643
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
368
+ this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
369
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
370
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
644
371
  if (this.BLE) {
645
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
372
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
646
373
  }
647
374
  }
648
-
649
- async deviceContext() {
650
- if (this.MotionDetected === undefined) {
651
- this.MotionDetected = false;
652
- } else {
653
- this.MotionDetected = this.accessory.context.MotionDetected;
654
- }
655
- if (this.FirmwareRevision === undefined) {
656
- this.FirmwareRevision = this.platform.version;
657
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
658
- }
659
- }
660
-
661
- async refreshRate(device: device & devicesConfig): Promise<void> {
662
- if (device.refreshRate) {
663
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
664
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
665
- } else if (this.platform.config.options!.refreshRate) {
666
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
667
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
668
- }
669
- }
670
-
671
- async deviceRetry(device: device & devicesConfig): Promise<void> {
672
- if (!device.maxRetries) {
673
- this.maxRetries = 5; // Maximum number of retries
674
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
675
- } else {
676
- this.maxRetries = device.maxRetries;
677
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
678
- }
679
- if (!device.delayBetweenRetries) {
680
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
681
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
682
- } else {
683
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
684
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
685
- }
686
- }
687
-
688
- async deviceConfig(device: device & devicesConfig): Promise<void> {
689
- let config = {};
690
- if (device.motion) {
691
- config = device.motion;
692
- }
693
- if (device.connectionType !== undefined) {
694
- config['connectionType'] = device.connectionType;
695
- }
696
- if (device.external !== undefined) {
697
- config['external'] = device.external;
698
- }
699
- if (device.logging !== undefined) {
700
- config['logging'] = device.logging;
701
- }
702
- if (device.refreshRate !== undefined) {
703
- config['refreshRate'] = device.refreshRate;
704
- }
705
- if (device.scanDuration !== undefined) {
706
- config['scanDuration'] = device.scanDuration;
707
- }
708
- if (device.offline !== undefined) {
709
- config['offline'] = device.offline;
710
- }
711
- if (device.webhook !== undefined) {
712
- config['webhook'] = device.webhook;
713
- }
714
- if (Object.entries(config).length !== 0) {
715
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
716
- }
717
- }
718
-
719
- async deviceLogs(device: device & devicesConfig): Promise<void> {
720
- if (this.platform.debugMode) {
721
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
722
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
723
- } else if (device.logging) {
724
- this.deviceLogging = this.accessory.context.logging = device.logging;
725
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
726
- } else if (this.platform.config.options?.logging) {
727
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
728
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
729
- } else {
730
- this.deviceLogging = this.accessory.context.logging = 'standard';
731
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
732
- }
733
- }
734
-
735
- /**
736
- * Logging for Device
737
- */
738
- successLog(...log: any[]): void {
739
- if (this.enablingDeviceLogging()) {
740
- this.platform.log.success(String(...log));
741
- }
742
- }
743
-
744
- infoLog(...log: any[]): void {
745
- if (this.enablingDeviceLogging()) {
746
- this.platform.log.info(String(...log));
747
- }
748
- }
749
-
750
- warnLog(...log: any[]): void {
751
- if (this.enablingDeviceLogging()) {
752
- this.platform.log.warn(String(...log));
753
- }
754
- }
755
-
756
- debugWarnLog(...log: any[]): void {
757
- if (this.enablingDeviceLogging()) {
758
- if (this.deviceLogging?.includes('debug')) {
759
- this.platform.log.warn('[DEBUG]', String(...log));
760
- }
761
- }
762
- }
763
-
764
- errorLog(...log: any[]): void {
765
- if (this.enablingDeviceLogging()) {
766
- this.platform.log.error(String(...log));
767
- }
768
- }
769
-
770
- debugErrorLog(...log: any[]): void {
771
- if (this.enablingDeviceLogging()) {
772
- if (this.deviceLogging?.includes('debug')) {
773
- this.platform.log.error('[DEBUG]', String(...log));
774
- }
775
- }
776
- }
777
-
778
- debugLog(...log: any[]): void {
779
- if (this.enablingDeviceLogging()) {
780
- if (this.deviceLogging === 'debug') {
781
- this.platform.log.info('[DEBUG]', String(...log));
782
- } else {
783
- this.platform.log.debug(String(...log));
784
- }
785
- }
786
- }
787
-
788
- enablingDeviceLogging(): boolean {
789
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
790
- }
791
375
  }