@switchbot/homebridge-switchbot 1.4.1-beta.0 → 1.4.1-beta.4

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 +12 -2
  4. package/dist/devices/bots.d.ts.map +1 -1
  5. package/dist/devices/bots.js +155 -89
  6. package/dist/devices/bots.js.map +1 -1
  7. package/dist/devices/colorbulb.d.ts +12 -2
  8. package/dist/devices/colorbulb.d.ts.map +1 -1
  9. package/dist/devices/colorbulb.js +153 -84
  10. package/dist/devices/colorbulb.js.map +1 -1
  11. package/dist/devices/contact.d.ts +12 -2
  12. package/dist/devices/contact.d.ts.map +1 -1
  13. package/dist/devices/contact.js +116 -52
  14. package/dist/devices/contact.js.map +1 -1
  15. package/dist/devices/curtains.d.ts +13 -4
  16. package/dist/devices/curtains.d.ts.map +1 -1
  17. package/dist/devices/curtains.js +191 -124
  18. package/dist/devices/curtains.js.map +1 -1
  19. package/dist/devices/humidifiers.d.ts +12 -2
  20. package/dist/devices/humidifiers.d.ts.map +1 -1
  21. package/dist/devices/humidifiers.js +171 -108
  22. package/dist/devices/humidifiers.js.map +1 -1
  23. package/dist/devices/meters.d.ts +12 -2
  24. package/dist/devices/meters.d.ts.map +1 -1
  25. package/dist/devices/meters.js +113 -49
  26. package/dist/devices/meters.js.map +1 -1
  27. package/dist/devices/motion.d.ts +12 -2
  28. package/dist/devices/motion.d.ts.map +1 -1
  29. package/dist/devices/motion.js +101 -37
  30. package/dist/devices/motion.js.map +1 -1
  31. package/dist/devices/plugs.d.ts +12 -2
  32. package/dist/devices/plugs.d.ts.map +1 -1
  33. package/dist/devices/plugs.js +95 -31
  34. package/dist/devices/plugs.js.map +1 -1
  35. package/dist/irdevices/airconditioners.d.ts +10 -2
  36. package/dist/irdevices/airconditioners.d.ts.map +1 -1
  37. package/dist/irdevices/airconditioners.js +77 -37
  38. package/dist/irdevices/airconditioners.js.map +1 -1
  39. package/dist/irdevices/airpurifiers.d.ts +10 -2
  40. package/dist/irdevices/airpurifiers.d.ts.map +1 -1
  41. package/dist/irdevices/airpurifiers.js +65 -25
  42. package/dist/irdevices/airpurifiers.js.map +1 -1
  43. package/dist/irdevices/cameras.d.ts +10 -2
  44. package/dist/irdevices/cameras.d.ts.map +1 -1
  45. package/dist/irdevices/cameras.js +61 -21
  46. package/dist/irdevices/cameras.js.map +1 -1
  47. package/dist/irdevices/fans.d.ts +10 -2
  48. package/dist/irdevices/fans.d.ts.map +1 -1
  49. package/dist/irdevices/fans.js +71 -30
  50. package/dist/irdevices/fans.js.map +1 -1
  51. package/dist/irdevices/lights.d.ts +10 -2
  52. package/dist/irdevices/lights.d.ts.map +1 -1
  53. package/dist/irdevices/lights.js +62 -22
  54. package/dist/irdevices/lights.js.map +1 -1
  55. package/dist/irdevices/others.d.ts +10 -2
  56. package/dist/irdevices/others.d.ts.map +1 -1
  57. package/dist/irdevices/others.js +68 -28
  58. package/dist/irdevices/others.js.map +1 -1
  59. package/dist/irdevices/tvs.d.ts +10 -2
  60. package/dist/irdevices/tvs.d.ts.map +1 -1
  61. package/dist/irdevices/tvs.js +78 -38
  62. package/dist/irdevices/tvs.js.map +1 -1
  63. package/dist/irdevices/vacuumcleaners.d.ts +10 -2
  64. package/dist/irdevices/vacuumcleaners.d.ts.map +1 -1
  65. package/dist/irdevices/vacuumcleaners.js +61 -21
  66. package/dist/irdevices/vacuumcleaners.js.map +1 -1
  67. package/dist/irdevices/waterheaters.d.ts +10 -2
  68. package/dist/irdevices/waterheaters.d.ts.map +1 -1
  69. package/dist/irdevices/waterheaters.js +61 -21
  70. package/dist/irdevices/waterheaters.js.map +1 -1
  71. package/dist/platform.d.ts +10 -10
  72. package/dist/platform.d.ts.map +1 -1
  73. package/dist/platform.js +290 -307
  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 +161 -89
  81. package/src/devices/colorbulb.ts +161 -86
  82. package/src/devices/contact.ts +122 -52
  83. package/src/devices/curtains.ts +199 -128
  84. package/src/devices/humidifiers.ts +177 -109
  85. package/src/devices/meters.ts +119 -49
  86. package/src/devices/motion.ts +106 -36
  87. package/src/devices/plugs.ts +99 -31
  88. package/src/irdevices/airconditioners.ts +81 -36
  89. package/src/irdevices/airpurifiers.ts +69 -24
  90. package/src/irdevices/cameras.ts +65 -20
  91. package/src/irdevices/fans.ts +75 -30
  92. package/src/irdevices/lights.ts +66 -21
  93. package/src/irdevices/others.ts +72 -27
  94. package/src/irdevices/tvs.ts +82 -37
  95. package/src/irdevices/vacuumcleaners.ts +65 -20
  96. package/src/irdevices/waterheaters.ts +65 -20
  97. package/src/platform.ts +285 -302
  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
