@switchbot/homebridge-switchbot 1.15.1-beta.9 → 2.0.0-beta.1

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 (90) hide show
  1. package/.github/Old Releaser.zip +0 -0
  2. package/.github/npm-version-script.js +81 -0
  3. package/.github/workflows/beta-release.yml +31 -0
  4. package/.github/workflows/build.yml +18 -0
  5. package/.github/workflows/codeql-analysis.yml +5 -63
  6. package/.github/workflows/label.yml +2 -2
  7. package/.github/workflows/release-drafter.yml +14 -0
  8. package/.github/workflows/release.yml +24 -0
  9. package/.github/workflows/stale.yml +6 -22
  10. package/README.md +10 -3
  11. package/dist/device/bot.js +1 -1
  12. package/dist/device/bot.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts +1 -0
  14. package/dist/device/colorbulb.d.ts.map +1 -1
  15. package/dist/device/colorbulb.js +33 -7
  16. package/dist/device/colorbulb.js.map +1 -1
  17. package/dist/device/contact.js +1 -1
  18. package/dist/device/contact.js.map +1 -1
  19. package/dist/device/curtain.d.ts.map +1 -1
  20. package/dist/device/curtain.js +8 -2
  21. package/dist/device/curtain.js.map +1 -1
  22. package/dist/device/humidifier.js +1 -1
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/meter.js +1 -1
  25. package/dist/device/meter.js.map +1 -1
  26. package/dist/device/meterplus.js +1 -1
  27. package/dist/device/meterplus.js.map +1 -1
  28. package/dist/device/motion.js +1 -1
  29. package/dist/device/motion.js.map +1 -1
  30. package/dist/device/plug.js +1 -1
  31. package/dist/device/plug.js.map +1 -1
  32. package/dist/irdevice/airconditioner.d.ts +1 -1
  33. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  34. package/dist/irdevice/airconditioner.js +35 -32
  35. package/dist/irdevice/airconditioner.js.map +1 -1
  36. package/dist/irdevice/airpurifier.d.ts +1 -1
  37. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  38. package/dist/irdevice/airpurifier.js +30 -28
  39. package/dist/irdevice/airpurifier.js.map +1 -1
  40. package/dist/irdevice/camera.d.ts +1 -1
  41. package/dist/irdevice/camera.d.ts.map +1 -1
  42. package/dist/irdevice/camera.js +28 -26
  43. package/dist/irdevice/camera.js.map +1 -1
  44. package/dist/irdevice/fan.d.ts +1 -1
  45. package/dist/irdevice/fan.d.ts.map +1 -1
  46. package/dist/irdevice/fan.js +47 -45
  47. package/dist/irdevice/fan.js.map +1 -1
  48. package/dist/irdevice/light.d.ts +1 -1
  49. package/dist/irdevice/light.d.ts.map +1 -1
  50. package/dist/irdevice/light.js +40 -38
  51. package/dist/irdevice/light.js.map +1 -1
  52. package/dist/irdevice/other.d.ts +1 -1
  53. package/dist/irdevice/other.d.ts.map +1 -1
  54. package/dist/irdevice/other.js +28 -26
  55. package/dist/irdevice/other.js.map +1 -1
  56. package/dist/irdevice/tv.d.ts +1 -1
  57. package/dist/irdevice/tv.d.ts.map +1 -1
  58. package/dist/irdevice/tv.js +82 -80
  59. package/dist/irdevice/tv.js.map +1 -1
  60. package/dist/irdevice/vacuumcleaner.d.ts +1 -1
  61. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  62. package/dist/irdevice/vacuumcleaner.js +24 -22
  63. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  64. package/dist/irdevice/waterheater.d.ts +1 -1
  65. package/dist/irdevice/waterheater.d.ts.map +1 -1
  66. package/dist/irdevice/waterheater.js +28 -26
  67. package/dist/irdevice/waterheater.js.map +1 -1
  68. package/package.json +79 -79
  69. package/src/device/bot.ts +1 -1
  70. package/src/device/colorbulb.ts +33 -9
  71. package/src/device/contact.ts +1 -1
  72. package/src/device/curtain.ts +8 -2
  73. package/src/device/humidifier.ts +1 -1
  74. package/src/device/meter.ts +1 -1
  75. package/src/device/meterplus.ts +1 -1
  76. package/src/device/motion.ts +1 -1
  77. package/src/device/plug.ts +1 -1
  78. package/src/irdevice/airconditioner.ts +35 -40
  79. package/src/irdevice/airpurifier.ts +30 -37
  80. package/src/irdevice/camera.ts +28 -33
  81. package/src/irdevice/fan.ts +47 -52
  82. package/src/irdevice/light.ts +40 -45
  83. package/src/irdevice/other.ts +28 -33
  84. package/src/irdevice/tv.ts +82 -87
  85. package/src/irdevice/vacuumcleaner.ts +24 -29
  86. package/src/irdevice/waterheater.ts +28 -33
  87. package/.github/labeler.yml +0 -13
  88. package/.github/workflows/nodejs-beta.yml +0 -42
  89. package/.github/workflows/nodejs.yml +0 -47
  90. package/.github/workflows/prerelease-beta.js +0 -34
