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

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