@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
@@ -1,119 +1,76 @@
1
- import { request } from 'undici';
2
- import { sleep } from '../utils.js';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * humidifier.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
3
6
  import { interval, Subject } from 'rxjs';
4
- import { SwitchBotPlatform } from '../platform.js';
7
+ import { convertUnits } from '../utils.js';
5
8
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
6
- import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
7
- import { device, devicesConfig, serviceData, deviceStatus, Devices, SwitchBotPlatformConfig } from '../settings.js';
9
+
10
+ import type { SwitchBotPlatform } from '../platform.js';
11
+ import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
12
+ import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
8
13
 
9
14
  /**
10
15
  * Platform Accessory
11
16
  * An instance of this class is created for each accessory your platform registers
12
17
  * Each accessory may expose multiple services of different service types.
13
18
  */
14
- export class Humidifier {
15
- public readonly api: API;
16
- public readonly log: Logging;
17
- public readonly config!: SwitchBotPlatformConfig;
18
- protected readonly hap: HAP;
19
+ export class Humidifier extends deviceBase {
19
20
  // Services
20
- humidifierService: Service;
21
- temperatureservice?: Service;
22
-
23
- // Characteristic Values
24
- Active!: CharacteristicValue;
25
- WaterLevel!: CharacteristicValue;
26
- FirmwareRevision!: CharacteristicValue;
27
- CurrentTemperature!: CharacteristicValue;
28
- CurrentRelativeHumidity!: CharacteristicValue;
29
- TargetHumidifierDehumidifierState!: CharacteristicValue;
30
- CurrentHumidifierDehumidifierState!: CharacteristicValue;
31
- RelativeHumidityHumidifierThreshold!: CharacteristicValue;
32
-
33
- // OpenAPI
34
- OpenAPI_Active: deviceStatus['power'];
35
- OpenAPI_WaterLevel: deviceStatus['lackWater'];
36
- OpenAPI_FirmwareRevision: deviceStatus['version'];
37
- OpenAPI_CurrentTemperature: deviceStatus['temperature'];
38
- OpenAPI_CurrentRelativeHumidity: deviceStatus['humidity'];
39
- OpenAPI_CurrentHumidifierDehumidifierState: deviceStatus['auto'];
40
- OpenAPI_RelativeHumidityHumidifierThreshold: deviceStatus['nebulizationEfficiency'];
41
-
42
- // BLE Others
43
- connected?: boolean;
44
- onState!: serviceData['onState'];
45
- autoMode!: serviceData['autoMode'];
46
- percentage!: serviceData['percentage'];
47
-
48
- // Config
49
- set_minStep?: number;
50
- scanDuration!: number;
51
- deviceLogging!: string;
52
- deviceRefreshRate!: number;
21
+ private HumidifierDehumidifier: {
22
+ Name: CharacteristicValue;
23
+ Service: Service;
24
+ Active: CharacteristicValue;
25
+ WaterLevel: CharacteristicValue;
26
+ CurrentRelativeHumidity: CharacteristicValue;
27
+ TargetHumidifierDehumidifierState: CharacteristicValue;
28
+ CurrentHumidifierDehumidifierState: CharacteristicValue;
29
+ RelativeHumidityHumidifierThreshold: CharacteristicValue;
30
+ };
31
+
32
+ private TemperatureSensor?: {
33
+ Name: CharacteristicValue;
34
+ Service: Service;
35
+ CurrentTemperature: CharacteristicValue;
36
+ };
53
37
 
54
38
  // Updates
55
39
  humidifierUpdateInProgress!: boolean;
56
40
  doHumidifierUpdate!: Subject<void>;
57
41
 
58
- // Connection
59
- private readonly OpenAPI: boolean;
60
- private readonly BLE: boolean;
61
-
62
42
  constructor(
63
- private readonly platform: SwitchBotPlatform,
64
- private accessory: PlatformAccessory,
65
- public device: device & devicesConfig,
43
+ readonly platform: SwitchBotPlatform,
44
+ accessory: PlatformAccessory,
45
+ device: device & devicesConfig,
66
46
  ) {
67
- this.api = this.platform.api;
68
- this.log = this.platform.log;
69
- this.config = this.platform.config;
70
- this.hap = this.api.hap;
71
- // Connection
72
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
73
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
74
- // default placeholders
75
- this.deviceLogs(device);
76
- this.scan(device);
77
- this.refreshRate(device);
78
- this.deviceContext();
79
- this.deviceConfig(device);
80
-
47
+ super(platform, accessory, device);
81
48
  // this is subject we use to track when we need to POST changes to the SwitchBot API
82
49
  this.doHumidifierUpdate = new Subject();
83
50
  this.humidifierUpdateInProgress = false;
84
51
 
85
- // Retrieve initial values and updateHomekit
86
- this.refreshStatus();
87
-
88
- // set accessory information
89
- accessory
90
- .getService(this.hap.Service.AccessoryInformation)!
91
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
92
- .setCharacteristic(this.hap.Characteristic.Model, 'W0801800')
93
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
94
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
95
-
96
- // get the service if it exists, otherwise create a new service
97
- // you can create multiple services for each accessory
98
- const humidifierService = `${accessory.displayName} Humidifier`;
99
- (this.humidifierService = accessory.getService(this.hap.Service.HumidifierDehumidifier)
100
- || accessory.addService(this.hap.Service.HumidifierDehumidifier)), humidifierService;
101
-
102
- this.humidifierService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
103
- if (!this.humidifierService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
104
- this.humidifierService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
105
- }
106
-
107
- // each service must implement at-minimum the "required characteristics" for the given service type
108
- // see https://developers.homebridge.io/#/service/HumidifierDehumidifier
109
-
110
- // create handlers for required characteristics
111
- this.humidifierService.setCharacteristic(
112
- this.hap.Characteristic.CurrentHumidifierDehumidifierState,
113
- this.CurrentHumidifierDehumidifierState,
114
- );
115
-
116
- this.humidifierService
52
+ // Initialize the HumidifierDehumidifier Service
53
+ accessory.context.HumidifierDehumidifier = accessory.context.HumidifierDehumidifier ?? {};
54
+ this.HumidifierDehumidifier = {
55
+ Name: accessory.context.HumidifierDehumidifier.Name ?? accessory.displayName,
56
+ Service: accessory.getService(this.hap.Service.HumidifierDehumidifier)
57
+ ?? accessory.addService(this.hap.Service.HumidifierDehumidifier) as Service,
58
+ Active: accessory.context.Active ?? this.hap.Characteristic.Active.ACTIVE,
59
+ WaterLevel: accessory.context.WaterLevel ?? 100,
60
+ CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity ?? 50,
61
+ TargetHumidifierDehumidifierState: accessory.context.TargetHumidifierDehumidifierState
62
+ ?? this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER,
63
+ CurrentHumidifierDehumidifierState: accessory.context.CurrentHumidifierDehumidifierState
64
+ ?? this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE,
65
+ RelativeHumidityHumidifierThreshold: accessory.context.RelativeHumidityHumidifierThreshold ?? 50,
66
+ };
67
+ accessory.context.HumidifierDehumidifier = this.HumidifierDehumidifier as object;
68
+
69
+ // Initialize the HumidifierDehumidifier Characteristics
70
+ this.HumidifierDehumidifier.Service
71
+ .setCharacteristic(this.hap.Characteristic.Name, this.HumidifierDehumidifier.Name)
72
+ .setCharacteristic(this.hap.Characteristic.CurrentHumidifierDehumidifierState,
73
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState)
117
74
  .getCharacteristic(this.hap.Characteristic.TargetHumidifierDehumidifierState)
118
75
  .setProps({
119
76
  validValueRanges: [0, 1],
@@ -121,36 +78,50 @@ export class Humidifier {
121
78
  maxValue: 1,
122
79
  validValues: [0, 1],
123
80
  })
81
+ .onGet(() => {
82
+ return this.HumidifierDehumidifier.TargetHumidifierDehumidifierState;
83
+ })
124
84
  .onSet(this.TargetHumidifierDehumidifierStateSet.bind(this));
125
85
 
126
- this.humidifierService.getCharacteristic(this.hap.Characteristic.Active).onSet(this.ActiveSet.bind(this));
86
+ this.HumidifierDehumidifier.Service
87
+ .getCharacteristic(this.hap.Characteristic.Active)
88
+ .onGet(() => {
89
+ return this.HumidifierDehumidifier.Active;
90
+ })
91
+ .onSet(this.ActiveSet.bind(this));
127
92
 
128
- this.humidifierService
93
+ this.HumidifierDehumidifier.Service
129
94
  .getCharacteristic(this.hap.Characteristic.RelativeHumidityHumidifierThreshold)
130
95
  .setProps({
131
96
  validValueRanges: [0, 100],
132
97
  minValue: 0,
133
98
  maxValue: 100,
134
- minStep: this.minStep(),
99
+ minStep: device.humidifier?.set_minStep ?? 1,
100
+ })
101
+ .onGet(() => {
102
+ return this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold;
135
103
  })
136
104
  .onSet(this.RelativeHumidityHumidifierThresholdSet.bind(this));
137
105
 
138
- // Temperature Sensor Service
139
- if (device.humidifier?.hide_temperature || this.BLE) {
140
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
141
- this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor);
142
- accessory.removeService(this.temperatureservice!);
143
- } else if (!this.temperatureservice && !this.BLE) {
144
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
145
- const temperatureservice = `${accessory.displayName} Temperature Sensor`;
146
- (this.temperatureservice = this.accessory.getService(this.hap.Service.TemperatureSensor)
147
- || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureservice;
148
-
149
- this.temperatureservice.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
150
- if (!this.temperatureservice.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
151
- this.temperatureservice.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
106
+ // Initialize the Temperature Sensor Service
107
+ if (device.humidifier?.hide_temperature) {
108
+ if (this.TemperatureSensor) {
109
+ this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
110
+ this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
111
+ accessory.removeService(this.TemperatureSensor!.Service);
152
112
  }
153
- this.temperatureservice
113
+ } else {
114
+ accessory.context.TemperatureSensor = accessory.context.TemperatureSensor ?? {};
115
+ this.TemperatureSensor = {
116
+ Name: accessory.context.TemperatureSensor.Name ?? `${accessory.displayName} Temperature Sensor`,
117
+ Service: accessory.getService(this.hap.Service.TemperatureSensor) ?? this.accessory.addService(this.hap.Service.TemperatureSensor) as Service,
118
+ CurrentTemperature: accessory.context.CurrentTemperature || 30,
119
+ };
120
+ accessory.context.TemperatureSensor = this.TemperatureSensor as object;
121
+
122
+ // Initialize the Temperature Sensor Characteristics
123
+ this.TemperatureSensor.Service
124
+ .setCharacteristic(this.hap.Characteristic.Name, this.TemperatureSensor.Name)
154
125
  .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
155
126
  .setProps({
156
127
  validValueRanges: [-273.15, 100],
@@ -159,45 +130,25 @@ export class Humidifier {
159
130
  minStep: 0.1,
160
131
  })
161
132
  .onGet(() => {
162
- return this.CurrentTemperature;
133
+ return this.TemperatureSensor!.CurrentTemperature;
163
134
  });
164
- } else {
165
- this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
166
135
  }
167
136
 
137
+ // Retrieve initial values and updateHomekit
138
+ this.refreshStatus();
139
+
168
140
  // Retrieve initial values and updateHomekit
169
141
  this.updateHomeKitCharacteristics();
170
142
 
171
143
  // Start an update interval
172
144
  interval(this.deviceRefreshRate * 1000)
173
145
  .pipe(skipWhile(() => this.humidifierUpdateInProgress))
174
- .subscribe(() => {
175
- this.refreshStatus();
146
+ .subscribe(async () => {
147
+ await this.refreshStatus();
176
148
  });
177
149
 
178
150
  //regisiter webhook event handler
179
- if (this.device.webhook) {
180
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
181
- this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
182
- try {
183
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
184
- if (context.scale === 'CELSIUS') {
185
- const { temperature, humidity } = context;
186
- const { CurrentTemperature, CurrentRelativeHumidity } = this;
187
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
188
- '(temperature, humidity) = ' +
189
- `Webhook:(${temperature}, ${humidity}), ` +
190
- `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
191
- this.CurrentRelativeHumidity = humidity;
192
- this.CurrentTemperature = temperature;
193
- this.updateHomeKitCharacteristics();
194
- }
195
- } catch (e: any) {
196
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
197
- + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
198
- }
199
- };
200
- }
151
+ this.registerWebhook(accessory, device);
201
152
 
202
153
  // Watch for Humidifier change events
203
154
  // We put in a debounce of 100ms so we don't make duplicate calls
@@ -206,121 +157,112 @@ export class Humidifier {
206
157
  tap(() => {
207
158
  this.humidifierUpdateInProgress = true;
208
159
  }),
209
- debounceTime(this.platform.config.options!.pushRate! * 1000),
160
+ debounceTime(this.devicePushRate * 1000),
210
161
  )
211
162
  .subscribe(async () => {
212
163
  try {
213
164
  await this.pushChanges();
214
165
  } catch (e: any) {
215
166
  this.apiError(e);
216
- this.errorLog(
217
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,` +
218
- ` Error Message: ${JSON.stringify(e.message)}`,
219
- );
167
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
168
+ + ` Error Message: ${JSON.stringify(e.message)}`);
220
169
  }
221
170
  this.humidifierUpdateInProgress = false;
222
171
  });
223
172
  }
224
173
 
225
- /**
226
- * Parse the device status from the SwitchBot api
227
- */
228
- async parseStatus(): Promise<void> {
229
- if (!this.device.enableCloudService && this.OpenAPI) {
230
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
231
- } else if (this.BLE) {
232
- await this.BLEparseStatus();
233
- } else if (this.OpenAPI && this.platform.config.credentials?.token) {
234
- await this.openAPIparseStatus();
235
- } else {
236
- await this.offlineOff();
237
- this.debugWarnLog(
238
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
239
- );
240
- }
241
- }
242
-
243
- async BLEparseStatus(): Promise<void> {
174
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
244
175
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
176
+ // Target Humidifier Dehumidifier State
177
+ if (serviceData.autoMode) {
178
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
179
+ }
245
180
  // Current Relative Humidity
246
- this.CurrentRelativeHumidity = this.percentage!;
247
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
181
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = serviceData.percentage!;
182
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
183
+ + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
248
184
  // Active
249
- if (this.onState) {
250
- this.Active = this.hap.Characteristic.Active.ACTIVE;
185
+ if (serviceData.onState) {
186
+ this.HumidifierDehumidifier.Active = this.hap.Characteristic.Active.ACTIVE;
251
187
  } else {
252
- this.Active = this.hap.Characteristic.Active.INACTIVE;
188
+ this.HumidifierDehumidifier.Active = this.hap.Characteristic.Active.INACTIVE;
253
189
  }
254
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
190
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.HumidifierDehumidifier.Active}`);
255
191
  }
256
192
 
257
- async openAPIparseStatus(): Promise<void> {
193
+ async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
258
194
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
259
195
  // Current Relative Humidity
260
- this.CurrentRelativeHumidity = this.OpenAPI_CurrentTemperature!;
261
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
196
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = deviceStatus.body.temperature!;
197
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
198
+ + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
262
199
  // Current Temperature
263
200
  if (!this.device.humidifier?.hide_temperature) {
264
- this.CurrentTemperature = this.OpenAPI_CurrentTemperature!;
265
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
201
+ this.TemperatureSensor!.CurrentTemperature = deviceStatus.body.temperature!;
202
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
266
203
  }
267
204
  // Target Humidifier Dehumidifier State
268
- switch (this.OpenAPI_CurrentHumidifierDehumidifierState) {
205
+ switch (deviceStatus.body.auto) {
269
206
  case true:
270
- this.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
271
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
272
- this.RelativeHumidityHumidifierThreshold = this.CurrentRelativeHumidity;
207
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState =
208
+ this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
209
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
210
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = this.HumidifierDehumidifier.CurrentRelativeHumidity;
273
211
  break;
274
212
  default:
275
- this.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
276
- if (this.OpenAPI_RelativeHumidityHumidifierThreshold! > 100) {
277
- this.RelativeHumidityHumidifierThreshold = 100;
213
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
214
+ if (deviceStatus.body.nebulizationEfficiency! > 100) {
215
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = 100;
278
216
  } else {
279
- this.RelativeHumidityHumidifierThreshold = this.OpenAPI_RelativeHumidityHumidifierThreshold!;
217
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = deviceStatus.body.nebulizationEfficiency!;
280
218
  }
281
- if (this.CurrentRelativeHumidity > this.RelativeHumidityHumidifierThreshold) {
282
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
283
- } else if (this.Active === this.hap.Characteristic.Active.INACTIVE) {
284
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
219
+ if (this.HumidifierDehumidifier.CurrentRelativeHumidity > this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold) {
220
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
221
+ } else if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.INACTIVE) {
222
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
285
223
  } else {
286
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
224
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
287
225
  }
288
226
  }
289
- this.debugLog(
290
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`,
291
- );
292
- this.debugLog(
293
- `${this.device.deviceType}: ${this.accessory.displayName}` +
294
- ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
295
- );
296
- this.debugLog(
297
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`,
298
- );
227
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
228
+ + ` TargetHumidifierDehumidifierState: ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState}`);
229
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
230
+ + ` RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}`);
231
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
232
+ + ` CurrentHumidifierDehumidifierState: ${this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState}`);
299
233
  // Active
300
- switch (this.OpenAPI_Active) {
234
+ switch (deviceStatus.body.power) {
301
235
  case 'on':
302
- this.Active = this.hap.Characteristic.Active.ACTIVE;
236
+ this.HumidifierDehumidifier.Active = this.hap.Characteristic.Active.ACTIVE;
303
237
  break;
304
238
  default:
305
- this.Active = this.hap.Characteristic.Active.INACTIVE;
239
+ this.HumidifierDehumidifier.Active = this.hap.Characteristic.Active.INACTIVE;
306
240
  }
307
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
241
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.HumidifierDehumidifier.Active}`);
308
242
  // Water Level
309
- if (this.OpenAPI_WaterLevel) {
310
- this.WaterLevel = 0;
243
+ if (deviceStatus.body.lackWater) {
244
+ this.HumidifierDehumidifier.WaterLevel = 0;
311
245
  } else {
312
- this.WaterLevel = 100;
246
+ this.HumidifierDehumidifier.WaterLevel = 100;
313
247
  }
314
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
248
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.HumidifierDehumidifier.WaterLevel}`);
315
249
 
316
- // FirmwareRevision
317
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
318
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
250
+ // Firmware Version
251
+ const version = deviceStatus.body.version?.toString();
252
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
253
+ if (deviceStatus.body.version) {
254
+ const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
255
+ this.accessory
256
+ .getService(this.hap.Service.AccessoryInformation)!
257
+ .setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
258
+ .setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
259
+ .getCharacteristic(this.hap.Characteristic.FirmwareRevision)
260
+ .updateValue(deviceVersion);
261
+ this.accessory.context.deviceVersion = deviceVersion;
262
+ this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
263
+ }
319
264
  }
320
265
 
321
- /**
322
- * Asks the SwitchBot API for the latest device information
323
- */
324
266
  async refreshStatus(): Promise<void> {
325
267
  if (!this.device.enableCloudService && this.OpenAPI) {
326
268
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
@@ -330,134 +272,75 @@ export class Humidifier {
330
272
  await this.openAPIRefreshStatus();
331
273
  } else {
332
274
  await this.offlineOff();
333
- this.debugWarnLog(
334
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` +
335
- ` ${this.device.connectionType}, refreshStatus will not happen.`,
336
- );
275
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
276
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
337
277
  }
338
278
  }
339
279
 
340
280
  async BLERefreshStatus(): Promise<void> {
341
281
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
342
- const switchbot = await this.platform.connectBLE();
343
- // Convert to BLE Address
344
- this.device.bleMac = this.device
345
- .deviceId!.match(/.{1,2}/g)!
346
- .join(':')
347
- .toLowerCase();
348
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
349
- this.getCustomBLEAddress(switchbot);
282
+ const switchbot = await this.switchbotBLE();
283
+
284
+ if (switchbot === undefined) {
285
+ await this.BLERefreshConnection(switchbot);
286
+ } else {
350
287
  // Start to monitor advertisement packets
351
- (async () => {
288
+ (async () => {
352
289
  // Start to monitor advertisement packets
353
- await switchbot.startScan({
354
- model: 'e',
355
- id: this.device.bleMac,
356
- });
357
- // Set an event handler
358
- switchbot.onadvertisement = (ad: any) => {
359
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
360
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
361
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'e') {
362
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
363
- this.autoMode = ad.serviceData.autoMode;
364
- this.onState = ad.serviceData.onState;
365
- this.percentage = ad.serviceData.percentage > 100 ? 100 : ad.serviceData.percentage;
290
+ const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
291
+ // Update HomeKit
292
+ if (serviceData.model !== '' && serviceData.modelName !== '') {
293
+ await this.BLEparseStatus(serviceData);
294
+ await this.updateHomeKitCharacteristics();
366
295
  } else {
367
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
368
- }
369
- };
370
- // Wait 1 seconds
371
- await switchbot.wait(this.scanDuration * 1000);
372
- // Stop to monitor
373
- await switchbot.stopScan();
374
- // Update HomeKit
375
- await this.BLEparseStatus();
376
- await this.updateHomeKitCharacteristics();
377
- })();
378
- /*if (switchbot !== false) {
379
- switchbot
380
- .startScan({
381
- model: 'e',
382
- id: this.device.bleMac,
383
- })
384
- .then(async () => {
385
- // Set an event handler
386
- switchbot.onadvertisement = async (ad: ad) => {
387
- this.debugLog(
388
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
389
- ` BLE Address Found: ${ad.address}`,
390
- );
391
- this.autoMode = ad.serviceData.autoMode;
392
- this.onState = ad.serviceData.onState;
393
- this.percentage = ad.serviceData.percentage;
394
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
395
- this.debugLog(
396
- `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
397
- `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`,
398
- );
399
-
400
- if (ad.serviceData) {
401
- this.connected = true;
402
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
403
- await this.stopScanning(switchbot);
404
- } else {
405
- this.connected = false;
406
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
407
- }
408
- };
409
- // Wait
410
- return await sleep(this.scanDuration * 1000);
411
- })
412
- .then(async () => {
413
- // Stop to monitor
414
- await this.stopScanning(switchbot);
415
- })
416
- .catch(async (e: any) => {
417
- this.apiError(e);
418
- this.errorLog(
419
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
420
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
421
- );
296
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
422
297
  await this.BLERefreshConnection(switchbot);
423
- });
424
- } else {
425
- await this.BLERefreshConnection(switchbot);
426
- }*/
298
+ }
299
+ })();
300
+ }
427
301
  }
