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

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