@switchbot/homebridge-switchbot 1.15.1-beta.8 → 2.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) 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.d.ts.map +1 -1
  12. package/dist/device/bot.js +33 -29
  13. package/dist/device/bot.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +1 -0
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +129 -97
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.js +1 -1
  19. package/dist/device/contact.js.map +1 -1
  20. package/dist/device/curtain.d.ts.map +1 -1
  21. package/dist/device/curtain.js +34 -26
  22. package/dist/device/curtain.js.map +1 -1
  23. package/dist/device/humidifier.d.ts.map +1 -1
  24. package/dist/device/humidifier.js +88 -92
  25. package/dist/device/humidifier.js.map +1 -1
  26. package/dist/device/lock.d.ts.map +1 -1
  27. package/dist/device/lock.js +29 -25
  28. package/dist/device/lock.js.map +1 -1
  29. package/dist/device/meter.js +1 -1
  30. package/dist/device/meter.js.map +1 -1
  31. package/dist/device/meterplus.js +1 -1
  32. package/dist/device/meterplus.js.map +1 -1
  33. package/dist/device/motion.js +1 -1
  34. package/dist/device/motion.js.map +1 -1
  35. package/dist/device/plug.d.ts.map +1 -1
  36. package/dist/device/plug.js +31 -27
  37. package/dist/device/plug.js.map +1 -1
  38. package/dist/device/striplight.d.ts.map +1 -1
  39. package/dist/device/striplight.js +81 -72
  40. package/dist/device/striplight.js.map +1 -1
  41. package/dist/irdevice/airconditioner.d.ts +2 -2
  42. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  43. package/dist/irdevice/airconditioner.js +35 -32
  44. package/dist/irdevice/airconditioner.js.map +1 -1
  45. package/dist/irdevice/airpurifier.d.ts +2 -2
  46. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  47. package/dist/irdevice/airpurifier.js +35 -32
  48. package/dist/irdevice/airpurifier.js.map +1 -1
  49. package/dist/irdevice/camera.d.ts +2 -2
  50. package/dist/irdevice/camera.d.ts.map +1 -1
  51. package/dist/irdevice/camera.js +28 -26
  52. package/dist/irdevice/camera.js.map +1 -1
  53. package/dist/irdevice/fan.d.ts +2 -2
  54. package/dist/irdevice/fan.d.ts.map +1 -1
  55. package/dist/irdevice/fan.js +47 -45
  56. package/dist/irdevice/fan.js.map +1 -1
  57. package/dist/irdevice/light.d.ts +2 -2
  58. package/dist/irdevice/light.d.ts.map +1 -1
  59. package/dist/irdevice/light.js +40 -38
  60. package/dist/irdevice/light.js.map +1 -1
  61. package/dist/irdevice/other.d.ts +2 -2
  62. package/dist/irdevice/other.d.ts.map +1 -1
  63. package/dist/irdevice/other.js +28 -26
  64. package/dist/irdevice/other.js.map +1 -1
  65. package/dist/irdevice/tv.d.ts +2 -2
  66. package/dist/irdevice/tv.d.ts.map +1 -1
  67. package/dist/irdevice/tv.js +82 -80
  68. package/dist/irdevice/tv.js.map +1 -1
  69. package/dist/irdevice/vacuumcleaner.d.ts +2 -2
  70. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  71. package/dist/irdevice/vacuumcleaner.js +24 -22
  72. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  73. package/dist/irdevice/waterheater.d.ts +2 -2
  74. package/dist/irdevice/waterheater.d.ts.map +1 -1
  75. package/dist/irdevice/waterheater.js +28 -26
  76. package/dist/irdevice/waterheater.js.map +1 -1
  77. package/dist/settings.d.ts +0 -5
  78. package/dist/settings.d.ts.map +1 -1
  79. package/dist/settings.js.map +1 -1
  80. package/package.json +79 -79
  81. package/src/device/bot.ts +34 -39
  82. package/src/device/colorbulb.ts +130 -119
  83. package/src/device/contact.ts +1 -1
  84. package/src/device/curtain.ts +35 -35
  85. package/src/device/humidifier.ts +89 -118
  86. package/src/device/lock.ts +29 -34
  87. package/src/device/meter.ts +1 -1
  88. package/src/device/meterplus.ts +1 -1
  89. package/src/device/motion.ts +1 -1
  90. package/src/device/plug.ts +32 -36
  91. package/src/device/striplight.ts +82 -98
  92. package/src/irdevice/airconditioner.ts +36 -41
  93. package/src/irdevice/airpurifier.ts +36 -43
  94. package/src/irdevice/camera.ts +29 -34
  95. package/src/irdevice/fan.ts +48 -53
  96. package/src/irdevice/light.ts +41 -46
  97. package/src/irdevice/other.ts +29 -34
  98. package/src/irdevice/tv.ts +83 -88
  99. package/src/irdevice/vacuumcleaner.ts +25 -30
  100. package/src/irdevice/waterheater.ts +29 -34
  101. package/src/settings.ts +0 -19
  102. package/.github/workflows/nodejs-beta.yml +0 -42
  103. package/.github/workflows/nodejs.yml +0 -47
  104. package/.github/workflows/prerelease-beta.js +0 -34