428
302
 
429
303
  async openAPIRefreshStatus(): Promise<void> {
430
304
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
431
305
  try {
432
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/status`, {
433
- headers: this.platform.generateHeaders(),
434
- });
435
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
306
+ const { body, statusCode } = await this.deviceRefreshStatus();
436
307
  const deviceStatus: any = await body.json();
437
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
438
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
308
+ await this.refreshStatusCodes(statusCode, deviceStatus);;
439
309
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
440
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
441
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
442
- this.OpenAPI_CurrentHumidifierDehumidifierState = deviceStatus.body.auto;
443
- this.OpenAPI_Active = deviceStatus.body.power;
444
- this.OpenAPI_WaterLevel = deviceStatus.body.lackWater;
445
- this.OpenAPI_CurrentRelativeHumidity = deviceStatus.body.humidity;
446
- this.OpenAPI_CurrentTemperature = deviceStatus.body.temperature;
447
- this.OpenAPI_RelativeHumidityHumidifierThreshold = deviceStatus.body.nebulizationEfficiency;
448
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
449
- this.openAPIparseStatus();
450
- this.updateHomeKitCharacteristics();
310
+ await this.successfulRefreshStatus(statusCode, deviceStatus);
311
+ await this.openAPIparseStatus(deviceStatus);
312
+ await this.updateHomeKitCharacteristics();
451
313
  } else {
452
- this.statusCode(statusCode);
453
- this.statusCode(deviceStatus.statusCode);
314
+ await this.statusCodes(statusCode, deviceStatus);
454
315
  }
455
316
  } catch (e: any) {
456
317
  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
- );
318
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
319
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
320
+ }
321
+ }
322
+
323
+ async registerWebhook(accessory: PlatformAccessory, device: device & devicesConfig) {
324
+ if (device.webhook) {
325
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
326
+ this.platform.webhookEventHandler[device.deviceId] = async (context) => {
327
+ try {
328
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
329
+ const { temperature, humidity } = context;
330
+ const { CurrentRelativeHumidity } = this.HumidifierDehumidifier;
331
+ const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
332
+ this.debugLog(`${device.deviceType}: ${accessory.displayName} (temperature, humidity) = Webhook:(${convertUnits(temperature,
333
+ context.scale, device.iosensor?.convertUnitTo)}, ${humidity}), current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
334
+ this.HumidifierDehumidifier.CurrentRelativeHumidity = humidity;
335
+ if (!device.humidifier?.hide_temperature) {
336
+ this.TemperatureSensor!.CurrentTemperature = convertUnits(temperature, context.scale, device.iosensor?.convertUnitTo);
337
+ }
338
+ this.updateHomeKitCharacteristics();
339
+ } catch (e: any) {
340
+ this.errorLog(`${device.deviceType}: ${accessory.displayName} `
341
+ + `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
342
+ }
343
+ };
461
344
  }
462
345
  }
463
346
 
@@ -467,17 +350,18 @@ export class Humidifier {
467
350
  async pushChanges(): Promise<void> {
468
351
  if (!this.device.enableCloudService && this.OpenAPI) {
469
352
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
470
- /*} else if (this.BLE) {
471
- await this.BLEpushChanges();*/
353
+ } else if (this.BLE) {
354
+ await this.BLEpushChanges();
472
355
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
473
356
  await this.openAPIpushChanges();
474
357
  } else {
475
358
  await this.offlineOff();
476
- this.debugWarnLog(
477
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, pushChanges will not happen.`,
478
- );
359
+ this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
360
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
479
361
  }
480
- interval(5000)
362
+ // Refresh the status from the API
363
+ interval(15000)
364
+ .pipe(skipWhile(() => this.humidifierUpdateInProgress))
481
365
  .pipe(take(1))
482
366
  .subscribe(async () => {
483
367
  await this.refreshStatus();
@@ -488,30 +372,22 @@ export class Humidifier {
488
372
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
489
373
  const switchbot = await this.platform.connectBLE();
490
374
  // Convert to BLE Address
491
- this.device.bleMac = this.device
492
- .deviceId!.match(/.{1,2}/g)!
493
- .join(':')
494
- .toLowerCase();
495
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
375
+ await this.convertBLEAddress();
496
376
  switchbot
497
- .discover({
498
- model: 'e',
499
- quick: true,
500
- id: this.device.bleMac,
501
- })
377
+ .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
502
378
  .then(async (device_list: any) => {
503
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
504
- return await device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
379
+ this.infoLog(`${this.accessory.displayName} Active: ${this.HumidifierDehumidifier.Active}`);
380
+ return await device_list[0].percentage(this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold);
505
381
  })
506
382
  .then(() => {
507
383
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
384
+ this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
385
+ + `Active: ${this.HumidifierDehumidifier.Active} sent over BLE, sent successfully`);
508
386
  })
509
387
  .catch(async (e: any) => {
510
388
  this.apiError(e);
511
- this.errorLog(
512
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}` +
513
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
514
- );
389
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
390
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
515
391
  await this.BLEPushConnection();
516
392
  });
517
393
  }
@@ -519,44 +395,37 @@ export class Humidifier {
519
395
  async openAPIpushChanges(): Promise<void> {
520
396
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
521
397
  if (
522
- this.TargetHumidifierDehumidifierState === this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
523
- this.Active === this.hap.Characteristic.Active.ACTIVE
398
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState === this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER &&
399
+ this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE
524
400
  ) {
525
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Manual: ${this.RelativeHumidityHumidifierThreshold}!`);
401
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
402
+ + ` Pushing Manual: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}!`);
526
403
  const bodyChange = JSON.stringify({
527
404
  command: 'setMode',
528
- parameter: `${this.RelativeHumidityHumidifierThreshold}`,
405
+ parameter: `${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}`,
529
406
  commandType: 'command',
530
407
  });
531
408
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
532
409
  try {
533
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
534
- body: bodyChange,
535
- method: 'POST',
536
- headers: this.platform.generateHeaders(),
537
- });
538
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
410
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
539
411
  const deviceStatus: any = await body.json();
540
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
541
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
542
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
412
+ await this.pushStatusCodes(statusCode, deviceStatus);
543
413
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
544
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
545
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
414
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
415
+ await this.updateHomeKitCharacteristics();
546
416
  } else {
547
- this.statusCode(statusCode);
548
- this.statusCode(deviceStatus.statusCode);
417
+ await this.statusCode(statusCode);
418
+ await this.statusCode(deviceStatus.statusCode);
549
419
  }
550
420
  } catch (e: any) {
551
421
  this.apiError(e);
552
- this.errorLog(
553
- `${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}` +
554
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
555
- );
422
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
423
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
556
424
  }
