homebridge-melcloud-control 4.0.0-beta.298 → 4.0.0-beta.299

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.298",
4
+ "version": "4.0.0-beta.299",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/melcloud.js CHANGED
@@ -9,6 +9,7 @@ import Functions from './functions.js';
9
9
  import { ApiUrls, ApiUrlsHome } from './constants.js';
10
10
  import { URLSearchParams } from 'url';
11
11
  const TOKEN_ENDPOINT = 'https://auth.melcloudhome.com/connect/token';
12
+ const REDIRECT_URI = 'melcloudhome://';
12
13
 
13
14
  class MelCloud extends EventEmitter {
14
15
  constructor(accountType, user, passwd, language, accountFile, buildingsFile, devicesFile, logWarn, logDebug, requestConfig) {
@@ -304,16 +305,8 @@ class MelCloud extends EventEmitter {
304
305
  this.emit('warn', authCode);
305
306
 
306
307
  // 5. Wymień code na access_token
307
- const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({
308
- client_id: 'homemobile',
309
- grant_type: 'authorization_code',
310
- code: authCode,
311
- redirect_uri: 'melcloudhome://'
312
- }).toString(), {
313
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
314
- });
315
-
316
- const tokenData = await tokenResp.json();
308
+ const tokenResponse = await melCloudHomeToken.getTokens(authCode);
309
+ const tokenData = await tokenResponse.json();
317
310
  this.emit('debug', `Token: ${JSON.stringify(tokenData, null, 2)}`);
318
311
  return {
319
312
  accessToken: tokenData.access_token,
@@ -75,6 +75,29 @@ class MelCloudHomeToken {
75
75
  }
76
76
  }
77
77
 
78
+ async getTokens(code) {
79
+ const pkce = this.generatePKCE();
80
+ const data = new URLSearchParams();
81
+ data.append('grant_type', 'authorization_code');
82
+ data.append('client_id', CLIENT_ID);
83
+ data.append('code', code);
84
+ data.append('redirect_uri', REDIRECT_URI);
85
+ data.append('code_verifier', pkce.verifier);
86
+
87
+ const res = await this.client.post(TOKEN_ENDPOINT, data.toString(), {
88
+ headers: {
89
+ 'Content-Type': 'application/x-www-form-urlencoded',
90
+ 'Authorization': 'Basic aG9tZW1vYmlsZTo='
91
+ },
92
+ });
93
+
94
+ if (res.status !== 200) {
95
+ throw new Error(`Token exchange failed: ${res.status} ${res.statusText}`);
96
+ }
97
+
98
+ return res.data;
99
+ }
100
+
78
101
 
79
102
  async buildAuthorizeUrl() {
80
103
  try {
@@ -88,7 +111,6 @@ class MelCloudHomeToken {
88
111
  authUrl.searchParams.set('code_challenge', pkce.challenge);
89
112
  authUrl.searchParams.set('code_challenge_method', 'S256');
90
113
  authUrl.searchParams.set('state', state);
91
-
92
114
  const loginPage = await this.getLoginPage(authUrl.toString());
93
115
 
94
116
  return loginPage.url;