@switchbot/homebridge-switchbot 1.3.1-beta.9 → 1.4.1-beta.2

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