557
425
  } else if (
558
- this.TargetHumidifierDehumidifierState === this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
559
- this.Active === this.hap.Characteristic.Active.ACTIVE
426
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState ===
427
+ this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
428
+ this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE
560
429
  ) {
561
430
  await this.pushAutoChanges();
562
431
  } else {
@@ -570,8 +439,9 @@ export class Humidifier {
570
439
  async pushAutoChanges(): Promise<void> {
571
440
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushAutoChanges`);
572
441
  if (
573
- this.TargetHumidifierDehumidifierState === this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
574
- this.Active === this.hap.Characteristic.Active.ACTIVE
442
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState ===
443
+ this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER &&
444
+ this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE
575
445
  ) {
576
446
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Auto`);
577
447
  const bodyChange = JSON.stringify({
@@ -581,35 +451,24 @@ export class Humidifier {
581
451
  });
582
452
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
583
453
  try {
584
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
585
- body: bodyChange,
586
- method: 'POST',
587
- headers: this.platform.generateHeaders(),
588
- });
589
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
454
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
590
455
  const deviceStatus: any = await body.json();
591
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
592
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
593
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
456
+ await this.pushStatusCodes(statusCode, deviceStatus);
594
457
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
595
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
596
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
458
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
459
+ await this.updateHomeKitCharacteristics();
597
460
  } else {
598
- this.statusCode(statusCode);
599
- this.statusCode(deviceStatus.statusCode);
461
+ await this.statusCode(statusCode);
462
+ await this.statusCode(deviceStatus.statusCode);
600
463
  }
601
464
  } catch (e: any) {
602
465
  this.apiError(e);
603
- this.errorLog(
604
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}` +
605
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
606
- );
466
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushAutoChanges with ${this.device.connectionType}`
467
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
607
468
  }
