homebridge-envoy-solar-sensor 0.1.5 → 0.1.6

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 (2) hide show
  1. package/index.js +18 -11
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const { Agent } = require('undici');
3
+ const { Agent, request } = require('undici');
4
4
 
5
5
  const PLUGIN_NAME = 'homebridge-envoy-solar-sensor';
6
6
  const PLATFORM_NAME = 'EnvoySolarSensor';
@@ -203,10 +203,7 @@ class EnvoySolarPlatform {
203
203
  const candidate = eim || production || inverters || productionArray[0];
204
204
  const wNow = candidate && candidate.wNow;
205
205
 
206
- if (typeof wNow !== 'number') {
207
- throw new Error('production.json mist wNow in production items');
208
- }
209
-
206
+ if (typeof wNow !== 'number') throw new Error('production.json mist wNow in production items');
210
207
  return Math.max(0, wNow);
211
208
  }
212
209
 
@@ -232,13 +229,23 @@ class EnvoySolarPlatform {
232
229
  ? new Agent({ connect: { rejectUnauthorized: !allowInsecureTLS } })
233
230
  : undefined;
234
231
 
235
- const res = await fetch(url, { headers, dispatcher });
232
+ try {
233
+ const res = await request(url, { method: 'GET', headers, dispatcher });
236
234
 
237
- if (!res.ok) {
238
- const text = await res.text().catch(() => '');
239
- throw new Error(`HTTP ${res.status} op ${url} ${text}`);
240
- }
235
+ if (res.statusCode === 401 || res.statusCode === 403) {
236
+ throw new Error(`HTTP ${res.statusCode} unauthorized. Check token and that you pasted token only, without 'Bearer'.`);
237
+ }
241
238
 
242
- return await res.json();
239
+ if (res.statusCode < 200 || res.statusCode >= 300) {
240
+ const body = await res.body.text().catch(() => '');
241
+ throw new Error(`HTTP ${res.statusCode} op ${url} ${body}`);
242
+ }
243
+
244
+ const text = await res.body.text();
245
+ return JSON.parse(text);
246
+ } catch (e) {
247
+ const msg = e && e.message ? e.message : String(e);
248
+ throw new Error(`fetch failed: ${msg} url=${url} insecureTLS=${allowInsecureTLS}`);
249
+ }
243
250
  }
244
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-envoy-solar-sensor",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Expose Enphase Envoy solar production as a HomeKit sensor for daylight-based automations",
5
5
  "main": "index.js",
6
6
  "license": "MIT",