@@ -183,22 +183,22 @@ export class AirConditioner {
183
183
  */
184
184
  async pushAirConditionerOnChanges(): Promise<void> {
185
185
  if (this.Active !== this.platform.Characteristic.Active.ACTIVE || this.pushOn) {
186
- const payload = {
187
- commandType: 'command',
188
- parameter: 'default',
189
- command: 'turnOn',
190
- } as any;
191
- await this.pushChanges(payload);
186
+ const body = JSON.stringify({
187
+ 'command': 'turnOn',
188
+ 'parameter': 'default',
189
+ 'commandType': 'command',
190
+ });
191
+ await this.pushChanges(body);
192
192
  }
193
193
  }
194
194
 
195
195
  async pushAirConditionerOffChanges(): Promise<void> {
196
- const payload = {
197
- commandType: 'command',
198
- parameter: 'default',
199
- command: 'turnOff',
200
- } as any;
201
- await this.pushChanges(payload);
196
+ const body = JSON.stringify({
197
+ 'command': 'turnOff',
198
+ 'parameter': 'default',
199
+ 'commandType': 'command',
200
+ });
201
+ await this.pushChanges(body);
202
202
  }
203
203
 
204
204
  async pushAirConditionerStatusChanges(): Promise<void> {
@@ -213,11 +213,6 @@ export class AirConditioner {
213
213
  }
214
214
 
215
215
  async pushAirConditionerDetailsChanges(): Promise<void> {
216
- const payload = {
217
- commandType: 'command',
218
- command: 'setAll',
219
- } as any;
220
-
221
216
  this.CurrentTemperatureUndefined();
222
217
  if (this.CurrentMode === undefined) {
223
218
  this.CurrentMode = 1;
@@ -236,7 +231,7 @@ export class AirConditioner {
236
231
  `Air Conditioner: ${this.accessory.displayName} CurrentMode: ${this.CurrentMode},` + ` CurrentTemperature: ${this.CurrentTemperature}`,
237
232
  );
238
233
  }
239
- payload.parameter = `${this.CurrentTemperature},${this.CurrentMode},${this.CurrentFanSpeed},${this.state}`;
234
+ const parameter = `${this.CurrentTemperature},${this.CurrentMode},${this.CurrentFanSpeed},${this.state}`;
240
235
 
241
236
  if (this.Active === this.platform.Characteristic.Active.ACTIVE) {
242
237
  this.CurrentTemperatureUndefined();
@@ -248,53 +243,53 @@ export class AirConditioner {
248
243
  } else {
249
244
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
250
245
  }
246
+ const body = JSON.stringify({
247
+ 'command': 'setAll',
248
+ 'parameter': `${parameter}`,
249
+ 'commandType': 'command',
250
+ });
251
251
 
252
- await this.pushChanges(payload);
252
+ await this.pushChanges(body);
253
253
  }
254
254
 
255
- async pushChanges(payload): Promise<void> {
255
+ async pushChanges(body): Promise<void> {
256
256
  try {
257
- this.infoLog(
258
- `Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
259
- ` parameter: [${payload.parameter}], commandType: ${payload.commandType}`,
260
- );
261
-
262
- // Make the API request
257
+ // Make Push On request to the API
263
258
  const t = Date.now();
264
259
  const nonce = 'requestID';
265
260
  const data = this.platform.config.credentials?.token + t + nonce;
266
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
261
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
262
+ .update(Buffer.from(data, 'utf-8'))
263
+ .digest();
267
264
  const sign = signTerm.toString('base64');
268
265
  this.debugLog(`Air Conditioner: ${this.accessory.displayName} sign: ${sign}`);
266
+ this.infoLog(`Air Conditioner: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
269
267
  const options = {
270
268
  hostname: HostDomain,
271
269
  port: 443,
272
270
  path: `${DevicePath}/${this.device.deviceId}/commands`,
273
271
  method: 'POST',
274
272
  headers: {
275
- Authorization: this.platform.config.credentials?.token,
276
- sign: sign,
277
- nonce: nonce,
278
- t: t,
273
+ 'Authorization': this.platform.config.credentials?.token,
274
+ 'sign': sign,
275
+ 'nonce': nonce,
276
+ 't': t,
279
277
  'Content-Type': 'application/json',
278
+ 'Content-Length': body.length,
280
279
  },
281
280
  };
282
-
283
- const req = https.request(options, (res) => {
281
+ const req = https.request(options, res => {
284
282
  this.debugLog(`Air Conditioner: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
285
283
  this.statusCode({ res });
286
- res.on('data', (d) => {
284
+ res.on('data', d => {
287
285
  this.debugLog(`Air Conditioner: ${this.accessory.displayName} d: ${d}`);
288
286
  });
289
287
  });
290
-
291
- req.on('error', (error) => {
292
- this.errorLog(`Air Conditioner: ${this.accessory.displayName} error: ${error}`);
288
+ req.on('error', (e: any) => {
289
+ this.errorLog(`Air Conditioner: ${this.accessory.displayName} error message: ${e.message}`);
293
290
  });
294
-
295
- req.write(payload);
291
+ req.write(body);
296
292
  req.end();
297
-
298
293
  this.debugLog(`Air Conditioner: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
299
294
  this.CurrentTemperatureCached = this.CurrentTemperature;
300
295
  this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
@@ -158,23 +158,23 @@ export class AirPurifier {
158
158
  */
159
159
  async pushAirPurifierOnChanges(): Promise<void> {
160
160
  if (this.Active !== 1) {
161
- const payload = {
162
- commandType: 'command',
163
- parameter: 'default',
164
- command: 'turnOn',
165
- };
166
- await this.pushChanges(payload);
161
+ const body = JSON.stringify({
162
+ 'command': 'turnOn',
163
+ 'parameter': 'default',
164
+ 'commandType': 'command',
165
+ });
166
+ await this.pushChanges(body);
167
167
  }
168
168
  }
169
169
 
170
170
  async pushAirPurifierOffChanges(): Promise<void> {
171
171
  if (this.Active !== 0) {
172
- const payload = {
173
- commandType: 'command',
174
- parameter: 'default',
175
- command: 'turnOff',
176
- };
177
- await this.pushChanges(payload);
172
+ const body = JSON.stringify({
173
+ 'command': 'turnOff',
174
+ 'parameter': 'default',
175
+ 'commandType': 'command',
176
+ });
177
+ await this.pushChanges(body);
178
178
  }
179
179
  }
180
180
 
@@ -195,12 +195,11 @@ export class AirPurifier {
195
195
  this.CurrentAPFanSpeed = this.CurrentFanSpeed || 1;
196
196
  this.APActive = this.Active === 1 ? 'on' : 'off';
197
197
  const parameter = `${this.CurrentAPTemp},${this.CurrentAPMode},${this.CurrentAPFanSpeed},${this.APActive}`;
198
- const payload = JSON.stringify({
198
+ const body = JSON.stringify({
199
199
  'command': 'setAll',
200
200
  'parameter': `${parameter}`,
201
201
  'commandType': 'command',
202
202
  });
203
-
204
203
  if (this.Active === 1) {
205
204
  if ((this.CurrentTemperature || 24) < (this.LastTemperature || 30)) {
206
205
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.COOLING;
@@ -210,53 +209,47 @@ export class AirPurifier {
210
209
  } else {
211
210
  this.CurrentHeaterCoolerState = this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
212
211
  }
213
-
214
- await this.pushChanges(payload);
212
+ await this.pushChanges(body);
215
213
  }
216
214
 
217
- async pushChanges(payload): Promise<void> {
215
+ async pushChanges(body): Promise<void> {
218
216
  try {
219
- this.infoLog(
220
- `Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
221
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
222
- );
223
-
224
- // Make the API request
217
+ // Make Push On request to the API
225
218
  const t = Date.now();
226
219
  const nonce = 'requestID';
227
220
  const data = this.platform.config.credentials?.token + t + nonce;
228
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
221
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
222
+ .update(Buffer.from(data, 'utf-8'))
223
+ .digest();
229
224
  const sign = signTerm.toString('base64');
230
225
  this.debugLog(`Air Purifier: ${this.accessory.displayName} sign: ${sign}`);
226
+ this.infoLog(`Air Purifier: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
231
227
  const options = {
232
228
  hostname: HostDomain,
233
229
  port: 443,
234
230
  path: `${DevicePath}/${this.device.deviceId}/commands`,
235
231
  method: 'POST',
236
232
  headers: {
237
- Authorization: this.platform.config.credentials?.token,
238
- sign: sign,
239
- nonce: nonce,
240
- t: t,
233
+ 'Authorization': this.platform.config.credentials?.token,
234
+ 'sign': sign,
235
+ 'nonce': nonce,
236
+ 't': t,
241
237
  'Content-Type': 'application/json',
238
+ 'Content-Length': body.length,
242
239
  },
243
240
  };
244
-
245
- const req = https.request(options, (res) => {
241
+ const req = https.request(options, res => {
246
242
  this.debugLog(`Air Purifier: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
247
243
  this.statusCode({ res });
248
- res.on('data', (d) => {
244
+ res.on('data', d => {
249
245
  this.debugLog(`Air Purifier: ${this.accessory.displayName} d: ${d}`);
250
246
  });
251
247
  });
252
-
253
- req.on('error', (error) => {
254
- this.errorLog(`Air Purifier: ${this.accessory.displayName} error: ${error}`);
248
+ req.on('error', (e: any) => {
249
+ this.errorLog(`Air Purifier: ${this.accessory.displayName} error message: ${e.message}`);
255
250
  });
256
-
257
- req.write(payload);
251
+ req.write(body);
258
252
  req.end();
259
-
260
253
  this.debugLog(`Air Purifier: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
261
254
  this.updateHomeKitCharacteristics();
262
255
  this.CurrentTemperatureCached = this.CurrentTemperature;
@@ -86,69 +86,64 @@ export class Camera {
86
86
  */
87
87
  async pushOnChanges(): Promise<void> {
88
88
  if (this.On) {
89
- const payload = {
90
- commandType: 'command',
91
- parameter: 'default',
92
- command: 'turnOn',
93
- };
94
- await this.pushChanges(payload);
89
+ const body = JSON.stringify({
90
+ 'command': 'turnOn',
91
+ 'parameter': 'default',
92
+ 'commandType': 'command',
93
+ });
94
+ await this.pushChanges(body);
95
95
  }
96
96
  }
97
97
 
98
98
  async pushOffChanges(): Promise<void> {
99
99
  if (!this.On) {
100
- const payload = {
101
- commandType: 'command',
102
- parameter: 'default',
103
- command: 'turnOff',
104
- };
105
- await this.pushChanges(payload);
100
+ const body = JSON.stringify({
101
+ 'command': 'turnOff',
102
+ 'parameter': 'default',
103
+ 'commandType': 'command',
104
+ });
105
+ await this.pushChanges(body);
106
106
  }
107
107
  }
108
108
 
109
- async pushChanges(payload): Promise<void> {
109
+ async pushChanges(body): Promise<void> {
110
110
  try {
111
- this.infoLog(
112
- `Camera: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
113
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
114
- );
115
-
116
- // Make the API request
111
+ // Make Push On request to the API
117
112
  const t = Date.now();
118
113
  const nonce = 'requestID';
119
114
  const data = this.platform.config.credentials?.token + t + nonce;
120
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
115
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
116
+ .update(Buffer.from(data, 'utf-8'))
117
+ .digest();
121
118
  const sign = signTerm.toString('base64');
122
119
  this.debugLog(`Camera: ${this.accessory.displayName} sign: ${sign}`);
120
+ this.infoLog(`Camera: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
123
121
  const options = {
124
122
  hostname: HostDomain,
125
123
  port: 443,
126
124
  path: `${DevicePath}/${this.device.deviceId}/commands`,
127
125
  method: 'POST',
128
126
  headers: {
129
- Authorization: this.platform.config.credentials?.token,
130
- sign: sign,
131
- nonce: nonce,
132
- t: t,
127
+ 'Authorization': this.platform.config.credentials?.token,
128
+ 'sign': sign,
129
+ 'nonce': nonce,
130
+ 't': t,
133
131
  'Content-Type': 'application/json',
132
+ 'Content-Length': body.length,
134
133
  },
135
134
  };
136
-
137
- const req = https.request(options, (res) => {
135
+ const req = https.request(options, res => {
138
136
  this.debugLog(`Camera: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
139
137
  this.statusCode({ res });
140
- res.on('data', (d) => {
138
+ res.on('data', d => {
141
139
  this.debugLog(`Camera: ${this.accessory.displayName} d: ${d}`);
142
140
  });
143
141
  });
144
-
145
- req.on('error', (error) => {
146
- this.errorLog(`Camera: ${this.accessory.displayName} error: ${error}`);
142
+ req.on('error', (e: any) => {
143
+ this.errorLog(`Camera: ${this.accessory.displayName} error message: ${e.message}`);
147
144
  });
148
-
149
- req.write(payload);
145
+ req.write(body);
150
146
  req.end();
151
-
152
147
  this.debugLog(`Camera: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
153
148
  this.updateHomeKitCharacteristics();
154
149
  this.OnCached = this.On;
@@ -188,94 +188,89 @@ export class Fan {
188
188
  */
189
189
  async pushFanOnChanges(): Promise<void> {
190
190
  if (this.Active !== 1) {
191
- const payload = {
192
- commandType: 'command',
193
- parameter: 'default',
194
- command: 'turnOn',
195
- };
196
- await this.pushTVChanges(payload);
191
+ const body = JSON.stringify({
192
+ 'command': 'turnOn',
193
+ 'parameter': 'default',
194
+ 'commandType': 'command',
195
+ });
196
+ await this.pushTVChanges(body);
197
197
  }
198
198
  }
199
199
 
200
200
  async pushFanOffChanges(): Promise<void> {
201
- const payload = {
202
- commandType: 'command',
203
- parameter: 'default',
204
- command: 'turnOff',
205
- };
206
- await this.pushTVChanges(payload);
201
+ const body = JSON.stringify({
202
+ 'command': 'turnOff',
203
+ 'parameter': 'default',
204
+ 'commandType': 'command',
205
+ });
206
+ await this.pushTVChanges(body);
207
207
  }
208
208
 
209
209
  async pushFanSpeedUpChanges(): Promise<void> {
210
- const payload = {
211
- commandType: 'command',
212
- parameter: 'default',
213
- command: 'highSpeed',
214
- };
215
- await this.pushTVChanges(payload);
210
+ const body = JSON.stringify({
211
+ 'command': 'highSpeed',
212
+ 'parameter': 'default',
213
+ 'commandType': 'command',
214
+ });
215
+ await this.pushTVChanges(body);
216
216
  }
217
217
 
218
218
  async pushFanSpeedDownChanges(): Promise<void> {
219
- const payload = {
220
- commandType: 'command',
221
- parameter: 'default',
222
- command: 'lowSpeed',
223
- };
224
- await this.pushTVChanges(payload);
219
+ const body = JSON.stringify({
220
+ 'command': 'lowSpeed',
221
+ 'parameter': 'default',
222
+ 'commandType': 'command',
223
+ });
224
+ await this.pushTVChanges(body);
225
225
  }
226
226
 
227
227
  async pushFanSwingChanges(): Promise<void> {
228
- const payload = {
229
- commandType: 'command',
230
- parameter: 'default',
231
- command: 'swing',
232
- };
233
- await this.pushTVChanges(payload);
228
+ const body = JSON.stringify({
229
+ 'command': 'swing',
230
+ 'parameter': 'default',
231
+ 'commandType': 'command',
232
+ });
233
+ await this.pushTVChanges(body);
234
234
  }
235
235
 
236
- async pushTVChanges(payload): Promise<void> {
236
+ async pushTVChanges(body): Promise<void> {
237
237
  try {
238
- this.infoLog(
239
- `Fan: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
240
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
241
- );
242
-
243
- // Make the API request
238
+ // Make Push On request to the API
244
239
  const t = Date.now();
245
240
  const nonce = 'requestID';
246
241
  const data = this.platform.config.credentials?.token + t + nonce;
247
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
242
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
243
+ .update(Buffer.from(data, 'utf-8'))
244
+ .digest();
248
245
  const sign = signTerm.toString('base64');
249
246
  this.debugLog(`Fan: ${this.accessory.displayName} sign: ${sign}`);
247
+ this.infoLog(`Fan: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
250
248
  const options = {
251
249
  hostname: HostDomain,
252
250
  port: 443,
253
251
  path: `${DevicePath}/${this.device.deviceId}/commands`,
254
252
  method: 'POST',
255
253
  headers: {
256
- Authorization: this.platform.config.credentials?.token,
257
- sign: sign,
258
- nonce: nonce,
259
- t: t,
254
+ 'Authorization': this.platform.config.credentials?.token,
255
+ 'sign': sign,
256
+ 'nonce': nonce,
257
+ 't': t,
260
258
  'Content-Type': 'application/json',
259
+ 'Content-Length': body.length,
261
260
  },
262
261
  };
263
-
264
- const req = https.request(options, (res) => {
262
+ const req = https.request(options, res => {
265
263
  this.debugLog(`Fan: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
266
264
  this.statusCode({ res });
267
- res.on('data', (d) => {
268
- this.debugLog(`an: ${this.accessory.displayName} d: ${d}`);
265
+ res.on('data', d => {
266
+ this.debugLog(`Fan: ${this.accessory.displayName} d: ${d}`);
269
267
  });
270
268
  });
271
-
272
- req.on('error', (error) => {
273
- this.errorLog(`Fan: ${this.accessory.displayName} error: ${error}`);
269
+ req.on('error', (e: any) => {
270
+ this.errorLog(`Fan: ${this.accessory.displayName} error message: ${e.message}`);
274
271
  });
275
-
276
- req.write(payload);
272
+ req.write(body);
277
273
  req.end();
278
-
279
274
  this.debugLog(`Fan: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
280
275
  this.updateHomeKitCharacteristics();
281
276
  } catch (e: any) {
@@ -101,87 +101,82 @@ export class Light {
101
101
  */
102
102
  async pushLightOnChanges(): Promise<void> {
103
103
  if (this.On) {
104
- const payload = {
105
- commandType: 'command',
106
- parameter: 'default',
107
- command: 'turnOn',
108
- };
109
- await this.pushChanges(payload);
104
+ const body = JSON.stringify({
105
+ 'command': 'turnOn',
106
+ 'parameter': 'default',
107
+ 'commandType': 'command',
108
+ });
109
+ await this.pushChanges(body);
110
110
  }
111
111
  }
112
112
 
113
113
  async pushLightOffChanges(): Promise<void> {
114
114
  if (!this.On) {
115
- const payload = {
116
- commandType: 'command',
117
- parameter: 'default',
118
- command: 'turnOff',
119
- };
120
- await this.pushChanges(payload);
115
+ const body = JSON.stringify({
116
+ 'command': 'turnOff',
117
+ 'parameter': 'default',
118
+ 'commandType': 'command',
119
+ });
120
+ await this.pushChanges(body);
121
121
  }
122
122
  }
123
123
 
124
124
  async pushLightBrightnessUpChanges(): Promise<void> {
125
- const payload = {
126
- commandType: 'command',
127
- parameter: 'default',
128
- command: 'brightnessUp',
129
- };
130
- await this.pushChanges(payload);
125
+ const body = JSON.stringify({
126
+ 'command': 'brightnessUp',
127
+ 'parameter': 'default',
128
+ 'commandType': 'command',
129
+ });
130
+ await this.pushChanges(body);
131
131
  }
132
132
 
133
133
  async pushLightBrightnessDownChanges(): Promise<void> {
134
- const payload = {
135
- commandType: 'command',
136
- parameter: 'default',
137
- command: 'brightnessDown',
138
- };
139
- await this.pushChanges(payload);
134
+ const body = JSON.stringify({
135
+ 'command': 'brightnessDown',
136
+ 'parameter': 'default',
137
+ 'commandType': 'command',
138
+ });
139
+ await this.pushChanges(body);
140
140
  }
141
141
 
142
- async pushChanges(payload): Promise<void> {
142
+ async pushChanges(body): Promise<void> {
143
143
  try {
144
- this.infoLog(
145
- `Light: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
146
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
147
- );
148
-
149
- // Make the API request
144
+ // Make Push On request to the API
150
145
  const t = Date.now();
151
146
  const nonce = 'requestID';
152
147
  const data = this.platform.config.credentials?.token + t + nonce;
153
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
148
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
149
+ .update(Buffer.from(data, 'utf-8'))
150
+ .digest();
154
151
  const sign = signTerm.toString('base64');
155
152
  this.debugLog(`Light: ${this.accessory.displayName} sign: ${sign}`);
153
+ this.infoLog(`Light: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
156
154
  const options = {
157
155
  hostname: HostDomain,
158
156
  port: 443,
159
157
  path: `${DevicePath}/${this.device.deviceId}/commands`,
160
158
  method: 'POST',
161
159
  headers: {
162
- Authorization: this.platform.config.credentials?.token,
163
- sign: sign,
164
- nonce: nonce,
165
- t: t,
160
+ 'Authorization': this.platform.config.credentials?.token,
161
+ 'sign': sign,
162
+ 'nonce': nonce,
163
+ 't': t,
166
164
  'Content-Type': 'application/json',
165
+ 'Content-Length': body.length,
167
166
  },
168
167
  };
169
-
170
- const req = https.request(options, (res) => {
168
+ const req = https.request(options, res => {
171
169
  this.debugLog(`Light: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
172
170
  this.statusCode({ res });
173
- res.on('data', (d) => {
171
+ res.on('data', d => {
174
172
  this.debugLog(`Light: ${this.accessory.displayName} d: ${d}`);
175
173
  });
176
174
  });
177
-
178
- req.on('error', (error) => {
179
- this.errorLog(`Light: ${this.accessory.displayName} error: ${error}`);
175
+ req.on('error', (e: any) => {
176
+ this.errorLog(`Light: ${this.accessory.displayName} error message: ${e.message}`);
180
177
  });
181
-
182
- req.write(payload);
178
+ req.write(body);
183
179
  req.end();
184
-
185
180
  this.debugLog(`Light: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
186
181
  this.OnCached = this.On;
187
182
  this.accessory.context.On = this.OnCached;