homebridge-philips-airplus-cloud 0.5.2 → 0.5.3

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/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  The Home Assistant integration in `custom_components/` is unchanged from upstream and keeps its own history.
4
4
 
5
+ ## 0.5.3 — 2026-11-21
6
+
7
+ - Fix: logging in from the settings panel failed with "Request a new code first" when the page was reloaded between "Send code" and "Log in" — typically on a phone, after switching to the mail app to read the code, because Homebridge UI then restarts the panel. The pending request is now kept by the page: after a reload the panel reopens on the code field and the code can still be verified.
8
+ - Fix: the login no longer fails if the Philips API refuses to return the account's user id; it is only used to name the cloud connection and has a fallback.
9
+
5
10
  ## 0.5.2 — 2026-11-21
6
11
 
7
12
  - README: new "Filter notifications" section — how to turn on notifications for the filter alert sensors in the Home app, why a sensor that is already open does not notify, and how to name the sensors so "opened / closed" reads clearly. No code changes.
@@ -70,11 +70,37 @@
70
70
  homebridge.fixScrollHeight();
71
71
  }
72
72
 
73
+ // The pending code request (email + vToken) is kept in the page's storage,
74
+ // so the login can be finished even if the settings page is reloaded or
75
+ // the panel's server restarts while you read the email (common on phones).
76
+ const PENDING_KEY = 'philips-airplus-pending-login';
77
+ const PENDING_MAX_AGE = 15 * 60 * 1000;
78
+ let pending = null;
79
+ function savePending(p) {
80
+ pending = p;
81
+ try {
82
+ if (p) localStorage.setItem(PENDING_KEY, JSON.stringify(p));
83
+ else localStorage.removeItem(PENDING_KEY);
84
+ } catch {
85
+ // storage unavailable: keep it in memory only
86
+ }
87
+ }
88
+ function loadPending() {
89
+ try {
90
+ const p = JSON.parse(localStorage.getItem(PENDING_KEY) || 'null');
91
+ if (p && p.email && p.vToken && Date.now() - p.at < PENDING_MAX_AGE) return p;
92
+ } catch {
93
+ // ignore unreadable storage
94
+ }
95
+ return null;
96
+ }
97
+
73
98
  $('send-btn').addEventListener('click', async () => {
74
99
  const email = $('email').value.trim();
75
100
  homebridge.showSpinner();
76
101
  try {
77
- await homebridge.request('/send-code', { email });
102
+ const r = await homebridge.request('/send-code', { email });
103
+ savePending(r && r.vToken ? { email: r.email || email, vToken: r.vToken, at: Date.now() } : null);
78
104
  $('email-echo').textContent = email;
79
105
  showStep('code');
80
106
  $('code').focus();
@@ -89,9 +115,13 @@
89
115
  $('verify-btn').addEventListener('click', async () => {
90
116
  homebridge.showSpinner();
91
117
  try {
92
- const r = await homebridge.request('/verify-code', { code: $('code').value });
118
+ const body = { code: $('code').value };
119
+ if (pending) Object.assign(body, { email: pending.email, vToken: pending.vToken });
120
+ const r = await homebridge.request('/verify-code', body);
121
+ const loginEmail = (pending && pending.email) || $('email').value.trim();
122
+ savePending(null);
93
123
  Object.assign(configs[0], {
94
- email: $('email').value.trim(),
124
+ email: loginEmail,
95
125
  refreshToken: r.refreshToken,
96
126
  clientId: r.clientId,
97
127
  userId: r.userId,
@@ -109,7 +139,10 @@
109
139
  }
110
140
  });
111
141
 
112
- $('back-btn').addEventListener('click', () => showStep('email'));
142
+ $('back-btn').addEventListener('click', () => {
143
+ savePending(null);
144
+ showStep('email');
145
+ });
113
146
  $('email').addEventListener('keydown', (e) => e.key === 'Enter' && $('send-btn').click());
114
147
  $('code').addEventListener('keydown', (e) => e.key === 'Enter' && $('verify-btn').click());
115
148
 
@@ -119,6 +152,13 @@
119
152
  });
120
153
 
121
154
  showStatus();
155
+ pending = loadPending();
156
+ if (pending) {
157
+ // Back from the mail app: go straight to the code field.
158
+ $('email').value = pending.email;
159
+ $('email-echo').textContent = pending.email;
160
+ showStep('code');
161
+ }
122
162
  homebridge.showSchemaForm();
123
163
  })();
124
164
  </script>
@@ -21,10 +21,18 @@ const { LoginSession } = require('../src/cloud');
21
21
  } catch (e) {
22
22
  throw new RequestError(e.message, {});
23
23
  }
