@switchbot/homebridge-switchbot 1.4.1-beta.1 → 1.4.1-beta.5

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 (103) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/config.schema.json +83 -11
  3. package/dist/devices/bots.d.ts +13 -3
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +172 -106
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +13 -3
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +166 -92
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +13 -3
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +131 -56
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +14 -5
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +208 -131
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +13 -3
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +191 -117
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +13 -3
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +125 -53
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +13 -3
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +111 -41
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +13 -3
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +100 -35
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +11 -6
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +91 -85
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +11 -3
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +72 -28
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +11 -3
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +65 -24
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +11 -3
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +77 -33
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +11 -3
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +66 -25
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +11 -3
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +72 -31
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +11 -3
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +83 -41
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +11 -3
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +65 -24
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +11 -3
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +65 -24
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +9 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +264 -285
  74. package/dist/platform.js.map +1 -1
  75. package/dist/settings.d.ts +4 -1
  76. package/dist/settings.d.ts.map +1 -1
  77. package/dist/settings.js.map +1 -1
  78. package/nodemon.json +1 -1
  79. package/package.json +10 -6
  80. package/src/devices/bots.ts +178 -107
  81. package/src/devices/colorbulb.ts +175 -95
  82. package/src/devices/contact.ts +138 -57
  83. package/src/devices/curtains.ts +216 -135
  84. package/src/devices/humidifiers.ts +198 -119
  85. package/src/devices/meters.ts +132 -54
  86. package/src/devices/motion.ts +117 -41
  87. package/src/devices/plugs.ts +105 -36
  88. package/src/irdevices/airconditioners.ts +96 -89
  89. package/src/irdevices/airpurifiers.ts +77 -28
  90. package/src/irdevices/cameras.ts +70 -24
  91. package/src/irdevices/fans.ts +82 -34
  92. package/src/irdevices/lights.ts +71 -25
  93. package/src/irdevices/others.ts +76 -30
  94. package/src/irdevices/tvs.ts +88 -41
  95. package/src/irdevices/vacuumcleaners.ts +70 -24
  96. package/src/irdevices/waterheaters.ts +70 -24
  97. package/src/platform.ts +267 -290
  98. package/src/settings.ts +4 -1
  99. package/dist/devices/indoorcam.d.ts +0 -64
  100. package/dist/devices/indoorcam.d.ts.map +0 -1
  101. package/dist/devices/indoorcam.js +0 -249
  102. package/dist/devices/indoorcam.js.map +0 -1
  103. package/src/devices/indoorcam.ts +0 -309
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { irDevicesConfig, DeviceURL, irdevice, payload } from '../settings';
4
- import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
4
+ import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -21,7 +21,6 @@ export class AirConditioner {
21
21
  CurrentTemperatureCached!: CharacteristicValue;
22
22
  TargetHeaterCoolerState?: CharacteristicValue;
23
23
  CurrentHeaterCoolerState!: CharacteristicValue;
24
- HeatingThresholdTemperature?: CharacteristicValue;
25
24
 
26
25
  // Others
27
26
  state!: string;
@@ -35,8 +34,7 @@ export class AirConditioner {
35
34
  static MODE_HEAT: number;
36
35
 
37
36
  // Config
38
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
39
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
37
+ deviceLogging!: string;
40
38
 
41
39
  constructor(
42
40
  private readonly platform: SwitchBotPlatform,
@@ -44,6 +42,7 @@ export class AirConditioner {
44
42
  public device: irdevice & irDevicesConfig,
45
43
  ) {
46
44
  // default placeholders
45
+ this.logs();
47
46
  if (this.Active === undefined) {
48
47
  this.Active = this.platform.Characteristic.Active.INACTIVE;
49
48
  } else {
@@ -106,30 +105,6 @@ export class AirConditioner {
106
105
  return this.CurrentHeaterCoolerStateGet();
107
106
  });
108
107
 
109
- this.service
110
- .getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature)
111
- .setProps({
112
- minValue: 16,
113
- maxValue: 30,
114
- minStep: 1,
115
- })
116
- .onGet(() => {
117
- return this.HeatingThresholdTemperatureGet();
118
- })
119
- .onSet(this.HeatingThresholdTemperatureSet.bind(this));
120
-
121
- this.service
122
- .getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature)
123
- .setProps({
124
- minValue: 16,
125
- maxValue: 30,
126
- minStep: 1,
127
- })
128
- .onGet(() => {
129
- return this.HeatingThresholdTemperatureGet();
130
- })
131
- .onSet(this.HeatingThresholdTemperatureSet.bind(this));
132
-
133
108
  this.service
134
109
  .getCharacteristic(this.platform.Characteristic.RotationSpeed)
135
110
  .setProps({
@@ -143,6 +118,16 @@ export class AirConditioner {
143
118
  .onSet(this.RotationSpeedSet.bind(this));
144
119
  }
145
120
 
121
+ logs() {
122
+ if (this.device.logging) {
123
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
124
+ } else if (this.platform.config.options?.logging) {
125
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
126
+ } else {
127
+ this.deviceLogging = this.accessory.context.logging = 'standard';
128
+ }
129
+ }
130
+
146
131
  private RotationSpeedSet(value: CharacteristicValue) {
147
132
  if (value === 4) {
148
133
  this.CurrentFanSpeed = 1;
@@ -163,14 +148,14 @@ export class AirConditioner {
163
148
  }
164
149
 
165
150
  private ActiveSet(value: CharacteristicValue) {
166
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Set Active: ${value}`);
151
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Set Active: ${value}`);
167
152
 
168
153
  if (value === this.platform.Characteristic.Active.INACTIVE) {
169
154
  this.pushAirConditionerOffChanges();
170
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
155
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
171
156
  } else {
172
157
  this.pushAirConditionerOnChanges();
173
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
158
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} pushAirConditionerOffChanges, Active: ${this.Active}`);
174
159
  }
175
160
  this.Active = value;
176
161
  this.ActiveCached = this.Active;
@@ -179,49 +164,42 @@ export class AirConditioner {
179
164
 
180
165
  private updateHomeKitCharacteristics() {
181
166
  if (this.Active === undefined) {
182
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Active: ${this.Active}`);
167
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Active: ${this.Active}`);
183
168
  } else {
184
169
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
185
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
170
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
186
171
  }
187
172
  if (this.RotationSpeed === undefined) {
188
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
173
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} RotationSpeed: ${this.RotationSpeed}`);
189
174
  } else {
190
175
  this.service?.updateCharacteristic(this.platform.Characteristic.RotationSpeed, this.RotationSpeed);
191
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
176
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic RotationSpeed: ${this.RotationSpeed}`);
192
177
  }
193
178
  if (this.CurrentTemperature === undefined) {
194
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
179
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
195
180
  } else {
196
181
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.CurrentTemperature);
197
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
182
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
198
183
  }
199
184
  if (this.TargetHeaterCoolerState === undefined) {
200
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
185
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
201
186
  } else {
202
187
  this.service?.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, this.TargetHeaterCoolerState);
203
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
188
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName}`
204
189
  + ` updateCharacteristic TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
205
190
  }
206
191
  if (this.CurrentHeaterCoolerState === undefined) {
207
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
192
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
208
193
  } else {
209
194
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
210
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
195
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName}`
211
196
  + ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
212
197
  }
213
- if (this.HeatingThresholdTemperature === undefined) {
214
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} HeatingThresholdTemperature: ${this.HeatingThresholdTemperature}`);
215
- } else {
216
- this.service?.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, this.HeatingThresholdTemperature);
217
- this.platform.device(`Air Conditioner: ${this.accessory.displayName}`
218
- + ` updateCharacteristic HeatingThresholdTemperature: ${this.HeatingThresholdTemperature}`);
219
- }
220
198
  }
221
199
 
222
200
  private CurrentTemperatureGet(value: CharacteristicValue) {
223
201
  value = Number(this.CurrentTemperature) || 24;
224
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Trigger Get CurrentTemperature: ${value}` );
202
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Trigger Get CurrentTemperature: ${value}`);
225
203
 
226
204
  this.service
227
205
  .getCharacteristic(this.platform.Characteristic.CurrentTemperature)
@@ -232,17 +210,17 @@ export class AirConditioner {
232
210
 
233
211
  private TargetHeaterCoolerStateSet(value: CharacteristicValue) {
234
212
  switch (value) {
235
- case this.platform.Characteristic.TargetHeaterCoolerState.AUTO:
236
- this.CurrentMode = AirConditioner.MODE_AUTO;
237
- break;
238
- case this.platform.Characteristic.TargetHeaterCoolerState.COOL:
239
- this.CurrentMode = AirConditioner.MODE_COOL;
240
- break;
241
213
  case this.platform.Characteristic.TargetHeaterCoolerState.HEAT:
242
214
  this.CurrentMode = AirConditioner.MODE_HEAT;
215
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Set) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
243
216
  break;
244
- default:
217
+ case this.platform.Characteristic.TargetHeaterCoolerState.COOL:
218
+ this.CurrentMode = AirConditioner.MODE_COOL;
219
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Set) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
245
220
  break;
221
+ case this.platform.Characteristic.TargetHeaterCoolerState.AUTO:
222
+ this.CurrentMode = AirConditioner.MODE_AUTO;
223
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Set) TargetHeaterCoolerState: ${this.TargetHeaterCoolerState}`);
246
224
  }
