homebridge-melcloud-control 4.0.0-beta.259 → 4.0.0-beta.260

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.259",
4
+ "version": "4.0.0-beta.260",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -39,30 +39,11 @@ class MelCloudHomeToken {
39
39
  return { csrf, url: res.request.res.responseUrl };
40
40
  }
41
41
 
42
- async submitFormPost(actionUrl, code, state) {
43
- console.log('Submitting form_post to callback URL:', actionUrl);
44
- const form = new URLSearchParams({ code, state });
45
- const res = await this.client.post(actionUrl, form.toString(), {
46
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
47
- maxRedirects: 0,
48
- validateStatus: (status) => status >= 200 && status < 400,
49
- });
50
-
51
- const location = res.headers.location;
52
- if (location?.startsWith('melcloudhome://')) {
53
- const codeMatch = location.match(/[?&]code=([^&]+)/);
54
- if (codeMatch) {
55
- console.log('Authorization code obtained from form_post redirect:', codeMatch[1]);
56
- return codeMatch[1];
57
- }
58
- }
59
- throw new Error('No authorization code found in form_post response');
60
- }
61
-
62
42
  async submitLogin(loginUrl, csrf, email, password) {
63
43
  console.log('Submitting login credentials to:', loginUrl);
44
+
64
45
  const form = new URLSearchParams({ _csrf: csrf, username: email, password });
65
- const res = await this.client.post(loginUrl, form.toString(), {
46
+ let res = await this.client.post(loginUrl, form.toString(), {
66
47
  headers: {
67
48
  'Content-Type': 'application/x-www-form-urlencoded',
68
49
  Referer: loginUrl,
@@ -73,19 +54,19 @@ class MelCloudHomeToken {
73
54
 
74
55
  console.log('Login POST status:', res.status);
75
56
 
76
- // Spróbuj pobrać kod z nagłówka Location
77
- const location = res.headers.location;
78
- if (location?.startsWith('melcloudhome://')) {
79
- const codeMatch = location.match(/[?&]code=([^&]+)/);
80
- if (codeMatch) {
81
- console.log('Authorization code obtained from Location header:', codeMatch[1]);
82
- return codeMatch[1];
83
- }
57
+ // Obsługa 302 redirect
58
+ while (res.status === 302 && res.headers.location) {
59
+ const location = res.headers.location;
60
+ console.log('Following redirect:', location);
61
+ res = await this.client.get(location, {
62
+ maxRedirects: 0,
63
+ validateStatus: (status) => status >= 200 && status < 400,
64
+ });
84
65
  }
85
66
 
86
- // Spróbuj parsować HTML w poszukiwaniu form_post
87
- const $ = cheerio.load(res.data);
88
- const formElement = $('form');
67
+ // Sprawdź HTML na form_post
68
+ let $ = cheerio.load(res.data);
69
+ let formElement = $('form');
89
70
  if (formElement.length) {
90
71
  const action = formElement.attr('action');
91
72
  const code = $('input[name="code"]').attr('value');
@@ -96,7 +77,7 @@ class MelCloudHomeToken {
96
77
  }
97
78
  }
98
79
 
99
- // Spróbuj wyciągnąć kod z JS w ciele strony
80
+ // Spróbuj pobrać kod bezpośrednio z JS w body
100
81
  const bodyCodeMatch = res.data.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
101
82
  if (bodyCodeMatch) {
102
83
  console.log('Authorization code obtained from HTML/JS body:', bodyCodeMatch[1]);
@@ -106,6 +87,30 @@ class MelCloudHomeToken {
106
87
  throw new Error('Authorization code not found in login response');
107
88
  }
108
89
 
90
+ async submitFormPost(actionUrl, code, state) {
91
+ console.log('Submitting form_post to callback URL:', actionUrl);
92
+
93
+ const form = new URLSearchParams({ code, state });
94
+ const res = await this.client.post(actionUrl, form.toString(), {
95
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
96
+ maxRedirects: 0,
97
+ validateStatus: (status) => status >= 200 && status < 400,
98
+ });
99
+
100
+ // Sprawdź redirect z finalnym code
101
+ const location = res.headers.location;
102
+ if (location?.startsWith('melcloudhome://')) {
103
+ const codeMatch = location.match(/[?&]code=([^&]+)/);
104
+ if (codeMatch) {
105
+ console.log('Authorization code obtained from form_post redirect:', codeMatch[1]);
106
+ return codeMatch[1];
107
+ }
108
+ }
109
+
110
+ throw new Error('No authorization code found in form_post response');
111
+ }
112
+
113
+
109
114
  async exchangeCodeForTokens(code, codeVerifier) {
110
115
  console.log('Exchanging authorization code for tokens...');
111
116
  const form = new URLSearchParams({