@@ -17,8 +17,7 @@ export class Others {
17
17
  ActiveCached!: CharacteristicValue;
18
18
 
19
19
  // Config
20
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
21
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
20
+ deviceLogging!: string;
22
21
 
23
22
  constructor(
24
23
  private readonly platform: SwitchBotPlatform,
@@ -26,6 +25,7 @@ export class Others {
26
25
  public device: irdevice & irDevicesConfig,
27
26
  ) {
28
27
  // default placeholders
28
+ this.logs();
29
29
  if (this.Active === undefined) {
30
30
  this.Active = this.platform.Characteristic.Active.INACTIVE;
31
31
  } else {
@@ -50,12 +50,22 @@ export class Others {
50
50
  this.service.getCharacteristic(this.platform.Characteristic.Active).onSet(this.ActiveSet.bind(this));
51
51
  } else {
52
52
  accessory.removeService(this.service!);
53
- this.platform.log.error(`Other: ${this.accessory.displayName} No Device Type Set, deviceType: ${device.other?.deviceType}`);
53
+ this.errorLog(`Other: ${this.accessory.displayName} No Device Type Set, deviceType: ${device.other?.deviceType}`);
54
+ }
55
+ }
56
+
57
+ logs() {
58
+ if (this.device.logging) {
59
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
60
+ } else if (this.platform.config.options?.logging) {
61
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
62
+ } else {
63
+ this.deviceLogging = this.accessory.context.logging = 'standard';
54
64
  }
55
65
  }
56
66
 
57
67
  private ActiveSet(value: CharacteristicValue) {
58
- this.platform.debug(`Other: ${this.accessory.displayName} On: ${value}`);
68
+ this.debugLog(`Other: ${this.accessory.displayName} On: ${value}`);
59
69
  if (this.Active) {
60
70
  this.pushOnChanges();
61
71
  } else {
@@ -68,10 +78,10 @@ export class Others {
68
78
 
69
79
  private updateHomeKitCharacteristics() {
70
80
  if (this.Active === undefined) {
71
- this.platform.debug(`Other: ${this.accessory.displayName} Active: ${this.Active}`);
81
+ this.debugLog(`Other: ${this.accessory.displayName} Active: ${this.Active}`);
72
82
  } else {
73
83
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
74
- this.platform.device(`Other: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
84
+ this.debugLog(`Other: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
75
85
  }
76
86
  }
77
87
 
@@ -98,13 +108,13 @@ export class Others {
98
108
  await this.pushChanges(payload);
99
109
  }
100
110
  } else {
101
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set, commandOn: ${this.device.other.commandOn}`);
111
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set, commandOn: ${this.device.other.commandOn}`);
102
112
  }
103
113
  } else {
104
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set, other: ${this.device.other}`);
114
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set, other: ${this.device.other}`);
105
115
  }
106
116
  } else {
107
- this.platform.log.error(`Other: ${this.accessory.displayName} On Command not set`);
117
+ this.errorLog(`Other: ${this.accessory.displayName} On Command not set`);
108
118
  }
109
119
  }
110
120
 
@@ -121,34 +131,34 @@ export class Others {
121
131
  await this.pushChanges(payload);
122
132
  }
123
133
  } else {
124
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set, commandOff: ${this.device.other.commandOff}`);
134
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set, commandOff: ${this.device.other.commandOff}`);
125
135
  }