247
225
  this.pushAirConditionerStatusChanges();
248
226
  }
@@ -251,30 +229,18 @@ export class AirConditioner {
251
229
  if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
252
230
  if ((this.CurrentTemperature || 24) < (this.LastTemperature || 30)) {
253
231
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.COOLING;
232
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Get) CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
254
233
  } else {
255
234
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.HEATING;
235
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Get) CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
256
236
  }
257
237
  } else {
258
238
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
239
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} (Get) CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
259
240
  }
260
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
261
241
  return this.CurrentHeaterCoolerState;
262
242
  }
263
243
 
264
- private HeatingThresholdTemperatureGet() {
265
- this.CurrentTemperature = this.CurrentTemperature || 24;
266
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
267
- return this.CurrentTemperature;
268
- }
269
-
270
- private HeatingThresholdTemperatureSet(value: CharacteristicValue) {
271
- this.pushAirConditionerStatusChanges();
272
- this.LastTemperature = Number(this.CurrentTemperature);
273
- this.CurrentTemperature = Number(value);
274
- this.platform.device(`Air Conditioner: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature},`
275
- + ` LastTemperature: ${this.LastTemperature}`);
276
- }
277
-
278
244
  /**
279
245
  * Pushes the requested changes to the SwitchBot API
280
246
  * deviceType commandType Command command parameter Description
@@ -356,60 +322,101 @@ export class AirConditioner {
356
322
 
357
323
  public async pushChanges(payload: payload) {
358
324
  try {
359
- this.platform.log.info(`Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
325
+ this.infoLog(`Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
360
326
  + ` parameter: [${payload.parameter}], commandType: ${payload.commandType}`);
361
327
 
362
328
  // Make the API request
363
329
  const push: any = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
364
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
330
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} pushChanges: ${JSON.stringify(push.data)}`);
365
331
  this.statusCode(push);
366
332
  this.CurrentTemperatureCached = this.CurrentTemperature;
367
333
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
368
334
  this.updateHomeKitCharacteristics();
369
335
  } catch (e: any) {
370
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges`);
371
- if (this.deviceDebug) {
372
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
336
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges`);
337
+ if (this.deviceLogging === 'debug') {
338
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
373
339
  + ` Error Message: ${JSON.stringify(e.message)}`);
374
340
  }
375
- if (this.debugDebug) {
376
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
341
+ if (this.platform.debugMode) {
342
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} failed pushChanges,`
377
343
  + ` Error: ${JSON.stringify(e)}`);
378
344
  }
379
- this.apiError();
345
+ this.apiError(e);
380
346
  }
381
347
  }
382
348
 
383
349
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
384
350
  switch (push.data.statusCode) {
385
351
  case 151:
386
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Command not supported by this device type.`);
352
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Command not supported by this device type.`);
387
353
  break;
388
354
  case 152:
389
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device not found.`);
355
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device not found.`);
390
356
  break;
391
357
  case 160:
392
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Command is not supported.`);
358
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Command is not supported.`);
393
359
  break;
394
360
  case 161:
395
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device is offline.`);
361
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device is offline.`);
396
362
  break;
397
363
  case 171:
398
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
364
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
399
365
  break;
400
366
  case 190:
401
- this.platform.log.error(`Air Conditioner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
367
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
402
368
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
403
369
  break;
404
370
  case 100:
405
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Command successfully sent.`);
371
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Command successfully sent.`);
406
372
  break;
407
373
  default:
408
- this.platform.debug(`Air Conditioner: ${this.accessory.displayName} Unknown statusCode.`);
374
+ this.debugLog(`Air Conditioner: ${this.accessory.displayName} Unknown statusCode.`);
375
+ }
376
+ }
377
+
378
+ public apiError(e: any) {
379
+ this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
380
+ this.service.updateCharacteristic(this.platform.Characteristic.RotationSpeed, e);
381
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
382
+ this.service.updateCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState, e);
383
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
384
+ this.service.updateCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature, e);
385
+ //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
386
+ }
387
+
388
+ /**
389
+ * Logging for Device
390
+ */
391
+ infoLog(...log: any[]) {
392
+ if (this.enablingDeviceLogging()) {
393
+ this.platform.log.info(String(...log));
394
+ }
395
+ }
396
+
397
+ warnLog(...log: any[]) {
398
+ if (this.enablingDeviceLogging()) {
399
+ this.platform.log.warn(String(...log));
400
+ }
401
+ }
402
+
403
+ errorLog(...log: any[]) {
404
+ if (this.enablingDeviceLogging()) {
405
+ this.platform.log.error(String(...log));
406
+ }
407
+ }
408
+
409
+ debugLog(...log: any[]) {
410
+ if (this.enablingDeviceLogging()) {
411
+ if (this.deviceLogging === 'debug') {
412
+ this.platform.log.info('[DEBUG]', String(...log));
413
+ } else {
414
+ this.platform.log.debug(String(...log));
415
+ }
409
416
  }
410
417
  }