608
469
  } else {
609
- this.debugLog(
610
- `${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges.` +
611
- `TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}, Active: ${this.Active}`,
612
- );
470
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushAutoChanges. TargetHumidifierDehumidifierState:`
471
+ + ` ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState}, Active: ${this.HumidifierDehumidifier.Active}`);
613
472
  }
614
473
  }
615
474
 
@@ -618,7 +477,7 @@ export class Humidifier {
618
477
  */
619
478
  async pushActiveChanges(): Promise<void> {
620
479
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} pushActiveChanges`);
621
- if (this.Active === this.hap.Characteristic.Active.INACTIVE) {
480
+ if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.INACTIVE) {
622
481
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Pushing Off`);
623
482
  const bodyChange = JSON.stringify({
624
483
  command: 'turnOff',
@@ -627,32 +486,23 @@ export class Humidifier {
627
486
  });
628
487
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
629
488
  try {
630
- const { body, statusCode } = await request(`${Devices}/${this.device.deviceId}/commands`, {
631
- body: bodyChange,
632
- method: 'POST',
633
- headers: this.platform.generateHeaders(),
634
- });
635
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
489
+ const { body, statusCode } = await this.pushChangeRequest(bodyChange);
636
490
  const deviceStatus: any = await body.json();
637
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
638
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
639
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
491
+ await this.pushStatusCodes(statusCode, deviceStatus);
640
492
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
641
- this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
642
- + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
493
+ await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
494
+ await this.updateHomeKitCharacteristics();
643
495
  } else {
644
- this.statusCode(statusCode);
645
- this.statusCode(deviceStatus.statusCode);
496
+ await this.statusCode(statusCode);
497
+ await this.statusCode(deviceStatus.statusCode);
646
498
  }
647
499
  } catch (e: any) {
648
500
  this.apiError(e);
649
- this.errorLog(
650
- `${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}` +
651
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
652
- );
501
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushActiveChanges with ${this.device.connectionType}`
502
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
653
503
  }