126
136
  } else {
127
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set, other: ${this.device.other}`);
137
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set, other: ${this.device.other}`);
128
138
  }
129
139
  } else {
130
- this.platform.log.error(`Other: ${this.accessory.displayName} Off Command not set.`);
140
+ this.errorLog(`Other: ${this.accessory.displayName} Off Command not set.`);
131
141
  }
132
142
  }
133
143
 
134
144
  public async pushChanges(payload: payload) {
135
145
  try {
136
- this.platform.log.info(`Other: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
146
+ this.infoLog(`Other: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
137
147
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
138
148
 
139
149
  // Make the API request
140
150
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
141
- this.platform.debug(`Other: ${this.accessory.displayName} pushChanges: ${push.data}`);
151
+ this.debugLog(`Other: ${this.accessory.displayName} pushChanges: ${push.data}`);
142
152
  this.statusCode(push);
143
153
  this.updateHomeKitCharacteristics();
144
154
  } catch (e: any) {
145
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
146
- if (this.deviceDebug) {
147
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
155
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
156
+ if (this.deviceLogging === 'debug') {
157
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
148
158
  + ` Error Message: ${JSON.stringify(e.message)}`);
149
159
  }
150
- if (this.debugDebug) {
151
- this.platform.log.error(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
160
+ if (this.platform.debugMode) {
161
+ this.errorLog(`Other: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
152
162
  + ` Error: ${JSON.stringify(e)}`);
153
163
  }
154
164
  this.apiError();
@@ -158,33 +168,68 @@ export class Others {
158
168
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
159
169
  switch (push.data.statusCode) {
160
170
  case 151:
161
- this.platform.log.error(`Other: ${this.accessory.displayName} Command not supported by this device type.`);
171
+ this.errorLog(`Other: ${this.accessory.displayName} Command not supported by this device type.`);
162
172
  break;
163
173
  case 152:
164
- this.platform.log.error(`Other: ${this.accessory.displayName} Device not found.`);
174
+ this.errorLog(`Other: ${this.accessory.displayName} Device not found.`);
165
175
  break;
166
176
  case 160:
167
- this.platform.log.error(`Other: ${this.accessory.displayName} Command is not supported.`);
177
+ this.errorLog(`Other: ${this.accessory.displayName} Command is not supported.`);
168
178
  break;
169
179
  case 161:
170
- this.platform.log.error(`Other: ${this.accessory.displayName} Device is offline.`);
180
+ this.errorLog(`Other: ${this.accessory.displayName} Device is offline.`);
171
181
  break;
172
182
  case 171:
173
- this.platform.log.error(`Other: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
183
+ this.errorLog(`Other: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
174
184
  break;
175
185
  case 190:
176
- this.platform.log.error(`Other: ${this.accessory.displayName} Device internal error due to device states not synchronized`
186
+ this.errorLog(`Other: ${this.accessory.displayName} Device internal error due to device states not synchronized`
177
187
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
178
188
  break;
179
189
  case 100:
180
- this.platform.debug(`Other: ${this.accessory.displayName} Command successfully sent.`);
190
+ this.debugLog(`Other: ${this.accessory.displayName} Command successfully sent.`);
181
191
  break;
182
192
  default:
183
- this.platform.debug(`Other: ${this.accessory.displayName} Unknown statusCode.`);
193
+ this.debugLog(`Other: ${this.accessory.displayName} Unknown statusCode.`);
184
194
  }
185
195
  }
186
196
 
187
197
  public apiError() {
188
198
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
189
199
  }
200
+
201
+ /**
202
+ * Logging for Device
203
+ */
204
+ infoLog(...log: any[]) {
205
+ if (this.enablingDeviceLogging()) {
206
+ this.platform.log.info(String(...log));
207
+ }
208
+ }
209
+
210
+ warnLog(...log: any[]) {
211
+ if (this.enablingDeviceLogging()) {
212
+ this.platform.log.warn(String(...log));
213
+ }
214
+ }
215
+
216
+ errorLog(...log: any[]) {
217
+ if (this.enablingDeviceLogging()) {
218
+ this.platform.log.error(String(...log));
219
+ }
220
+ }
221
+
222
+ debugLog(...log: any[]) {
223
+ if (this.enablingDeviceLogging()) {
224
+ if (this.deviceLogging === 'debug') {
225
+ this.platform.log.info('[DEBUG]', String(...log));
226
+ } else {
227
+ this.platform.log.debug(String(...log));
228
+ }
229
+ }
230
+ }
231
+
232
+ enablingDeviceLogging(): boolean {
233
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
234
+ }
190
235
  }
@@ -22,8 +22,7 @@ export class TV {
22
22
  deviceStatus!: deviceStatusResponse;
23
23
 
24
24
  // Config
25
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
26
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
25
+ deviceLogging!: string;
27
26
 
28
27
  constructor(
29
28
  private readonly platform: SwitchBotPlatform,
@@ -31,6 +30,7 @@ export class TV {
31
30
  public device: irdevice & irDevicesConfig,
32
31
  ) {
33
32
  // default placeholders
33
+ this.logs();
34
34
  if (this.Active === undefined) {
35
35
  this.Active = this.platform.Characteristic.Active.INACTIVE;
36
36
  } else {
@@ -128,8 +128,18 @@ export class TV {
128
128
  .onSet(this.VolumeSelectorSet.bind(this));
129
129
  }
130
130
 
131
+ logs() {
132
+ if (this.device.logging) {
133
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
134
+ } else if (this.platform.config.options?.logging) {
135
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
136
+ } else {
137
+ this.deviceLogging = this.accessory.context.logging = 'standard';
138
+ }
139
+ }
140
+
131
141
  private VolumeSelectorSet(value: CharacteristicValue) {
132
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} VolumeSelector: ${value}`);
142
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} VolumeSelector: ${value}`);
133
143
  if (value === this.platform.Characteristic.VolumeSelector.INCREMENT) {
134
144
  this.pushVolumeUpChanges();
135
145
  } else {
@@ -140,61 +150,61 @@ export class TV {
140
150
  private RemoteKeySet(value: CharacteristicValue) {
141
151
  switch (value) {
142
152
  case this.platform.Characteristic.RemoteKey.REWIND: {
143
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: REWIND`);
153
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: REWIND`);
144
154
  break;
145
155
  }
146
156
  case this.platform.Characteristic.RemoteKey.FAST_FORWARD: {
147
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: FAST_FORWARD`);
157
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: FAST_FORWARD`);
148
158
  break;
149
159
  }
150
160
  case this.platform.Characteristic.RemoteKey.NEXT_TRACK: {
151
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: NEXT_TRACK`);
161
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: NEXT_TRACK`);
152
162
  break;
153
163
  }
154
164
  case this.platform.Characteristic.RemoteKey.PREVIOUS_TRACK: {
155
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PREVIOUS_TRACK`);
165
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PREVIOUS_TRACK`);
156
166
  break;
157
167
  }
158
168
  case this.platform.Characteristic.RemoteKey.ARROW_UP: {
159
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_UP`);
169
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_UP`);
160
170
  //this.pushUpChanges();
161
171
  break;
162
172
  }
163
173
  case this.platform.Characteristic.RemoteKey.ARROW_DOWN: {
164
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_DOWN`);
174
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_DOWN`);
165
175
  //this.pushDownChanges();
166
176
  break;
167
177
  }
168
178
  case this.platform.Characteristic.RemoteKey.ARROW_LEFT: {
169
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_LEFT`);
179
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_LEFT`);
170
180
  //this.pushLeftChanges();
171
181
  break;
172
182
  }
173
183
  case this.platform.Characteristic.RemoteKey.ARROW_RIGHT: {
174
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_RIGHT`);
184
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: ARROW_RIGHT`);
175
185
  //this.pushRightChanges();
176
186
  break;
177
187
  }
178
188
  case this.platform.Characteristic.RemoteKey.SELECT: {
179
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: SELECT`);
189
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: SELECT`);
180
190
  //this.pushOkChanges();
181
191
  break;
182
192
  }
183
193
  case this.platform.Characteristic.RemoteKey.BACK: {
184
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: BACK`);
194
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: BACK`);
185
195
  //this.pushBackChanges();
186
196
  break;
187
197
  }
188
198
  case this.platform.Characteristic.RemoteKey.EXIT: {
189
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: EXIT`);
199
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: EXIT`);
190
200
  break;
191
201
  }
192
202
  case this.platform.Characteristic.RemoteKey.PLAY_PAUSE: {
193
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PLAY_PAUSE`);
203
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: PLAY_PAUSE`);
194
204
  break;
195
205
  }
196
206
  case this.platform.Characteristic.RemoteKey.INFORMATION: {
197
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: INFORMATION`);
207
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Set Remote Key Pressed: INFORMATION`);
198
208
  //this.pushMenuChanges();
199
209
  break;
200
210
  }
@@ -202,12 +212,12 @@ export class TV {
202
212
  }
203
213
 
204
214
  private ActiveIdentifierSet(value: CharacteristicValue) {
205
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${value}`);
215
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${value}`);
206
216
  this.ActiveIdentifier = value;
207
217
  }
208
218
 
209
219
  private ActiveSet(value: CharacteristicValue) {
210
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${value}`);
220
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${value}`);
211
221
  if (!this.device.irtv?.disable_power) {
212
222
  if (value === this.platform.Characteristic.Active.INACTIVE) {
213
223
  this.pushTvOffChanges();
@@ -222,16 +232,16 @@ export class TV {
222
232
 
223
233
  private updateHomeKitCharacteristics() {
224
234
  if (this.Active === undefined) {
225
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
235
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Active: ${this.Active}`);
226
236
  } else {
227
237
  this.service?.updateCharacteristic(this.platform.Characteristic.Active, this.Active);
228
- this.platform.device(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
238
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} updateCharacteristic Active: ${this.Active}`);
229
239
  }
230
240
  if (this.ActiveIdentifier === undefined) {
231
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${this.ActiveIdentifier}`);
241
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} ActiveIdentifier: ${this.ActiveIdentifier}`);
232
242
  } else {
233
243
  this.service?.updateCharacteristic(this.platform.Characteristic.ActiveIdentifier, this.ActiveIdentifier);
234
- this.platform.device(`${this.device.remoteType}: ${this.accessory.displayName}`
244
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName}`
235
245
  + ` updateCharacteristic ActiveIdentifier: ${this.ActiveIdentifier}`);