411
418
 
412
- public apiError() {
413
- throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
419
+ enablingDeviceLogging(): boolean {
420
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
414
421
  }
415
422
  }
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  import { SwitchBotPlatform } from '../platform';
3
3
  import { irDevicesConfig, DeviceURL, irdevice, payload } from '../settings';
4
- import { CharacteristicValue, HAPStatus, PlatformAccessory, Service } from 'homebridge';
4
+ import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
5
5
 
6
6
  /**
7
7
  * Platform Accessory
@@ -36,8 +36,7 @@ export class AirPurifier {
36
36
  static PURIFYING_AIR: number;
37
37
 
38
38
  // Config
39
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
40
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
39
+ deviceLogging!: string;
41
40
 
42
41
  constructor(
43
42
  private readonly platform: SwitchBotPlatform,
@@ -45,6 +44,7 @@ export class AirPurifier {
45
44
  public device: irdevice & irDevicesConfig,
46
45
  ) {
47
46
  // default placeholders
47
+ this.logs();
48
48
  if (this.Active === undefined) {
49
49
  this.Active = this.platform.Characteristic.Active.INACTIVE;
50
50
  } else {
@@ -87,8 +87,18 @@ export class AirPurifier {
87
87
  this.service.getCharacteristic(this.platform.Characteristic.TargetAirPurifierState).onSet(this.TargetAirPurifierStateSet.bind(this));
88
88
  }
89
89
 
90
+ logs() {
91
+ if (this.device.logging) {
92
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
93
+ } else if (this.platform.config.options?.logging) {
94
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
95
+ } else {
96
+ this.deviceLogging = this.accessory.context.logging = 'standard';
97
+ }
98
+ }
99
+
90
100
  private ActiveSet(value: CharacteristicValue) {
91
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Set Active: ${value}`);
101
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Set Active: ${value}`);
92
102
  if (value === this.platform.Characteristic.Active.INACTIVE) {
93
103
  this.pushAirConditionerOffChanges();
94
104
  } else {
@@ -101,23 +111,23 @@ export class AirPurifier {
101
111
 
102
112
  private updateHomeKitCharacteristics() {
103
113
  if (this.Active === undefined) {
104
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Active: ${this.Active}`);
114
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Active: ${this.Active}`);
105
115
  } else {
106
116
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
107
- this.platform.device(`Air Purifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
117
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
108
118
  }
109
119
  if (this.CurrentAirPurifierState === undefined) {
110
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
120
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
111
121
  } else {
112
122
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, this.CurrentAirPurifierState);
113
- this.platform.device(`Air Purifier: ${this.accessory.displayName}`
123
+ this.debugLog(`Air Purifier: ${this.accessory.displayName}`
114
124
  + ` updateCharacteristic CurrentAirPurifierState: ${this.CurrentAirPurifierState}`);
115
125
  }
116
126
  if (this.CurrentHeaterCoolerState === undefined) {
117
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
127
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
118
128
  } else {
119
129
  this.service?.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, this.CurrentHeaterCoolerState);
120
- this.platform.device(`Air Purifier: ${this.accessory.displayName}`
130
+ this.debugLog(`Air Purifier: ${this.accessory.displayName}`
121
131
  + ` updateCharacteristic CurrentHeaterCoolerState: ${this.CurrentHeaterCoolerState}`);
122
132
  }
123
133
  }
@@ -218,60 +228,99 @@ export class AirPurifier {
218
228
 
219
229
  public async pushChanges(payload: payload) {
220
230
  try {
221
- this.platform.log.info(`Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
231
+ this.infoLog(`Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
222
232
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
223
233
 
224
234
  // Make the API request
225
235
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
226
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} pushChanges: ${push.data}`);
236
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} pushChanges: ${push.data}`);
227
237
  this.statusCode(push);
228
238
  this.updateHomeKitCharacteristics();
229
239
  this.CurrentTemperatureCached = this.CurrentTemperature;
230
240
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
231
241
  } catch (e: any) {
232
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
233
- if (this.deviceDebug) {
234
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
242
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
243
+ if (this.deviceLogging === 'debug') {
244
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
235
245
  + ` Error Message: ${JSON.stringify(e.message)}`);
236
246
  }
237
- if (this.debugDebug) {
238
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
247
+ if (this.platform.debugMode) {
248
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
239
249
  + ` Error: ${JSON.stringify(e)}`);
240
250
  }
241
- this.apiError();
251
+ this.apiError(e);
242
252
  }
243
253
  }
244
254
 
245
255
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
246
256
  switch (push.data.statusCode) {
247
257
  case 151:
248
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Command not supported by this device type.`);
258
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Command not supported by this device type.`);
249
259
  break;
250
260
  case 152:
251
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device not found.`);
261
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device not found.`);
252
262
  break;
253
263
  case 160:
254
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Command is not supported.`);
264
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Command is not supported.`);
255
265
  break;
256
266
  case 161:
257
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device is offline.`);
267
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device is offline.`);
258
268
  break;
259
269
  case 171:
260
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
270
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
261
271
  break;
262
272
  case 190:
263
- this.platform.log.error(`Air Purifier: ${this.accessory.displayName} Device internal error due to device states not synchronized`
273
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} Device internal error due to device states not synchronized`
264
274
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
265
275
  break;
266
276
  case 100:
267
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Command successfully sent.`);
277
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Command successfully sent.`);
268
278
  break;
269
279
  default:
270
- this.platform.debug(`Air Purifier: ${this.accessory.displayName} Unknown statusCode.`);
280
+ this.debugLog(`Air Purifier: ${this.accessory.displayName} Unknown statusCode.`);
281
+ }
282
+ }
283
+
284
+ public apiError(e: any) {
285
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState, e);
286
+ this.service.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, e);
287
+ this.service.updateCharacteristic(this.platform.Characteristic.TargetAirPurifierState, e);
288
+ this.service.updateCharacteristic(this.platform.Characteristic.Active, e);
289
+ //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
290
+ }
291
+
292
+ /**
293
+ * Logging for Device
294
+ */
295
+ infoLog(...log: any[]) {
296
+ if (this.enablingDeviceLogging()) {
297
+ this.platform.log.info(String(...log));
298
+ }
299
+ }
300
+
301
+ warnLog(...log: any[]) {
302
+ if (this.enablingDeviceLogging()) {
303
+ this.platform.log.warn(String(...log));
304
+ }
305
+ }
306
+
307
+ errorLog(...log: any[]) {
308
+ if (this.enablingDeviceLogging()) {
309
+ this.platform.log.error(String(...log));
310
+ }
311
+ }
312
+
313
+ debugLog(...log: any[]) {
314
+ if (this.enablingDeviceLogging()) {
315
+ if (this.deviceLogging === 'debug') {
316
+ this.platform.log.info('[DEBUG]', String(...log));
317
+ } else {
318
+ this.platform.log.debug(String(...log));
319
+ }
271
320
  }
272
321
  }
273
322
 
274
- public apiError() {
275
- throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
323
+ enablingDeviceLogging(): boolean {
324
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
276
325
  }
277
326
  }