@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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 +164 -22
  4. package/dist/device/blindtilt.d.ts +21 -96
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +457 -866
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +27 -72
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +851 -913
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +15 -69
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +316 -742
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +15 -76
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +380 -833
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +14 -66
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +267 -602
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +18 -89
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +469 -897
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/device.d.ts +76 -0
  29. package/dist/device/device.d.ts.map +1 -0
  30. package/dist/device/device.js +846 -0
  31. package/dist/device/device.js.map +1 -0
  32. package/dist/device/fan.d.ts +63 -0
  33. package/dist/device/fan.d.ts.map +1 -0
  34. package/dist/device/fan.js +645 -0
  35. package/dist/device/fan.js.map +1 -0
  36. package/dist/device/hub.d.ts +19 -58
  37. package/dist/device/hub.d.ts.map +1 -1
  38. package/dist/device/hub.js +304 -640
  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 +300 -710
  43. package/dist/device/humidifier.js.map +1 -1
  44. package/dist/device/iosensor.d.ts +12 -64
  45. package/dist/device/iosensor.d.ts.map +1 -1
  46. package/dist/device/iosensor.js +225 -643
  47. package/dist/device/iosensor.js.map +1 -1
  48. package/dist/device/lightstrip.d.ts +12 -70
  49. package/dist/device/lightstrip.d.ts.map +1 -1
  50. package/dist/device/lightstrip.js +371 -742
  51. package/dist/device/lightstrip.js.map +1 -1
  52. package/dist/device/lock.d.ts +17 -63
  53. package/dist/device/lock.d.ts.map +1 -1
  54. package/dist/device/lock.js +309 -642
  55. package/dist/device/lock.js.map +1 -1
  56. package/dist/device/meter.d.ts +15 -64
  57. package/dist/device/meter.d.ts.map +1 -1
  58. package/dist/device/meter.js +223 -638
  59. package/dist/device/meter.js.map +1 -1
  60. package/dist/device/meterplus.d.ts +15 -65
  61. package/dist/device/meterplus.d.ts.map +1 -1
  62. package/dist/device/meterplus.js +216 -644
  63. package/dist/device/meterplus.js.map +1 -1
  64. package/dist/device/motion.d.ts +12 -69
  65. package/dist/device/motion.d.ts.map +1 -1
  66. package/dist/device/motion.js +209 -588
  67. package/dist/device/motion.js.map +1 -1
  68. package/dist/device/plug.d.ts +10 -57
  69. package/dist/device/plug.d.ts.map +1 -1
  70. package/dist/device/plug.js +141 -532
  71. package/dist/device/plug.js.map +1 -1
  72. package/dist/device/robotvacuumcleaner.d.ts +13 -63
  73. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  74. package/dist/device/robotvacuumcleaner.js +252 -609
  75. package/dist/device/robotvacuumcleaner.js.map +1 -1
  76. package/dist/device/waterdetector.d.ts +11 -63
  77. package/dist/device/waterdetector.d.ts.map +1 -1
  78. package/dist/device/waterdetector.js +179 -521
  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 +337 -482
  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 +108 -333
  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 +47 -299
  98. package/dist/irdevice/camera.js.map +1 -1
  99. package/dist/irdevice/fan.d.ts +7 -43
  100. package/dist/irdevice/fan.d.ts.map +1 -1
  101. package/dist/irdevice/fan.js +95 -364
  102. package/dist/irdevice/fan.js.map +1 -1
  103. package/dist/irdevice/irdevice.d.ts +46 -0
  104. package/dist/irdevice/irdevice.d.ts.map +1 -0
  105. package/dist/irdevice/irdevice.js +393 -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 +121 -376
  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 +441 -584
  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 +94 -353
  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 +48 -299
  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 +50 -305
  126. package/dist/irdevice/waterheater.js.map +1 -1
  127. package/dist/platform.d.ts +22 -8
  128. package/dist/platform.d.ts.map +1 -1
  129. package/dist/platform.js +342 -191
  130. package/dist/platform.js.map +1 -1
  131. package/dist/settings.d.ts +66 -18
  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 +95 -0
  136. package/dist/utils.d.ts.map +1 -1
  137. package/dist/utils.js +657 -1
  138. package/dist/utils.js.map +1 -1
  139. package/eslint.config.js +96 -0
  140. package/package.json +20 -16
  141. package/src/custom.d.ts +2 -1
  142. package/src/device/blindtilt.ts +488 -937
  143. package/src/device/bot.ts +907 -1016
  144. package/src/device/ceilinglight.ts +330 -798
  145. package/src/device/colorbulb.ts +394 -904
  146. package/src/device/contact.ts +298 -637
  147. package/src/device/curtain.ts +496 -954
  148. package/src/device/device.ts +872 -0
  149. package/src/device/fan.ts +692 -0
  150. package/src/device/hub.ts +338 -671
  151. package/src/device/humidifier.ts +330 -777
  152. package/src/device/iosensor.ts +258 -671
  153. package/src/device/lightstrip.ts +394 -809
  154. package/src/device/lock.ts +345 -700
  155. package/src/device/meter.ts +256 -666
  156. package/src/device/meterplus.ts +248 -672
  157. package/src/device/motion.ts +234 -622
  158. package/src/device/plug.ts +152 -572
  159. package/src/device/robotvacuumcleaner.ts +273 -653
  160. package/src/device/waterdetector.ts +198 -549
  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 +368 -540
  165. package/src/irdevice/airpurifier.ts +134 -369
  166. package/src/irdevice/camera.ts +60 -324
  167. package/src/irdevice/fan.ts +111 -392
  168. package/src/irdevice/irdevice.ts +406 -0
  169. package/src/irdevice/light.ts +159 -425
  170. package/src/irdevice/other.ts +509 -661
  171. package/src/irdevice/tv.ts +123 -388
  172. package/src/irdevice/vacuumcleaner.ts +61 -324
  173. package/src/irdevice/waterheater.ts +63 -333
  174. package/src/platform.ts +395 -259
  175. package/src/settings.ts +120 -610
  176. package/src/utils.ts +661 -1
  177. package/src/device/waterdetector.json +0 -11