236
246
  }
237
247
  }
@@ -349,22 +359,22 @@ export class TV {
349
359
 
350
360
  public async pushTVChanges(payload: payload) {
351
361
  try {
352
- this.platform.log.info(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
362
+ this.infoLog(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
353
363
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
354
364
 
355
365
  // Make the API request
356
366
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
357
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges: ${push.data}`);
367
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushChanges: ${push.data}`);
358
368
  this.statusCode(push);
359
369
  this.updateHomeKitCharacteristics();
360
370
  } catch (e: any) {
361
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
362
- if (this.deviceDebug) {
363
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
371
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
372
+ if (this.deviceLogging === 'debug') {
373
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
364
374
  + ` Error Message: ${JSON.stringify(e.message)}`);
365
375
  }
366
- if (this.debugDebug) {
367
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
376
+ if (this.platform.debugMode) {
377
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
368
378
  + ` Error: ${JSON.stringify(e)}`);
369
379
  }
370
380
  this.apiError();
@@ -375,34 +385,69 @@ export class TV {
375
385
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
376
386
  switch (push.data.statusCode) {
377
387
  case 151:
378
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this device type.`);
388
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command not supported by this device type.`);
379
389
  break;
380
390
  case 152:
381
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Device not found.`);
391
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device not found.`);
382
392
  break;
383
393
  case 160:
384
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported.`);
394
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Command is not supported.`);
385
395
  break;
386
396
  case 161:
387
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline.`);
397
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Device is offline.`);
388
398
  break;
389
399
  case 171:
390
- this.platform.log.error(`${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
400
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
391
401
  break;
392
402
  case 190:
393
- this.platform.log.error(`${this.device.remoteType}: `
403
+ this.errorLog(`${this.device.remoteType}: `
394
404
  + `${this.accessory.displayName} Device internal error due to device states not synchronized with server,`
395
405
  + ` Or command: ${JSON.stringify(push.data)} format is invalid`);
396
406
  break;
397
407
  case 100:
398
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent.`);
408
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Command successfully sent.`);
399
409
  break;
400
410
  default:
401
- this.platform.debug(`${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode.`);
411
+ this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} Unknown statusCode.`);
402
412
  }
403
413
  }