654
504
  } else {
655
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushActiveChanges. Active: ${this.Active}`);
505
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No pushActiveChanges. Active: ${this.HumidifierDehumidifier.Active}`);
656
506
  }
657
507
  }
658
508
 
@@ -660,13 +510,13 @@ export class Humidifier {
660
510
  * Handle requests to set the "Active" characteristic
661
511
  */
662
512
  async ActiveSet(value: CharacteristicValue): Promise<void> {
663
- if (this.Active === this.accessory.context.Active) {
513
+ if (this.HumidifierDehumidifier.Active === this.accessory.context.Active) {
664
514
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set Active: ${value}`);
665
515
  } else {
666
516
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
667
517
  }
668
518
 
669
- this.Active = value;
519
+ this.HumidifierDehumidifier.Active = value;
670
520
  this.doHumidifierUpdate.next();
671
521
  }
672
522
 
@@ -674,13 +524,13 @@ export class Humidifier {
674
524
  * Handle requests to set the "Target Humidifier Dehumidifier State" characteristic
675
525
  */
676
526
  async TargetHumidifierDehumidifierStateSet(value: CharacteristicValue): Promise<void> {
677
- if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
527
+ if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE) {
678
528
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
679
529
  } else {
680
530
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetHumidifierDehumidifierState: ${value}`);
681
531
  }
682
532
 
683
- this.TargetHumidifierDehumidifierState = value;
533
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState = value;
684
534
  this.doHumidifierUpdate.next();
685
535
  }
686
536
 
@@ -688,16 +538,16 @@ export class Humidifier {
688
538
  * Handle requests to set the "Relative Humidity Humidifier Threshold" characteristic
689
539
  */
690
540
  async RelativeHumidityHumidifierThresholdSet(value: CharacteristicValue): Promise<void> {
691
- if (this.Active === this.hap.Characteristic.Active.ACTIVE) {
541
+ if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE) {
692
542
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
693
543
  } else {
694
544
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Set RelativeHumidityHumidifierThreshold: ${value}`);
695
545
  }
696
546
 
697
- this.RelativeHumidityHumidifierThreshold = value;
698
- if (this.Active === this.hap.Characteristic.Active.INACTIVE) {
699
- this.Active = this.hap.Characteristic.Active.ACTIVE;
700
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
547
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold = value;
548
+ if (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.INACTIVE) {
549
+ this.HumidifierDehumidifier.Active = this.hap.Characteristic.Active.ACTIVE;
550
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.IDLE;
701
551
  }
702
552
  this.doHumidifierUpdate.next();
703
553
  }
@@ -706,117 +556,75 @@ export class Humidifier {
706
556
  * Updates the status for each of the HomeKit Characteristics
707
557
  */
708
558
  async updateHomeKitCharacteristics(): Promise<void> {
709
- if (this.CurrentRelativeHumidity === undefined) {
710
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
559
+ if (this.HumidifierDehumidifier.CurrentRelativeHumidity === undefined) {
560
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
561
+ + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
711
562
  } else {
712
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
713
- this.debugLog(
714
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
715
- );
716
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
563
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
564
+ this.HumidifierDehumidifier.CurrentRelativeHumidity);
565
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
566
+ + ` CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`);
567
+ this.accessory.context.CurrentRelativeHumidity = this.HumidifierDehumidifier.CurrentRelativeHumidity;
717
568
  }
718
569
  if (this.OpenAPI) {
719
- if (this.WaterLevel === undefined) {
720
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.WaterLevel}`);
570
+ if (this.HumidifierDehumidifier.WaterLevel === undefined) {
571
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} WaterLevel: ${this.HumidifierDehumidifier.WaterLevel}`);
721
572
  } else {
722
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.WaterLevel, this.WaterLevel);
723
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic WaterLevel: ${this.WaterLevel}`);
724
- this.accessory.context.WaterLevel = this.WaterLevel;
573
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.WaterLevel, this.HumidifierDehumidifier.WaterLevel);
574
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
575
+ + ` WaterLevel: ${this.HumidifierDehumidifier.WaterLevel}`);
576
+ this.accessory.context.WaterLevel = this.HumidifierDehumidifier.WaterLevel;
725
577
  }