package/src/device/hub.ts CHANGED
@@ -1,106 +1,69 @@
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 { convertUnits } from '../utils.js';
8
+ import { Subject, interval, skipWhile } from 'rxjs';
9
+
10
+ import type { SwitchBotPlatform } from '../platform.js';
11
+ import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
12
+ import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
13
+
14
+ export class Hub extends deviceBase {
15
15
  // 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;
16
+ private LightSensor?: {
17
+ Name: CharacteristicValue;
18
+ Service: Service;
19
+ CurrentAmbientLightLevel: CharacteristicValue;
20
+ };
21
+
22
+ private HumiditySensor?: {
23
+ Name: CharacteristicValue;
24
+ Service: Service;
25
+ CurrentRelativeHumidity: CharacteristicValue;
26
+ };
27
+
28
+ private TemperatureSensor?: {
29
+ Name: CharacteristicValue;
30
+ Service: Service;
31
+ CurrentTemperature: CharacteristicValue;
32
+ };
33
+
34
+ // Updates
35
+ hubUpdateInProgress!: boolean;
36
+ doHubUpdate!: Subject<void>;
53
37
 
54
38
  constructor(
55
- private readonly platform: SwitchBotPlatform,
56
- private accessory: PlatformAccessory,
57
- public device: device & devicesConfig,
39
+ readonly platform: SwitchBotPlatform,
40
+ accessory: PlatformAccessory,
41
+ device: device & devicesConfig,
58
42
  ) {
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;
43
+ super(platform, accessory, device);
44
+ // this is subject we use to track when we need to POST changes to the SwitchBot API
45
+ this.doHubUpdate = new Subject();
46
+ this.hubUpdateInProgress = false;
76
47
 
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
48
+ // Initialize Temperature Sensor Service
89
49
  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`);
50
+ if (this.TemperatureSensor) {
51
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
52
+ this.TemperatureSensor.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
53
+ accessory.removeService(this.TemperatureSensor.Service);
102
54
  }
103
- this.temperatureService
55
+ } else {
56
+ accessory.context.TemperatureSensor = accessory.context.TemperatureSensor ?? {};
57
+ this.TemperatureSensor = {
58
+ Name: accessory.context.TemperatureSensor.Name ?? `${accessory.displayName} Temperature Sensor`,
59
+ Service: accessory.getService(this.hap.Service.TemperatureSensor) ?? this.accessory.addService(this.hap.Service.TemperatureSensor) as Service,
60
+ CurrentTemperature: accessory.context.CurrentTemperature ?? 0,
61
+ };
62
+ accessory.context.TemperatureSensor = this.TemperatureSensor as object;
63
+
64
+ // Initialize Temperature Sensor Characteristic
65
+ this.TemperatureSensor.Service
66
+ .setCharacteristic(this.hap.Characteristic.Name, this.TemperatureSensor.Name)
104
67
  .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
105
68
  .setProps({
106
69
  unit: Units['CELSIUS'],
@@ -110,354 +73,231 @@ export class Hub {
110
73
  minStep: 0.1,
111
74
  })
112
75
  .onGet(() => {
113
- return this.CurrentTemperature!;
76
+ return this.TemperatureSensor!.CurrentTemperature;
114
77
  });
115
- } else {
116
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
117
78
  }
118
79
 
119
- // Humidity Sensor Service
80
+ // Initialize Humidity Sensor Service
120
81
  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`);
82
+ if (this.HumiditySensor) {
83
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
84
+ this.HumiditySensor.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service;
85
+ accessory.removeService(this.HumiditySensor.Service);
133
86
  }
134
- this.humidityService
87
+ } else {
88
+ accessory.context.HumiditySensor = accessory.context.HumiditySensor ?? {};
89
+ this.HumiditySensor = {
90
+ Name: accessory.context.HumiditySensor.Name ?? `${accessory.displayName} Humidity Sensor`,
91
+ Service: accessory.getService(this.hap.Service.HumiditySensor) ?? this.accessory.addService(this.hap.Service.HumiditySensor) as Service,
92
+ CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity ?? 0,
93
+ };
94
+ accessory.context.HumiditySensor = this.HumiditySensor as object;
95
+
96
+ // Initialize Humidity Sensor Characteristics
97
+ this.HumiditySensor!.Service
98
+ .setCharacteristic(this.hap.Characteristic.Name, this.HumiditySensor.Name)
135
99
  .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
136
100
  .setProps({
137
101
  minStep: 0.1,
138
102
  })
139
103
  .onGet(() => {
140
- return this.CurrentRelativeHumidity;
104
+ return this.HumiditySensor!.CurrentRelativeHumidity;
141
105
  });
142
- } else {
143
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
144
106
  }
