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

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