726
578
  }
727
- if (this.CurrentHumidifierDehumidifierState === undefined) {
728
- this.debugLog(
729
- `${this.device.deviceType}: ${this.accessory.displayName}` +
730
- ` CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`,
731
- );
579
+ if (this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState === undefined) {
580
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
581
+ + ` CurrentHumidifierDehumidifierState: ${this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState}`);
732
582
  } else {
733
- this.humidifierService.updateCharacteristic(
734
- this.hap.Characteristic.CurrentHumidifierDehumidifierState,
735
- this.CurrentHumidifierDehumidifierState,
736
- );
737
- this.debugLog(
738
- `${this.device.deviceType}: ${this.accessory.displayName}` +
739
- ` updateCharacteristic CurrentHumidifierDehumidifierState: ${this.CurrentHumidifierDehumidifierState}`,
740
- );
741
- this.accessory.context.CurrentHumidifierDehumidifierState = this.CurrentHumidifierDehumidifierState;
742
- }
743
- if (this.TargetHumidifierDehumidifierState === undefined) {
744
- this.debugLog(
745
- `${this.device.deviceType}: ${this.accessory.displayName}` + ` TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`,
746
- );
583
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.CurrentHumidifierDehumidifierState,
584
+ this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState);
585
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
586
+ + ` CurrentHumidifierDehumidifierState: ${this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState}`);
587
+ this.accessory.context.CurrentHumidifierDehumidifierState = this.HumidifierDehumidifier.CurrentHumidifierDehumidifierState;
588
+ }
589
+ if (this.HumidifierDehumidifier.TargetHumidifierDehumidifierState === undefined) {
590
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
591
+ + ` TargetHumidifierDehumidifierState: ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState}`);
747
592
  } else {
748
- this.humidifierService.updateCharacteristic(
749
- this.hap.Characteristic.TargetHumidifierDehumidifierState,
750
- this.TargetHumidifierDehumidifierState,
751
- );
752
- this.debugLog(
753
- `${this.device.deviceType}: ${this.accessory.displayName}` +
754
- ` updateCharacteristic TargetHumidifierDehumidifierState: ${this.TargetHumidifierDehumidifierState}`,
755
- );
756
- this.accessory.context.TargetHumidifierDehumidifierState = this.TargetHumidifierDehumidifierState;
757
- }
758
- if (this.Active === undefined) {
759
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.Active}`);
593
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.TargetHumidifierDehumidifierState,
594
+ this.HumidifierDehumidifier.TargetHumidifierDehumidifierState);
595
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
596
+ + ` TargetHumidifierDehumidifierState: ${this.HumidifierDehumidifier.TargetHumidifierDehumidifierState}`);
597
+ this.accessory.context.TargetHumidifierDehumidifierState = this.HumidifierDehumidifier.TargetHumidifierDehumidifierState;
598
+ }
599
+ if (this.HumidifierDehumidifier.Active === undefined) {
600
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${this.HumidifierDehumidifier.Active}`);
760
601
  } else {
761
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.Active, this.Active);
762
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
763
- this.accessory.context.Active = this.Active;
602
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.Active, this.HumidifierDehumidifier.Active);
603
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.HumidifierDehumidifier.Active}`);
604
+ this.accessory.context.Active = this.HumidifierDehumidifier.Active;
764
605
  }
765
- if (this.RelativeHumidityHumidifierThreshold === undefined) {
766
- this.debugLog(
767
- `${this.device.deviceType}: ${this.accessory.displayName}` +
768
- ` RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
769
- );
606
+ if (this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold === undefined) {
607
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
608
+ + ` RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}`);
770
609
  } else {
771
- this.humidifierService.updateCharacteristic(
772
- this.hap.Characteristic.RelativeHumidityHumidifierThreshold,
773
- this.RelativeHumidityHumidifierThreshold,
774
- );
775
- this.debugLog(
776
- `${this.device.deviceType}: ${this.accessory.displayName}` +
777
- ` updateCharacteristic RelativeHumidityHumidifierThreshold: ${this.RelativeHumidityHumidifierThreshold}`,
778
- );
779
- this.accessory.context.RelativeHumidityHumidifierThreshold = this.RelativeHumidityHumidifierThreshold;
610
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.RelativeHumidityHumidifierThreshold,
611
+ this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold);
612
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
613
+ + ` RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}`);
614
+ this.accessory.context.RelativeHumidityHumidifierThreshold = this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold;
780
615
  }