145
107
 
146
- // Light Sensor Service
108
+ // Initialize Light Sensor Service
147
109
  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`);
110
+ if (this.LightSensor) {
111
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
112
+ this.LightSensor.Service = this.accessory.getService(this.hap.Service.LightSensor) as Service;
113
+ accessory.removeService(this.LightSensor.Service);
114
+ }
159
115
  } else {
160
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
116
+ accessory.context.LightSensor = accessory.context.LightSensor ?? {};
117
+ this.LightSensor = {
118
+ Name: accessory.context.LightSensor.Name ?? `${accessory.displayName} Light Sensor`,
119
+ Service: accessory.getService(this.hap.Service.LightSensor) ?? this.accessory.addService(this.hap.Service.LightSensor) as Service,
120
+ CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
121
+ };
122
+ accessory.context.LightSensor = this.LightSensor as object;
123
+
124
+ // Initialize Light Sensor Characteristics
125
+ this.LightSensor!.Service
126
+ .setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name)
127
+ .getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
128
+ .setProps({
129
+ minStep: 1,
130
+ })
131
+ .onGet(() => {
132
+ return this.LightSensor!.CurrentAmbientLightLevel;
133
+ });
161
134
  }
162
135
 
136
+ // Retrieve initial values and updateHomekit
137
+ this.refreshStatus();
138
+
163
139
  // Retrieve initial values and update Homekit
164
140
  this.updateHomeKitCharacteristics();
165
141
 
166
142
  // Start an update interval
167
143
  interval(this.deviceRefreshRate * 1000)
144
+ .pipe(skipWhile(() => this.hubUpdateInProgress))
168
145
  .subscribe(async () => {
169
146
  await this.refreshStatus();
170
147
  });
171
148
 
172
149
  //regisiter webhook event handler
173
- if (this.device.webhook) {
174
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
175
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
176
- try {
177
- 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();
276
- }
277
- } catch (e: any) {
278
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
279
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
280
- }
281
- };
282
- }
150
+ this.registerWebhook(accessory, device);
151
+
283
152
  }
284
153
 
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
- }
154
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
155
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
156
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(serviceData)}`);
299
157
  }
