@switchbot/homebridge-switchbot 3.5.0-beta.7 → 3.5.0-beta.70

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