@switchbot/homebridge-switchbot 3.5.0-beta.3 → 3.5.0-beta.30

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