300
158
 
301
- async openAPIparseStatus(): Promise<void> {
159
+ async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
302
160
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
303
161
  // CurrentRelativeHumidity
304
162
  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}%`);
163
+ this.HumiditySensor!.CurrentRelativeHumidity = Number(deviceStatus.body.humidity);
164
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
307
165
  }
308
166
 
309
167
  // CurrentTemperature
310
168
  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`);
169
+ this.TemperatureSensor!.CurrentTemperature = Number(deviceStatus.body.temperature);
170
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
313
171
  }
314
172
 
315
173
  // Brightness
316
174
  if (!this.device.hub?.hide_lightsensor) {
317
175
  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
- }
406
- this.debugLog(
407
- `${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${this.OpenAPI_CurrentAmbientLightLevel},` +
408
- ` CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
409
- );
176
+ const set_minLux = this.device.curtain?.set_minLux ?? 1;
177
+ const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
178
+ const spaceBetweenLevels = 19;
179
+ const lightLevel = deviceStatus.body.lightLevel;
180
+ await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
181
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${deviceStatus.body.lightLevel},`
182
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
410
183
  }
411
184
  if (!this.device.hub?.hide_lightsensor) {
412
- this.lightSensorService?.setCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
185
+ this.LightSensor!.Service.setCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
413
186
  }
414
187
  }
415
188
 
416
- // FirmwareRevision
417
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
418
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
189
+ // Firmware Version
190
+ const version = deviceStatus.body.version?.toString();
191
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
192
+ if (deviceStatus.body.version) {
193
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
194
+ this.accessory
195
+ .getService(this.hap.Service.AccessoryInformation)!
196
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
197
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
198
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
199
+ .updateValue(deviceVersion);
200
+ this.accessory.context.deviceVersion = deviceVersion;
201
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
202
+ }
419
203
  }
420
204
 
205
+ /**
206
+ * Asks the SwitchBot API for the latest device information
207
+ */
421
208
  async refreshStatus(): Promise<void> {
422
- if (this.BLE) {
423
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE not supported`);
209
+ if (!this.device.enableCloudService && this.OpenAPI) {
210
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
211
+ } else if (this.BLE) {
212
+ await this.BLERefreshStatus();
424
213
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
425
214
  await this.openAPIRefreshStatus();
426
215
  } else {
427
216
  await this.offlineOff();
428
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type: OpenAPI, refreshStatus will not happen.`);
217
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
218
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
219
+ }
220
+ }
221
+
222
+ async BLERefreshStatus(): Promise<void> {
223
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
224
+ const switchbot = await this.switchbotBLE();
225
+
226
+ if (switchbot === undefined) {
227
+ await this.BLERefreshConnection(switchbot);
228
+ } else {
229
+ // Start to monitor advertisement packets
230
+ (async () => {
231
+ // Start to monitor advertisement packets
232
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
233
+ // Update HomeKit
234
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
235
+ await this.BLEparseStatus(serviceData);
236
+ await this.updateHomeKitCharacteristics();
237
+ } else {
238
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
239
+ await this.BLERefreshConnection(switchbot);
240
+ }
241
+ })();
429
242
  }
430
243
  }
431
244
 
432
245
  async openAPIRefreshStatus(): Promise<void> {
433
246
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
434
247
  try {
435
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
436
- headers: this.platform.generateHeaders(),
437
- });
438
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
248
+ const { body, statusCode } = await this.deviceRefreshStatus();
439
249
  const deviceStatus: any = await body.json();
