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