781
616
  if (!this.device.humidifier?.hide_temperature && !this.BLE) {
782
- if (this.CurrentTemperature === undefined) {
783
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
617
+ if (this.TemperatureSensor!.CurrentTemperature === undefined) {
618
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
784
619
  } else {
785
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
786
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
787
- this.accessory.context.CurrentTemperature = this.CurrentTemperature;
620
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
621
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
622
+ + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
623
+ this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
788
624
  }
789
625
  }
790
626
  }
791
627
 
792
- async stopScanning(switchbot: any) {
793
- switchbot.stopScan();
794
- if (this.connected) {
795
- await this.BLEparseStatus();
796
- await this.updateHomeKitCharacteristics();
797
- } else {
798
- await this.BLERefreshConnection(switchbot);
799
- }
800
- }
801
-
802
- async getCustomBLEAddress(switchbot: any) {
803
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
804
- (async () => {
805
- // Start to monitor advertisement packets
806
- await switchbot.startScan({
807
- model: 'e',
808
- });
809
- // Set an event handler
810
- switchbot.onadvertisement = (ad: any) => {
811
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
812
- };
813
- await sleep(10000);
814
- // Stop to monitor
815
- switchbot.stopScan();
816
- })();
817
- }
818
- }
819
-
820
628
  async BLEPushConnection() {
821
629
  if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
822
630
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection to Push Changes`);
@@ -833,283 +641,28 @@ export class Humidifier {
833
641
  }
834
642
  }
835
643
 
836
- minStep(): number {
837
- if (this.device.humidifier?.set_minStep) {
838
- this.set_minStep = this.device.humidifier?.set_minStep;
839
- } else {
840
- this.set_minStep = 1;
841
- }
842
- return this.set_minStep;
843
- }
844
-
845
- async scan(device: device & devicesConfig): Promise<void> {
846
- if (device.scanDuration) {
847
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
848
- if (this.BLE) {
849
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
850
- }
851
- } else {
852
- this.scanDuration = this.accessory.context.scanDuration = 1;
853
- if (this.BLE) {
854
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
855
- }
856
- }
857
- }
858
-
859
- async statusCode(statusCode: number): Promise<void> {
860
- switch (statusCode) {
861
- case 151:
862
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
863
- break;
864
- case 152:
865
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
866
- break;
867
- case 160:
868
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
869
- break;
870
- case 161:
871
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
872
- this.offlineOff();
873
- break;
874
- case 171:
875
- this.errorLog(
876
- `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
877
- `Hub: ${this.device.hubDeviceId}`,
878
- );
879
- this.offlineOff();
880
- break;
881
- case 190:
882
- this.errorLog(
883
- `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
884
- ` Or command format is invalid, statusCode: ${statusCode}`,
885
- );
886
- break;
887
- case 100:
888
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
889
- break;
890
- case 200:
891
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
892
- break;
893
- case 400:
894
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
895
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
896
- break;
897
- case 401:
898
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
899
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
900
- break;
901
- case 403:
902
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
903
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
904
- break;
905
- case 404:
906
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
907
- + `statusCode: ${statusCode}`);
908
- break;
909
- case 406:
910
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
911
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
912
- break;
913
- case 415:
914
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
915
- + `header that is not supported by the server, statusCode: ${statusCode}`);
916
- break;
917
- case 422:
918
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
919
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
920
- break;
921
- case 429:
922
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
923
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
924
- break;
925
- case 500:
926
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
927
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
928
- break;
929
- default:
930
- this.infoLog(
931
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
932
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
933
- );
934
- }
935
- }
936
-
937
644
  async offlineOff(): Promise<void> {
938
645
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} offline: ${this.device.offline}`);
939
646
  if (this.device.offline) {
940
- await this.deviceContext();
941
- if (this.CurrentTemperature === undefined) {
942
- this.CurrentTemperature = 0;
943
- }
944
- await this.updateHomeKitCharacteristics();
647
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.CurrentHumidifierDehumidifierState,
648
+ this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE);
649
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.TargetHumidifierDehumidifierState,
650
+ this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER);
651
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.Active, this.hap.Characteristic.Active.INACTIVE);
945
652
  }
946
653
  }