440
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
441
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
250
+ await this.refreshStatusCodes(statusCode, deviceStatus);;
442
251
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
443
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
444
- + `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();
450
- this.updateHomeKitCharacteristics();
252
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
253
+ await this.openAPIparseStatus(deviceStatus);
254
+ await this.updateHomeKitCharacteristics();
451
255
  } else {
452
- this.statusCode(statusCode);
453
- this.statusCode(deviceStatus.statusCode);
256
+ await this.statusCodes(statusCode, deviceStatus);
454
257
  }
455
258
  } catch (e: any) {
456
259
  this.apiError(e);
457
- this.errorLog(
458
- `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}` +
459
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
460
- );
260
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
261
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
262
+ }
263
+ }
264
+
265
+ async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
266
+ if (device.webhook) {
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
268
+ this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
269
+ try {
270
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
271
+ const { temperature, humidity, lightLevel } = context;
272
+ const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
273
+ const { CurrentAmbientLightLevel } = this.LightSensor || { CurrentAmbientLightLevel: undefined };
274
+ const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
275
+ if (context.scale !== 'CELCIUS' && device.hub?.convertUnitTo === undefined) {
276
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} received a non-CELCIUS Webhook scale: `
277
+ + `${context.scale}, Use the *convertUnitsTo* config under Hub settings, if displaying incorrectly in HomeKit.`);
278
+ }
279
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
280
+ '(scale, temperature, humidity, lightLevel) = ' +
281
+ `Webhook:(${context.scale}, ${convertUnits(temperature, context.scale, device.hub?.convertUnitTo)}, ${humidity}, ${lightLevel}), `
282
+ + `current:(${CurrentTemperature}, ${CurrentRelativeHumidity}, ${CurrentAmbientLightLevel})`);
283
+ if (!this.device.hub?.hide_humidity) {
284
+ this.HumiditySensor!.CurrentRelativeHumidity = humidity;
285
+ }
286
+ if (!this.device.hub?.hide_temperature) {
287
+ this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.hub?.convertUnitTo);
288
+ }
289
+ if (!this.device.hub?.hide_lightsensor) {
290
+ const set_minLux = this.device.curtain?.set_minLux ?? 1;
291
+ const set_maxLux = this.device.curtain?.set_maxLux ?? 6001;
292
+ const spaceBetweenLevels = 19;
293
+ await this.getLightLevel(lightLevel, set_minLux, set_maxLux, spaceBetweenLevels);
294
+ }
295
+ this.updateHomeKitCharacteristics();
296
+ } catch (e: any) {
297
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
298
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
299
+ }
300
+ };
461
301
  }
462
302
  }
463
303
 
@@ -471,56 +311,58 @@ export class Hub {
471
311
 
472
312
  // CurrentRelativeHumidity
473
313
  if (!this.device.hub?.hide_humidity) {
474
- if (this.CurrentRelativeHumidity === undefined) {
475
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
314
+ if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
315
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
316
+ + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
476
317
  } else {
477
318
  if (this.device.mqttURL) {
478
- mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
319
+ mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
479
320
  }
480
321
  if (this.device.history) {
481
- entry['humidity'] = this.CurrentRelativeHumidity;
322
+ entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
482
323
  }
483
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
484
- this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
324
+ this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
325
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
326
+ this.HumiditySensor!.CurrentRelativeHumidity);
485
327
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
486
- + `updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
328
+ + `updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
487
329
  }
488
330
  }
489
331
 
490
332
  // CurrentTemperature
491
333
  if (!this.device.hub?.hide_temperature) {
492
- if (this.CurrentTemperature === undefined) {
493
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
334
+ if (this.TemperatureSensor!.CurrentTemperature === undefined) {
335
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
494
336
  } else {
495
337
  if (this.device.mqttURL) {
496
- mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
338
+ mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
497
339
  }
498
340
  if (this.device.history) {
499
- entry['temp'] = this.CurrentTemperature;
341
+ entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
500
342
  }
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}`);
343
+ this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
344
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
345
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
346
+ + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
504
347
  }
505
348
  }
506
349
 
507
350
  // CurrentAmbientLightLevel
508
351
  if (!this.device.hub?.hide_lightsensor) {
509
- if (this.CurrentAmbientLightLevel === undefined) {
510
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
352
+ if (this.LightSensor!.CurrentAmbientLightLevel === undefined) {
353
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
354
+ + ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
511
355
  } else {
512
356
  if (this.device.mqttURL) {
513
- mqttmessage.push(`"light": ${this.CurrentAmbientLightLevel}`);
357
+ mqttmessage.push(`"light": ${this.LightSensor!.CurrentAmbientLightLevel}`);
514
358
  }
515
359
  if (this.device.history) {
516
- entry['lux'] = this.CurrentAmbientLightLevel;
360
+ entry['lux'] = this.LightSensor!.CurrentAmbientLightLevel;
517
361
  }
518
- this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
519
- this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
520
- this.debugLog(
521
- `${this.device.deviceType}: ${this.accessory.displayName} `
522
- + `updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
523
- );
362
+ this.accessory.context.CurrentAmbientLightLevel = this.LightSensor!.CurrentAmbientLightLevel;
363
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor!.CurrentAmbientLightLevel);
364
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} `
365
+ + `updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
524
366
  }