24
- return { sent: true };
24
+ // The page keeps email + vToken, so the code can still be verified if
25
+ // this server process is restarted in between (Homebridge UI restarts
26
+ // it when the settings page reloads, e.g. after switching to the mail
27
+ // app on a phone to read the code).
28
+ return { sent: true, email: this.session.email, vToken: this.session.vToken };
25
29
  });
26
30
 
27
- this.onRequest('/verify-code', async ({ code } = {}) => {
31
+ this.onRequest('/verify-code', async ({ code, email, vToken } = {}) => {
32
+ if (vToken && email && (!this.session || this.session.vToken !== vToken)) {
33
+ this.session = new LoginSession({ log: (m) => console.log(`[philips-airplus-ui] ${m}`) });
34
+ this.session.resume(email, vToken);
35
+ }
28
36
  if (!this.session) throw new RequestError('Request a new code first', {});
29
37
  try {
30
38
  await this.session.verifyCode(code);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-philips-airplus-cloud",
3
3
  "displayName": "Philips Air+",
4
- "version": "0.5.2",
4
+ "version": "0.5.3",
5
5
  "description": "Homebridge plugin for Philips Air+ / PureProtect air purifiers via the Philips (Versuni) cloud — the same MQTT protocol used by the Air+ app and Google Home.",
6
6
  "main": "src/index.js",
7
7
  "license": "MIT",
package/src/account.js CHANGED
@@ -111,7 +111,13 @@ class Account {
111
111
  async getUserId() {
112
112
  if (!this.userId) {
113
113
  await this.ensureToken();
114
- this.userId = await cloud.fetchUserId(this.accessToken, this.fetch);
114
+ try {
115
+ this.userId = await cloud.fetchUserId(this.accessToken, this.fetch);
116
+ } catch (e) {
117
+ // Only used to build a nicer MQTT client id; there is a fallback.
118
+ this.log.debug(`Could not read the user id: ${e.message}`);
119
+ return null;
120
+ }
115
121
  this._save();
116
122
  }
117
123
  return this.userId;
package/src/cloud.js CHANGED
@@ -107,6 +107,12 @@ class LoginSession {
107
107
  return true;
108
108
  }
109
109
 
110
+ /** Continue a login started by sendCode() in another process. */
111
+ resume(email, vToken) {
112
+ this.email = String(email).trim();
113
+ this.vToken = vToken;
114
+ }
115
+
110
116
  async verifyCode(code) {
111
117
  if (!this.vToken) throw new CloudError('Request a code first');
112
118
  const data = await this._postForm(C.GIGYA_OTP_LOGIN_ENDPOINT, {
@@ -225,7 +231,14 @@ class LoginSession {
225
231
  }
226
232
  }
227
233
 
228
- const userId = await fetchUserId(tokens.accessToken, this.fetch);
234
+ // The user id only makes the MQTT client id nicer; a failure here must
235
+ // not fail the whole login (the client id has a fallback without it).
236
+ let userId = null;
237
+ try {
238
+ userId = await fetchUserId(tokens.accessToken, this.fetch);
239
+ } catch (e) {
240
+ this.log(`Could not read the user id (${e.message}); continuing without it`);
241
+ }
229
242
  return {
230
243
  ...tokens,
231
244
  userId,