404
414
 
405
415
  public apiError() {
406
416
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
407
417
  }
418
+
419
+ /**
420
+ * Logging for Device
421
+ */
422
+ infoLog(...log: any[]) {
423
+ if (this.enablingDeviceLogging()) {
424
+ this.platform.log.info(String(...log));
425
+ }
426
+ }
427
+
428
+ warnLog(...log: any[]) {
429
+ if (this.enablingDeviceLogging()) {
430
+ this.platform.log.warn(String(...log));
431
+ }
432
+ }
433
+
434
+ errorLog(...log: any[]) {
435
+ if (this.enablingDeviceLogging()) {
436
+ this.platform.log.error(String(...log));
437
+ }
438
+ }
439
+
440
+ debugLog(...log: any[]) {
441
+ if (this.enablingDeviceLogging()) {
442
+ if (this.deviceLogging === 'debug') {
443
+ this.platform.log.info('[DEBUG]', String(...log));
444
+ } else {
445
+ this.platform.log.debug(String(...log));
446
+ }
447
+ }
448
+ }
449
+
450
+ enablingDeviceLogging(): boolean {
451
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
452
+ }
408
453
  }
@@ -17,8 +17,7 @@ export class VacuumCleaner {
17
17
  OnCached!: CharacteristicValue;
18
18
 
19
19
  // Config
20
- private readonly deviceDebug = this.platform.config.options?.debug === 'device' || this.platform.debugMode;
21
- private readonly debugDebug = this.platform.config.options?.debug === 'debug' || this.platform.debugMode;
20
+ deviceLogging!: string;
22
21
 
23
22
  constructor(
24
23
  private readonly platform: SwitchBotPlatform,
@@ -26,6 +25,7 @@ export class VacuumCleaner {
26
25
  public device: irdevice & irDevicesConfig,
27
26
  ) {
28
27
  // default placeholders
28
+ this.logs();
29
29
  if (this.On === undefined) {
30
30
  this.On = false;
31
31
  } else {
@@ -57,8 +57,18 @@ export class VacuumCleaner {
57
57
  this.service.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
58
58
  }
59
59
 
60
+ logs() {
61
+ if (this.device.logging) {
62
+ this.deviceLogging = this.accessory.context.logging = this.device.logging;
63
+ } else if (this.platform.config.options?.logging) {
64
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
65
+ } else {
66
+ this.deviceLogging = this.accessory.context.logging = 'standard';
67
+ }
68
+ }
69
+
60
70
  private OnSet(value: CharacteristicValue) {
61
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} On: ${value}`);
71
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} On: ${value}`);
62
72
  this.On = value;
63
73
  if (this.On) {
64
74
  this.pushOnChanges();
@@ -69,10 +79,10 @@ export class VacuumCleaner {
69
79
 
70
80
  private updateHomeKitCharacteristics() {
71
81
  if (this.On === undefined) {
72
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} On: ${this.On}`);
82
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} On: ${this.On}`);
73
83
  } else {
74
84
  this.service?.updateCharacteristic(this.platform.Characteristic.On, this.On);
75
- this.platform.device(`Vacuum Cleaner: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
85
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
76
86
  }
77
87
  }
78
88
 
@@ -110,24 +120,24 @@ export class VacuumCleaner {
110
120
 
111
121
  public async pushChanges(payload: payload) {
112
122
  try {
113
- this.platform.log.info(`Vacuum Cleaner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
123
+ this.infoLog(`Vacuum Cleaner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},`
114
124
  + ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`);
115
125
 
116
126
  // Make the API request
117
127
  const push = await this.platform.axios.post(`${DeviceURL}/${this.device.deviceId}/commands`, payload);
118
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} pushChanges: ${push.data}`);
128
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} pushChanges: ${push.data}`);
119
129
  this.statusCode(push);
120
130
  this.OnCached = this.On;
121
131
  this.accessory.context.On = this.OnCached;
122
132
  this.updateHomeKitCharacteristics();
123
133
  } catch (e: any) {
124
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
125
- if (this.deviceDebug) {
126
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
134
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
135
+ if (this.deviceLogging === 'debug') {
136
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
127
137
  + ` Error Message: ${JSON.stringify(e.message)}`);
128
138
  }
129
- if (this.debugDebug) {
130
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
139
+ if (this.platform.debugMode) {
140
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
131
141
  + ` Error: ${JSON.stringify(e)}`);
132
142
  }
133
143
  this.apiError();
@@ -137,33 +147,68 @@ export class VacuumCleaner {
137
147
  private statusCode(push: AxiosResponse<{ statusCode: number; }>) {
138
148
  switch (push.data.statusCode) {
139
149
  case 151:
140
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Command not supported by this device type.`);
150
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Command not supported by this device type.`);
141
151
  break;
142
152
  case 152:
143
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device not found.`);
153
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Device not found.`);
144
154
  break;
145
155
  case 160:
146
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Command is not supported.`);
156
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Command is not supported.`);
147
157
  break;
148
158
  case 161:
149
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device is offline.`);
159
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Device is offline.`);
150
160
  break;
151
161
  case 171:
152
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
162
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
153
163
  break;
154
164
  case 190:
155
- this.platform.log.error(`Vacuum Cleaner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
165
+ this.errorLog(`Vacuum Cleaner: ${this.accessory.displayName} Device internal error due to device states not synchronized`
156
166
  + ` with server, Or command: ${JSON.stringify(push.data)} format is invalid`);
157
167
  break;
158
168
  case 100:
159
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} Command successfully sent.`);
169
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} Command successfully sent.`);
160
170
  break;