525
367
  }
526
368
 
@@ -528,7 +370,7 @@ export class Hub {
528
370
  if (this.device.mqttURL) {
529
371
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
530
372
  }
531
- if (Number(this.CurrentRelativeHumidity) > 0) {
373
+ if (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0) {
532
374
  // reject unreliable data
533
375
  if (this.device.history) {
534
376
  this.historyService?.addEntry(entry);
@@ -536,301 +378,126 @@ export class Hub {
536
378
  }
537
379
  }
538
380
 
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)}`);
381
+ async BLERefreshConnection(switchbot: any): Promise<void> {
382
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
383
+ + ` ${JSON.stringify(switchbot)}`);
384
+ if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
385
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Refresh Status`);
386
+ await this.openAPIRefreshStatus();
387
+ }
551
388
  }
552
389
 
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}`);
390
+ async offlineOff(): Promise<void> {
391
+ if (this.device.offline) {
392
+ if (!this.device.hub?.hide_temperature) {
393
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.accessory.context.CurrentTemperature);
394
+ }
395
+ if (!this.device.hub?.hide_humidity) {
396
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
397
+ this.accessory.context.CurrentRelativeHumidity);
398
+ }
399
+ if (!this.device.hub?.hide_lightsensor) {
400
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel,
401
+ this.accessory.context.CurrentAmbientLightLevel);
568
402
  }
569
403
  }
570
404
  }
571
405
 
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;
406
+ async apiError(e: any): Promise<void> {
407
+ if (!this.device.hub?.hide_temperature) {
408
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
409
+ }
410
+ if (!this.device.hub?.hide_humidity) {
411
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
412
+ }
413
+ if (!this.device.hub?.hide_lightsensor) {
414
+ this.LightSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
415
+ }
587
416
  }
588
417
 
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}`);
418
+ async getLightLevel(lightLevel: any, set_minLux: number, set_maxLux: number, spaceBetweenLevels: number) {
419
+ switch (lightLevel) {
420
+ case 1:
421
+ this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
422
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
423
+ break;
424
+ case 2:
425
+ this.LightSensor!.CurrentAmbientLightLevel = (set_maxLux - set_minLux) / spaceBetweenLevels;
426
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel},`
427
+ + ` Calculation: ${(set_maxLux - set_minLux) / spaceBetweenLevels}`);
428
+ break;
429
+ case 3:
430
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 2;
431
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
593
432
  break;
594
- case 152:
595
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
433
+ case 4:
434
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 3;
435
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
596
436
  break;
597
- case 160:
598
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
437
+ case 5:
438
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 4;
439
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
599
440
  break;
600
- case 161:
601
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
602
- this.offlineOff();
441
+ case 6:
442
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 5;
443
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
603
444
  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();
445
+ case 7:
446
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 6;
447
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
610
448
  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
- );
449
+ case 8:
450
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 7;
451
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
616
452
  break;
617
- case 100:
618
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
453
+ case 9:
454
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 8;
455
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
619
456
  break;
620
- case 200:
621
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
457
+ case 10:
458
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 9;
459
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
622
460
  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}`);
461
+ case 11:
462
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 10;
463
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
626
464
  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}`);
465
+ case 12:
466
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 11;
467
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
630
468
  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}`);
469
+ case 13:
470
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 12;
471
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
634
472
  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}`);
473
+ case 14:
474
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 13;
475
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
638
476
  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}`);
477
+ case 15:
478
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 14;
479
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
642
480
  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}`);
481
+ case 16:
482
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 15;
483
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
646
484
  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}`);
485
+ case 17:
486
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 16;
487
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
650
488
  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}`);
489
+ case 18:
490
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 17;
491
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
654
492
  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}`);
493
+ case 19:
494
+ this.LightSensor!.CurrentAmbientLightLevel = ((set_maxLux - set_minLux) / spaceBetweenLevels) * 18;
495
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
658
496
  break;
497
+ case 20:
659
498
  default:
660
- this.infoLog(
661
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
662
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
663
- );
664
- }
665
- }
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
- }
499
+ this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${lightLevel}`);
830
501
  }
831
502
  }
832
-
833
- enablingDeviceLogging(): boolean {
834
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
835
- }
836
503
  }