@@ -3,7 +3,7 @@ import crypto from 'crypto';
3
3
  import { IncomingMessage } from 'http';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
6
- import { irdevice, irDevicesConfig, payload, HostDomain, DevicePath } from '../settings';
6
+ import { irdevice, irDevicesConfig, HostDomain, DevicePath } from '../settings';
7
7
 
8
8
  /**
9
9
  * Platform Accessory
@@ -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
- } as payload;
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
- } as payload;
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
- } as payload;
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
- } as payload;
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
- } as payload;
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: 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) {
@@ -2,7 +2,7 @@ import https from 'https';
2
2
  import crypto from 'crypto';
3
3
  import { IncomingMessage } from 'http';
4
4
  import { SwitchBotPlatform } from '../platform';
5
- import { irDevicesConfig, irdevice, payload, HostDomain, DevicePath } from '../settings';
5
+ import { irDevicesConfig, irdevice, HostDomain, DevicePath } from '../settings';
6
6
  import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
7
7
 
8
8
  /**
@@ -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
- } as payload;
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
- } as payload;
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
- } as payload;
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
- } as payload;
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: 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;
@@ -2,7 +2,7 @@ import https from 'https';
2
2
  import crypto from 'crypto';
3
3
  import { IncomingMessage } from 'http';
4
4
  import { SwitchBotPlatform } from '../platform';
5
- import { irDevicesConfig, irdevice, payload, HostDomain, DevicePath } from '../settings';
5
+ import { irDevicesConfig, irdevice, HostDomain, DevicePath } from '../settings';
6
6
  import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
7
7
 
8
8
  /**
@@ -99,12 +99,12 @@ export class Others {
99
99
  if (this.device.other) {
100
100
  if (this.device.other.commandOn) {
101
101
  if (!this.Active) {
102
- const payload = {
103
- commandType: 'customize',
104
- parameter: 'default',
105
- command: `${this.device.other.commandOn}`,
106
- } as payload;
107
- await this.pushChanges(payload);
102
+ const body = JSON.stringify({
103
+ 'command': `${this.device.other.commandOn}`,
104
+ 'parameter': 'default',
105
+ 'commandType': 'customize',
106
+ });
107
+ await this.pushChanges(body);
108
108
  }
109
109
  } else {
110
110
  this.errorLog(`Other: ${this.accessory.displayName} On Command not set, commandOn: ${this.device.other.commandOn}`);
@@ -122,12 +122,12 @@ export class Others {
122
122
  if (this.device.other) {
123
123
  if (this.device.other.commandOff) {
124
124
  if (this.Active) {
125
- const payload = {
126
- commandType: 'customize',
127
- parameter: 'default',
128
- command: `${this.device.other.commandOff}`,
129
- } as payload;
130
- await this.pushChanges(payload);
125
+ const body = JSON.stringify({
126
+ 'command': `${this.device.other.commandOff}`,
127
+ 'parameter': 'default',
128
+ 'commandType': 'customize',
129
+ });
130
+ await this.pushChanges(body);
131
131
  }
132
132
  } else {
133
133
  this.errorLog(`Other: ${this.accessory.displayName} Off Command not set, commandOff: ${this.device.other.commandOff}`);
@@ -140,49 +140,44 @@ export class Others {
140
140
  }
141
141
  }
142
142
 
143
- async pushChanges(payload: payload): Promise<void> {
143
+ async pushChanges(body): Promise<void> {
144
144
  try {
145
- this.infoLog(
146
- `Other: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
147
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
148
- );
149
-
150
- // Make the API request
145
+ // Make Push On request to the API
151
146
  const t = Date.now();
152
147
  const nonce = 'requestID';
153
148
  const data = this.platform.config.credentials?.token + t + nonce;
154
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
149
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
150
+ .update(Buffer.from(data, 'utf-8'))
151
+ .digest();
155
152
  const sign = signTerm.toString('base64');
156
153
  this.debugLog(`Other: ${this.accessory.displayName} sign: ${sign}`);
154
+ this.infoLog(`Other: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
157
155
  const options = {
158
156
  hostname: HostDomain,
159
157
  port: 443,
160
158
  path: `${DevicePath}/${this.device.deviceId}/commands`,
161
159
  method: 'POST',
162
160
  headers: {
163
- Authorization: this.platform.config.credentials?.token,
164
- sign: sign,
165
- nonce: nonce,
166
- t: t,
161
+ 'Authorization': this.platform.config.credentials?.token,
162
+ 'sign': sign,
163
+ 'nonce': nonce,
164
+ 't': t,
167
165
  'Content-Type': 'application/json',
166
+ 'Content-Length': body.length,
168
167
  },
169
168
  };
170
-
171
- const req = https.request(options, (res) => {
169
+ const req = https.request(options, res => {
172
170
  this.debugLog(`Other: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
173
171
  this.statusCode({ res });
174
- res.on('data', (d) => {
172
+ res.on('data', d => {
175
173
  this.debugLog(`Other: ${this.accessory.displayName} d: ${d}`);
176
174
  });
177
175
  });
178
-
179
- req.on('error', (error) => {
180
- this.errorLog(`Other: ${this.accessory.displayName} error: ${error}`);
176
+ req.on('error', (e: any) => {
177
+ this.errorLog(`Other: ${this.accessory.displayName} error message: ${e.message}`);
181
178
  });
182
-
183
- req.write(payload);
179
+ req.write(body);
184
180
  req.end();
185
-
186
181
  this.debugLog(`Other: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
187
182
  this.updateHomeKitCharacteristics();
188
183
  } catch (e: any) {
@@ -3,7 +3,7 @@ import crypto from 'crypto';
3
3
  import { IncomingMessage } from 'http';
4
4
  import { SwitchBotPlatform } from '../platform';
5
5
  import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
6
- import { irdevice, irDevicesConfig, payload, HostDomain, DevicePath } from '../settings';
6
+ import { irdevice, irDevicesConfig, HostDomain, DevicePath } from '../settings';
7
7
 
8
8
  /**
9
9
  * Platform Accessory
@@ -237,148 +237,143 @@ export class TV {
237
237
  */
