homebridge-envoy-solar-sensor 0.1.5 → 0.1.7

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.
@@ -74,6 +74,20 @@
74
74
  "type": "string",
75
75
  "default": "",
76
76
  "description": "Bearer token from Enphase Entrez, paste token only without the word Bearer"
77
+ },
78
+ "debugLogging": {
79
+ "title": "Debug Logging",
80
+ "type": "boolean",
81
+ "default": false,
82
+ "description": "Log production watts on every poll"
83
+ },
84
+ "debugBurstCount": {
85
+ "title": "Debug Burst Count",
86
+ "type": "integer",
87
+ "default": 0,
88
+ "minimum": 0,
89
+ "maximum": 200,
90
+ "description": "If set, logs the next N polls at INFO level for easier debugging"
77
91
  }
78
92
  }
79
93
  }
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';
@@ -83,6 +83,7 @@ class EnvoySolarPlatform {
83
83
  const tick = async () => {
84
84
  try {
85
85
  const watts = await this.readProductionWatts();
86
+ this.logWatts(watts);
86
87
  this.updateState(watts);
87
88
  } catch (err) {
88
89
  const msg = err && err.message ? err.message : String(err);
@@ -124,6 +125,38 @@ class EnvoySolarPlatform {
124
125
  return Boolean(this.config.allowInsecureTLS);
125
126
  }
126
127
 
128
+ isDebugLoggingEnabled() {
129
+ return Boolean(this.config.debugLogging);
130
+ }
131
+
132
+ getDebugBurstCount() {
133
+ const n = Number(this.config.debugBurstCount ?? 0);
134
+ if (!Number.isFinite(n) || n < 0) return 0;
135
+ return Math.floor(n);
136
+ }
137
+
138
+ consumeDebugBurst() {
139
+ const n = this.getDebugBurstCount();
140
+ if (n <= 0) return 0;
141
+ this.config.debugBurstCount = n - 1;
142
+ return n;
143
+ }
144
+
145
+ logWatts(productionWatts) {
146
+ const burst = this.getDebugBurstCount();
147
+ const debugEnabled = this.isDebugLoggingEnabled();
148
+
149
+ if (burst > 0) {
150
+ const before = this.consumeDebugBurst();
151
+ this.log.info(`Envoy production: ${productionWatts} W (debug burst remaining: ${before - 1})`);
152
+ return;
153
+ }
154
+
155
+ if (debugEnabled) {
156
+ this.log.debug(`Envoy production: ${productionWatts} W`);
157
+ }
158
+ }
159
+
127
160
  markFault() {
128
161
  if (!this.accessory) return;
129
162
  const service = this.accessory.getService(this.Service.ContactSensor);
@@ -203,10 +236,7 @@ class EnvoySolarPlatform {
203
236
  const candidate = eim || production || inverters || productionArray[0];
204
237
  const wNow = candidate && candidate.wNow;
205
238
 
206
- if (typeof wNow !== 'number') {
207
- throw new Error('production.json mist wNow in production items');
208
- }
209
-
239
+ if (typeof wNow !== 'number') throw new Error('production.json mist wNow in production items');
210
240
  return Math.max(0, wNow);
211
241
  }
212
242
 
@@ -232,13 +262,23 @@ class EnvoySolarPlatform {
232
262
  ? new Agent({ connect: { rejectUnauthorized: !allowInsecureTLS } })
233
263
  : undefined;
234
264
 
235
- const res = await fetch(url, { headers, dispatcher });
265
+ try {
266
+ const res = await request(url, { method: 'GET', headers, dispatcher });
236
267
 
237
- if (!res.ok) {
238
- const text = await res.text().catch(() => '');
239
- throw new Error(`HTTP ${res.status} op ${url} ${text}`);
240
- }
268
+ if (res.statusCode === 401 || res.statusCode === 403) {
269
+ throw new Error(`HTTP ${res.statusCode} unauthorized. Check token and that you pasted token only, without 'Bearer'.`);
270
+ }
241
271
 
242
- return await res.json();
272
+ if (res.statusCode < 200 || res.statusCode >= 300) {
273
+ const body = await res.body.text().catch(() => '');
274
+ throw new Error(`HTTP ${res.statusCode} op ${url} ${body}`);
275
+ }
276
+
277
+ const text = await res.body.text();
278
+ return JSON.parse(text);
279
+ } catch (e) {
280
+ const msg = e && e.message ? e.message : String(e);
281
+ throw new Error(`fetch failed: ${msg} url=${url} insecureTLS=${allowInsecureTLS}`);
282
+ }
243
283
  }
244
284
  }
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.7",
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",