947
654
 
948
655
  async apiError(e: any): Promise<void> {
949
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
656
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
950
657
  if (!this.BLE) {
951
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.WaterLevel, e);
658
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.WaterLevel, e);
952
659
  }
953
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.CurrentHumidifierDehumidifierState, e);
954
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.TargetHumidifierDehumidifierState, e);
955
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.Active, e);
956
- this.humidifierService.updateCharacteristic(this.hap.Characteristic.RelativeHumidityHumidifierThreshold, e);
660
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.CurrentHumidifierDehumidifierState, e);
661
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.TargetHumidifierDehumidifierState, e);
662
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.Active, e);
663
+ this.HumidifierDehumidifier.Service.updateCharacteristic(this.hap.Characteristic.RelativeHumidityHumidifierThreshold, e);
957
664
  if (!this.device.humidifier?.hide_temperature && !this.BLE) {
958
- this.temperatureservice?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
959
- }
960
- }
961
-
962
- async deviceContext() {
963
- if (this.Active === undefined) {
964
- this.Active = this.hap.Characteristic.Active.ACTIVE;
965
- } else {
966
- this.Active = this.accessory.context.Active;
967
- }
968
- if (this.CurrentTemperature === undefined) {
969
- this.CurrentTemperature = 30;
970
- } else {
971
- this.CurrentTemperature = this.accessory.context.CurrentTemperature;
972
- }
973
- if (this.CurrentRelativeHumidity === undefined) {
974
- this.CurrentRelativeHumidity = 0;
975
- } else {
976
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
977
- }
978
- if (this.TargetHumidifierDehumidifierState === undefined) {
979
- this.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
980
- } else if (this.accessory.context.TargetHumidifierDehumidifierState === undefined) {
981
- this.TargetHumidifierDehumidifierState = this.hap.Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
982
- } else {
983
- this.TargetHumidifierDehumidifierState = this.accessory.context.TargetHumidifierDehumidifierState;
665
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
984
666
  }
985
- if (this.CurrentHumidifierDehumidifierState === undefined) {
986
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
987
- } else if (this.accessory.context.CurrentHumidifierDehumidifierState === undefined) {
988
- this.CurrentHumidifierDehumidifierState = this.hap.Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
989
- } else {
990
- this.CurrentHumidifierDehumidifierState = this.accessory.context.CurrentHumidifierDehumidifierState;
991
- }
992
- if (this.RelativeHumidityHumidifierThreshold === undefined) {
993
- this.RelativeHumidityHumidifierThreshold = 0;
994
- } else {
995
- this.RelativeHumidityHumidifierThreshold = this.accessory.context.RelativeHumidityHumidifierThreshold;
996
- }
997
- if (this.WaterLevel === undefined) {
998
- this.WaterLevel = 0;
999
- } else {
1000
- this.WaterLevel = this.accessory.context.WaterLevel;
1001
- }
1002
- if (this.FirmwareRevision === undefined) {
1003
- this.FirmwareRevision = this.platform.version;
1004
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
1005
- }
1006
- }
1007
-
1008
- async refreshRate(device: device & devicesConfig): Promise<void> {
1009
- if (device.refreshRate) {
1010
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1011
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1012
- } else if (this.platform.config.options!.refreshRate) {
1013
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
1014
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1015
- }
1016
- }
1017
-
1018
- async deviceConfig(device: device & devicesConfig): Promise<void> {
1019
- let config = {};
1020
- if (device.humidifier) {
1021
- config = device.humidifier;
1022
- }
1023
- if (device.connectionType !== undefined) {
1024
- config['connectionType'] = device.connectionType;
1025
- }
1026
- if (device.external !== undefined) {
1027
- config['external'] = device.external;
1028
- }
1029
- if (device.logging !== undefined) {
1030
- config['logging'] = device.logging;
1031
- }
1032
- if (device.refreshRate !== undefined) {
1033
- config['refreshRate'] = device.refreshRate;
1034
- }
1035
- if (device.scanDuration !== undefined) {
1036
- config['scanDuration'] = device.scanDuration;
1037
- }
1038
- if (device.offline !== undefined) {
1039
- config['offline'] = device.offline;
1040
- }
1041
- if (device.webhook !== undefined) {
1042
- config['webhook'] = device.webhook;
1043
- }
1044
- if (Object.entries(config).length !== 0) {
1045
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
1046
- }
1047
- }
1048
-
1049
- async deviceLogs(device: device & devicesConfig): Promise<void> {
1050
- if (this.platform.debugMode) {
1051
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
1052
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
1053
- } else if (device.logging) {
1054
- this.deviceLogging = this.accessory.context.logging = device.logging;
1055
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
1056
- } else if (this.platform.config.options?.logging) {
1057
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
1058
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
1059
- } else {
1060
- this.deviceLogging = this.accessory.context.logging = 'standard';
1061
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1062
- }
1063
- }
1064
-
1065
- /**
1066
- * Logging for Device
1067
- */
1068
- async infoLog(...log: any[]): Promise<void> {
1069
- if (this.enablingDeviceLogging()) {
1070
- this.platform.log.info(String(...log));
1071
- }
1072
- }
1073
-
1074
- async warnLog(...log: any[]): Promise<void> {
1075
- if (this.enablingDeviceLogging()) {
1076
- this.platform.log.warn(String(...log));
1077
- }
1078
- }
1079
-
1080
- async debugWarnLog(...log: any[]): Promise<void> {
1081
- if (this.enablingDeviceLogging()) {
1082
- if (this.deviceLogging?.includes('debug')) {
1083
- this.platform.log.warn('[DEBUG]', String(...log));
1084
- }
1085
- }
1086
- }
1087
-
1088
- async errorLog(...log: any[]): Promise<void> {
1089
- if (this.enablingDeviceLogging()) {
1090
- this.platform.log.error(String(...log));
1091
- }
1092
- }
1093
-
1094
- async debugErrorLog(...log: any[]): Promise<void> {
1095
- if (this.enablingDeviceLogging()) {
1096
- if (this.deviceLogging?.includes('debug')) {
1097
- this.platform.log.error('[DEBUG]', String(...log));
1098
- }
1099
- }
1100
- }
1101
-
1102
- async debugLog(...log: any[]): Promise<void> {
1103
- if (this.enablingDeviceLogging()) {
1104
- if (this.deviceLogging === 'debug') {
1105
- this.platform.log.info('[DEBUG]', String(...log));
1106
- } else {
1107
- this.platform.log.debug(String(...log));
1108
- }
1109
- }
1110
- }
1111
-
1112
- enablingDeviceLogging(): boolean {
1113
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
1114
667
  }
1115
668
  }