238
238
  async pushTvOnChanges(): Promise<void> {
239
239
  if (this.Active !== 1) {
240
- const payload = {
241
- commandType: 'command',
242
- parameter: 'default',
243
- command: 'turnOn',
244
- } as payload;
245
- await this.pushTVChanges(payload);
240
+ const body = JSON.stringify({
241
+ 'command': 'turnOn',
242
+ 'parameter': 'default',
243
+ 'commandType': 'command',
244
+ });
245
+ await this.pushTVChanges(body);
246
246
  }
247
247
  }
248
248
 
249
249
  async pushTvOffChanges(): Promise<void> {
250
- const payload = {
251
- commandType: 'command',
252
- parameter: 'default',
253
- command: 'turnOff',
254
- } as payload;
255
- await this.pushTVChanges(payload);
250
+ const body = JSON.stringify({
251
+ 'command': 'turnOff',
252
+ 'parameter': 'default',
253
+ 'commandType': 'command',
254
+ });
255
+ await this.pushTVChanges(body);
256
256
  }
257
257
 
258
258
  async pushOkChanges(): Promise<void> {
259
- const payload = {
260
- commandType: 'command',
261
- parameter: 'default',
262
- command: 'Ok',
263
- } as payload;
264
- await this.pushTVChanges(payload);
259
+ const body = JSON.stringify({
260
+ 'command': 'Ok',
261
+ 'parameter': 'default',
262
+ 'commandType': 'command',
263
+ });
264
+ await this.pushTVChanges(body);
265
265
  }
266
266
 
267
267
  async pushBackChanges(): Promise<void> {
268
- const payload = {
269
- commandType: 'command',
270
- parameter: 'default',
271
- command: 'Back',
272
- } as payload;
273
- await this.pushTVChanges(payload);
268
+ const body = JSON.stringify({
269
+ 'command': 'Back',
270
+ 'parameter': 'default',
271
+ 'commandType': 'command',
272
+ });
273
+ await this.pushTVChanges(body);
274
274
  }
275
275
 
276
276
  async pushMenuChanges(): Promise<void> {
277
- const payload = {
278
- commandType: 'command',
279
- parameter: 'default',
280
- command: 'Menu',
281
- } as payload;
282
- await this.pushTVChanges(payload);
277
+ const body = JSON.stringify({
278
+ 'command': 'Menu',
279
+ 'parameter': 'default',
280
+ 'commandType': 'command',
281
+ });
282
+ await this.pushTVChanges(body);
283
283
  }
284
284
 
285
285
  async pushUpChanges(): Promise<void> {
286
- const payload = {
287
- commandType: 'command',
288
- parameter: 'default',
289
- command: 'Up',
290
- } as payload;
291
- await this.pushTVChanges(payload);
286
+ const body = JSON.stringify({
287
+ 'command': 'Up',
288
+ 'parameter': 'default',
289
+ 'commandType': 'command',
290
+ });
291
+ await this.pushTVChanges(body);
292
292
  }
293
293
 