161
171
  default:
162
- this.platform.debug(`Vacuum Cleaner: ${this.accessory.displayName} Unknown statusCode.`);
172
+ this.debugLog(`Vacuum Cleaner: ${this.accessory.displayName} Unknown statusCode.`);
163
173
  }
164
174
  }
165
175
 
166
176
  public apiError() {
167
177
  throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
168
178
  }
179
+
180
+ /**
181
+ * Logging for Device
182
+ */
183
+ infoLog(...log: any[]) {
184
+ if (this.enablingDeviceLogging()) {
185
+ this.platform.log.info(String(...log));
186
+ }
187
+ }
188
+
189
+ warnLog(...log: any[]) {
190
+ if (this.enablingDeviceLogging()) {
191
+ this.platform.log.warn(String(...log));
192
+ }
193
+ }
194
+
195
+ errorLog(...log: any[]) {
196
+ if (this.enablingDeviceLogging()) {
197
+ this.platform.log.error(String(...log));
198
+ }
199
+ }
200
+
201
+ debugLog(...log: any[]) {
202
+ if (this.enablingDeviceLogging()) {
203
+ if (this.deviceLogging === 'debug') {
204
+ this.platform.log.info('[DEBUG]', String(...log));
205
+ } else {
206
+ this.platform.log.debug(String(...log));
207
+ }
208
+ }
209
+ }
210
+
211
+ enablingDeviceLogging(): boolean {
212
+ return this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
213
+ }
169
214
  }