294
294
  async pushDownChanges(): Promise<void> {
295
- const payload = {
296
- commandType: 'command',
297
- parameter: 'default',
298
- command: 'Down',
299
- } as payload;
300
- await this.pushTVChanges(payload);
295
+ const body = JSON.stringify({
296
+ 'command': 'Down',
297
+ 'parameter': 'default',
298
+ 'commandType': 'command',
299
+ });
300
+ await this.pushTVChanges(body);
301
301
  }
302
302
 
303
303
  async pushRightChanges(): Promise<void> {
304
- const payload = {
305
- commandType: 'command',
306
- parameter: 'default',
307
- command: 'Right',
308
- } as payload;
309
- await this.pushTVChanges(payload);
304
+ const body = JSON.stringify({
305
+ 'command': 'Right',
306
+ 'parameter': 'default',
307
+ 'commandType': 'command',
308
+ });
309
+ await this.pushTVChanges(body);
310
310
  }
311
311
 
312
312
  async pushLeftChanges(): Promise<void> {
313
- const payload = {
314
- commandType: 'command',
315
- parameter: 'default',
316
- command: 'Left',
317
- } as payload;
318
- await this.pushTVChanges(payload);
313
+ const body = JSON.stringify({
314
+ 'command': 'Left',
315
+ 'parameter': 'default',
316
+ 'commandType': 'command',
317
+ });
318
+ await this.pushTVChanges(body);
319
319
  }
320
320
 
321
321
  async pushVolumeUpChanges(): Promise<void> {
322
- const payload = {
323
- commandType: 'command',
324
- parameter: 'default',
325
- command: 'volumeAdd',
326
- } as payload;
327
- await this.pushTVChanges(payload);
322
+ const body = JSON.stringify({
323
+ 'command': 'volumeAdd',
324
+ 'parameter': 'default',
325
+ 'commandType': 'command',
326
+ });
327
+ await this.pushTVChanges(body);
328
328
  }
329
329
 
330
330
  async pushVolumeDownChanges(): Promise<void> {
331
- const payload = {
332
- commandType: 'command',
333
- parameter: 'default',
334
- command: 'volumeSub',
335
- } as payload;
336
- await this.pushTVChanges(payload);
331
+ const body = JSON.stringify({
332
+ 'command': 'volumeSub',
333
+ 'parameter': 'default',
334
+ 'commandType': 'command',
335
+ });
336
+ await this.pushTVChanges(body);
337
337
  }
338
338
 
339
- async pushTVChanges(payload: payload): Promise<void> {
339
+ async pushTVChanges(body): Promise<void> {
340
340
  try {
341
- this.infoLog(
342
- `${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. command: ${payload.command},` +
343
- ` parameter: ${payload.parameter}, commandType: ${payload.commandType}`,
344
- );
345
-
346
- // Make the API request
341
+ // Make Push On request to the API
347
342
  const t = Date.now();
348
343
  const nonce = 'requestID';
349
344
  const data = this.platform.config.credentials?.token + t + nonce;
350
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
345
+ const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
346
+ .update(Buffer.from(data, 'utf-8'))
347
+ .digest();
351
348
  const sign = signTerm.toString('base64');
352
349
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} sign: ${sign}`);
350
+ this.infoLog(`${this.device.remoteType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
353
351
  const options = {
354
352
  hostname: HostDomain,
355
353
  port: 443,
356
354
  path: `${DevicePath}/${this.device.deviceId}/commands`,
357
355
  method: 'POST',
358
356
  headers: {
359
- Authorization: this.platform.config.credentials?.token,
360
- sign: sign,
361
- nonce: nonce,
362
- t: t,
357
+ 'Authorization': this.platform.config.credentials?.token,
358
+ 'sign': sign,
359
+ 'nonce': nonce,
360
+ 't': t,
363
361
  'Content-Type': 'application/json',
362
+ 'Content-Length': body.length,
364
363
  },
365
364
  };
366
-
367
- const req = https.request(options, (res) => {
365
+ const req = https.request(options, res => {
368
366
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
369
367
  this.statusCode({ res });
370
- res.on('data', (d) => {
368
+ res.on('data', d => {
371
369
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} d: ${d}`);
372
370
  });
373
371
  });
374
-
375
- req.on('error', (error) => {
376
- this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} error: ${error}`);
372
+ req.on('error', (e: any) => {
373
+ this.errorLog(`${this.device.remoteType}: ${this.accessory.displayName} error message: ${e.message}`);
377
374
  });
378
-
379
- req.write(payload);
375
+ req.write(body);
380
376
  req.end();
381
-
382
377
  this.debugLog(`${this.device.remoteType}: ${this.accessory.displayName} pushchanges: ${JSON.stringify(req)}`);
383
378
  this.updateHomeKitCharacteristics();
384
379
  } catch (e: any) {