node-red-contrib-octopus-intelligent 1.5.0 → 1.7.0
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 +597 -442
- package/README.md +1234 -980
- package/examples/basic-monitoring.json +1 -1
- package/examples/boost-on-cheap-or-surplus.json +171 -0
- package/examples/charging-and-error-alerts.json +95 -0
- package/examples/cheapest-window-planner.json +165 -0
- package/examples/daily-charging-summary.json +165 -0
- package/examples/ha-dashboard.yaml +667 -599
- package/examples/mqtt-integration.json +1 -1
- package/examples/node-red-dashboard-flow.json +1377 -1199
- package/examples/set-preferences.json +1 -1
- package/lib/active-now.js +33 -0
- package/lib/api-metrics.js +31 -31
- package/lib/auth.js +25 -20
- package/lib/boost.js +85 -0
- package/lib/categories/account.js +32 -32
- package/lib/categories/applicable-rates.js +137 -127
- package/lib/categories/completed-dispatches.js +36 -35
- package/lib/categories/electricity.js +156 -146
- package/lib/categories/flex-planned-dispatches.js +36 -36
- package/lib/categories/free-electricity.js +73 -61
- package/lib/categories/gas.js +102 -95
- package/lib/categories/home-mini.js +45 -37
- package/lib/categories/intelligent.js +221 -124
- package/lib/categories/octoplus.js +35 -32
- package/lib/categories/saving-sessions.js +78 -64
- package/lib/categories/smartflex-inverter.js +95 -0
- package/lib/categories/smartflex-vehicle.js +198 -0
- package/lib/categories/wheel-of-fortune.js +50 -49
- package/lib/commands.js +69 -0
- package/lib/discovery-retry.js +16 -0
- package/lib/discovery.js +154 -85
- package/lib/graphql-errors.js +174 -0
- package/lib/graphql.js +141 -44
- package/lib/http-get-json.js +80 -22
- package/lib/payload.js +104 -88
- package/lib/poll-retry.js +71 -0
- package/lib/poll-tick.js +112 -44
- package/lib/preferences.js +34 -0
- package/lib/scheduler.js +85 -47
- package/lib/sensor-definitions.js +269 -0
- package/lib/timezone.js +67 -49
- package/lib/update-check.js +38 -38
- package/lib/values.js +47 -0
- package/octopus-intelligent.html +258 -205
- package/octopus-intelligent.js +2816 -1807
- package/package.json +54 -54
package/lib/graphql.js
CHANGED
|
@@ -1,44 +1,141 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const https = require('https');
|
|
3
|
-
const { version } = require('../package.json');
|
|
4
|
-
|
|
5
|
-
// User-Agent is REQUIRED for api.backend.octopus.energy — the default Node.js UA
|
|
6
|
-
// is blocked at the edge (403). Sending a real UA on every request keeps both
|
|
7
|
-
// the main and backend endpoints reachable from a single transport.
|
|
8
|
-
const USER_AGENT = `node-red-contrib-octopus-intelligent/${version}`;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
const https = require('https');
|
|
3
|
+
const { version } = require('../package.json');
|
|
4
|
+
|
|
5
|
+
// User-Agent is REQUIRED for api.backend.octopus.energy — the default Node.js UA
|
|
6
|
+
// is blocked at the edge (403). Sending a real UA on every request keeps both
|
|
7
|
+
// the main and backend endpoints reachable from a single transport.
|
|
8
|
+
const USER_AGENT = `node-red-contrib-octopus-intelligent/${version}`;
|
|
9
|
+
|
|
10
|
+
// A request with no answer must fail, not hang: a hung poll blocks every later poll.
|
|
11
|
+
// The socket timeout below is an idle timeout; REQUEST_TIMEOUT_MS is also the most a
|
|
12
|
+
// whole request may take, so a response that trickles in is cut off too.
|
|
13
|
+
const REQUEST_TIMEOUT_MS = 30000;
|
|
14
|
+
|
|
15
|
+
// The deadline bounds time, not size. Kraken's real answers are tens of kilobytes; a body
|
|
16
|
+
// past this (an error page served in a loop, a misrouted proxy) is cut off and fails.
|
|
17
|
+
const MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
|
|
18
|
+
|
|
19
|
+
// The start of a body that isn't JSON (e.g. an HTML error page), on one line. Only the first
|
|
20
|
+
// kilobyte is looked at, however long the body is.
|
|
21
|
+
function snippet(raw) {
|
|
22
|
+
return String(raw).slice(0, 1024).replace(/\s+/g, ' ').trim().slice(0, 80);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Retry-After (RFC 9110): delay seconds or an HTTP date. In ms, or null when absent or unreadable.
|
|
26
|
+
function retryAfterMs(value, nowMs = Date.now()) {
|
|
27
|
+
if (value === undefined || value === null) return null;
|
|
28
|
+
const s = String(value).trim();
|
|
29
|
+
if (/^\d+$/.test(s)) return Number(s) * 1000;
|
|
30
|
+
const at = Date.parse(s);
|
|
31
|
+
return Number.isFinite(at) ? Math.max(0, at - nowMs) : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// An HTTP error reply. A rate limit (429) or outage (503) can say when to try again:
|
|
35
|
+
// err.retryAfterMs carries it for the caller's back-off.
|
|
36
|
+
function httpError(message, status, headers) {
|
|
37
|
+
const err = new Error(message);
|
|
38
|
+
err.status = status;
|
|
39
|
+
const wait = retryAfterMs(headers && headers['retry-after']);
|
|
40
|
+
if (wait !== null) err.retryAfterMs = wait;
|
|
41
|
+
return err;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isPlainObject(v) {
|
|
45
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function graphqlPost(body, token, hostname = 'api.octopus.energy') {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
// Settle exactly once: error, close, timeout and end can all arrive for one request.
|
|
51
|
+
let settled = false;
|
|
52
|
+
let deadline = null;
|
|
53
|
+
let req = null;
|
|
54
|
+
const settle = (fn, value) => {
|
|
55
|
+
if (settled) return;
|
|
56
|
+
settled = true;
|
|
57
|
+
if (deadline) clearTimeout(deadline);
|
|
58
|
+
fn(value);
|
|
59
|
+
};
|
|
60
|
+
const fail = (err) => settle(reject, err);
|
|
61
|
+
const timedOut = () => {
|
|
62
|
+
if (settled) return;
|
|
63
|
+
if (req && typeof req.destroy === 'function') req.destroy();
|
|
64
|
+
fail(new Error(`Octopus API request timed out after ${REQUEST_TIMEOUT_MS / 1000}s`));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const payload = JSON.stringify(body);
|
|
68
|
+
const headers = {
|
|
69
|
+
'Content-Type': 'application/json',
|
|
70
|
+
'Content-Length': Buffer.byteLength(payload),
|
|
71
|
+
'User-Agent': USER_AGENT
|
|
72
|
+
};
|
|
73
|
+
// Kraken expects the raw JWT. Both endpoints accept this. Adding a
|
|
74
|
+
// "JWT " prefix passes main-endpoint validation but fails sub-resolver
|
|
75
|
+
// auth on api.backend.octopus.energy (verified live 2026-05-11).
|
|
76
|
+
if (token) headers['Authorization'] = token;
|
|
77
|
+
let gotResponse = false;
|
|
78
|
+
// Set before the request: a mocked or very fast response can settle synchronously.
|
|
79
|
+
deadline = setTimeout(timedOut, REQUEST_TIMEOUT_MS);
|
|
80
|
+
req = https.request({
|
|
81
|
+
hostname,
|
|
82
|
+
path: '/v1/graphql/',
|
|
83
|
+
method: 'POST',
|
|
84
|
+
headers,
|
|
85
|
+
timeout: REQUEST_TIMEOUT_MS // also covers DNS and connecting
|
|
86
|
+
}, (res) => {
|
|
87
|
+
gotResponse = true;
|
|
88
|
+
let raw = '';
|
|
89
|
+
let bytes = 0;
|
|
90
|
+
let ended = false;
|
|
91
|
+
res.on('data', (chunk) => {
|
|
92
|
+
if (settled) return;
|
|
93
|
+
bytes += chunk.length;
|
|
94
|
+
if (bytes > MAX_RESPONSE_BYTES) {
|
|
95
|
+
if (req && typeof req.destroy === 'function') req.destroy();
|
|
96
|
+
fail(new Error('Octopus API response too large'));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
raw += chunk;
|
|
100
|
+
});
|
|
101
|
+
res.on('end', () => {
|
|
102
|
+
ended = true;
|
|
103
|
+
if (settled) return;
|
|
104
|
+
const status = res.statusCode;
|
|
105
|
+
// A gateway, rate-limit or outage reply: say so ("HTTP 503"), unless it carries
|
|
106
|
+
// GraphQL errors, which callers read for the Kraken error code.
|
|
107
|
+
const notOk = typeof status === 'number' && (status < 200 || status >= 300);
|
|
108
|
+
let data;
|
|
109
|
+
try {
|
|
110
|
+
data = JSON.parse(raw);
|
|
111
|
+
} catch (e) {
|
|
112
|
+
if (notOk) return fail(httpError(`HTTP ${status}: ${snippet(raw)}`, status, res.headers));
|
|
113
|
+
return fail(new Error('Failed to parse API response: ' + e.message));
|
|
114
|
+
}
|
|
115
|
+
if (notOk && !(data && Array.isArray(data.errors))) return fail(httpError(`HTTP ${status}`, status, res.headers));
|
|
116
|
+
// Callers read data.errors and data.data: a body of null, a string or a list is
|
|
117
|
+
// not a GraphQL answer and would only fail later with a TypeError.
|
|
118
|
+
if (!isPlainObject(data)) return fail(new Error(`Unexpected response shape: ${snippet(raw)}`));
|
|
119
|
+
settle(resolve, { data, status, headers: res.headers });
|
|
120
|
+
});
|
|
121
|
+
// A connection closed part way through the body gives no 'end' and no 'error'.
|
|
122
|
+
const closedEarly = () => {
|
|
123
|
+
if (!ended && !res.complete) fail(new Error('Octopus API connection closed before the response completed'));
|
|
124
|
+
};
|
|
125
|
+
res.on('aborted', closedEarly);
|
|
126
|
+
res.on('close', closedEarly);
|
|
127
|
+
});
|
|
128
|
+
req.on('error', fail);
|
|
129
|
+
// Once a response has started, its own close handling above decides.
|
|
130
|
+
req.on('close', () => {
|
|
131
|
+
if (!gotResponse) fail(new Error('Octopus API connection closed before a response arrived'));
|
|
132
|
+
});
|
|
133
|
+
if (typeof req.setTimeout === 'function') {
|
|
134
|
+
req.setTimeout(REQUEST_TIMEOUT_MS, timedOut);
|
|
135
|
+
}
|
|
136
|
+
req.write(payload);
|
|
137
|
+
req.end();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = { graphqlPost, snippet, retryAfterMs, REQUEST_TIMEOUT_MS, MAX_RESPONSE_BYTES };
|
package/lib/http-get-json.js
CHANGED
|
@@ -1,22 +1,80 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const https = require('https');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
const https = require('https');
|
|
3
|
+
|
|
4
|
+
// Idle timeout for the socket, and also the most a whole request may take.
|
|
5
|
+
const REQUEST_TIMEOUT_MS = 30000;
|
|
6
|
+
// The npm registry answer is a few kilobytes; a body past this is cut off and fails.
|
|
7
|
+
const MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
function httpGetJson(url) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
// Settle exactly once: error, close, timeout and end can all arrive for one request.
|
|
12
|
+
let settled = false;
|
|
13
|
+
let deadline = null;
|
|
14
|
+
let req = null;
|
|
15
|
+
let gotResponse = false;
|
|
16
|
+
const settle = (fn, value) => {
|
|
17
|
+
if (settled) return;
|
|
18
|
+
settled = true;
|
|
19
|
+
if (deadline) clearTimeout(deadline);
|
|
20
|
+
fn(value);
|
|
21
|
+
};
|
|
22
|
+
const fail = (err) => settle(reject, err);
|
|
23
|
+
const timedOut = () => {
|
|
24
|
+
if (settled) return;
|
|
25
|
+
if (req && typeof req.destroy === 'function') req.destroy();
|
|
26
|
+
fail(new Error(`Request timed out after ${REQUEST_TIMEOUT_MS / 1000}s`));
|
|
27
|
+
};
|
|
28
|
+
// Set before the request: a mocked or very fast response can settle synchronously.
|
|
29
|
+
deadline = setTimeout(timedOut, REQUEST_TIMEOUT_MS);
|
|
30
|
+
req = https.get(url, { timeout: REQUEST_TIMEOUT_MS }, res => {
|
|
31
|
+
gotResponse = true;
|
|
32
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
33
|
+
res.resume();
|
|
34
|
+
return fail(new Error(`HTTP ${res.statusCode}`));
|
|
35
|
+
}
|
|
36
|
+
let body = '';
|
|
37
|
+
let bytes = 0;
|
|
38
|
+
let ended = false;
|
|
39
|
+
res.on('data', (chunk) => {
|
|
40
|
+
if (settled) return;
|
|
41
|
+
bytes += chunk.length;
|
|
42
|
+
if (bytes > MAX_RESPONSE_BYTES) {
|
|
43
|
+
if (req && typeof req.destroy === 'function') req.destroy();
|
|
44
|
+
fail(new Error('Response too large'));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
body += chunk;
|
|
48
|
+
});
|
|
49
|
+
res.on('end', () => {
|
|
50
|
+
ended = true;
|
|
51
|
+
if (settled) return;
|
|
52
|
+
let data;
|
|
53
|
+
try { data = JSON.parse(body); }
|
|
54
|
+
catch (e) { return fail(new Error(`JSON parse: ${e.message}`)); }
|
|
55
|
+
// Callers read fields off the answer: null, a string or a list is not one.
|
|
56
|
+
if (data === null || typeof data !== 'object' || Array.isArray(data)) {
|
|
57
|
+
return fail(new Error('Unexpected response shape'));
|
|
58
|
+
}
|
|
59
|
+
settle(resolve, data);
|
|
60
|
+
});
|
|
61
|
+
// A connection closed part way through the body gives no 'end' and no 'error'.
|
|
62
|
+
const closedEarly = () => {
|
|
63
|
+
if (!ended && !res.complete) fail(new Error('Connection closed before the response completed'));
|
|
64
|
+
};
|
|
65
|
+
res.on('aborted', closedEarly);
|
|
66
|
+
res.on('close', closedEarly);
|
|
67
|
+
});
|
|
68
|
+
req.on('error', fail);
|
|
69
|
+
// Once a response has started, its own close handling above decides.
|
|
70
|
+
req.on('close', () => {
|
|
71
|
+
if (!gotResponse) fail(new Error('Connection closed before a response arrived'));
|
|
72
|
+
});
|
|
73
|
+
if (typeof req.setTimeout === 'function') {
|
|
74
|
+
req.setTimeout(REQUEST_TIMEOUT_MS, timedOut);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = httpGetJson;
|
|
80
|
+
module.exports.MAX_RESPONSE_BYTES = MAX_RESPONSE_BYTES;
|
package/lib/payload.js
CHANGED
|
@@ -1,88 +1,104 @@
|
|
|
1
|
-
// lib/payload.js
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
function mergePayload(base, newData) {
|
|
5
|
-
return Object.assign({}, base, newData);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function buildDefaultPayload(state) {
|
|
9
|
-
if (!state || typeof state !== 'object') {
|
|
10
|
-
throw new TypeError('buildDefaultPayload: state must be an object');
|
|
11
|
-
}
|
|
12
|
-
const { confirmedLimit, confirmedTime, pendingLimit, pendingTime, chargingNow, smartChargingSuspended } = state;
|
|
13
|
-
return {
|
|
14
|
-
// V1 fields — unchanged
|
|
15
|
-
next_start: null, total_energy: 0, next_kwh: '0', next_source: 'unknown',
|
|
16
|
-
confirmed_limit: confirmedLimit, confirmed_time: confirmedTime,
|
|
17
|
-
pending_limit: pendingLimit, pending_time: pendingTime,
|
|
18
|
-
charging_now: chargingNow,
|
|
19
|
-
next_poll: null, next_poll_raw: null,
|
|
20
|
-
refresh_available_at: null,
|
|
21
|
-
api_requests_hour: 0, api_complexity_hour: 0, api_complexity_percent: 0,
|
|
22
|
-
slot1_start: null, slot1_end: null, slot2_start: null, slot2_end: null,
|
|
23
|
-
slot3_start: null, slot3_end: null,
|
|
24
|
-
window_start: null, window_end: null,
|
|
25
|
-
next_start_raw: null,
|
|
26
|
-
slot1_start_raw: null, slot1_end_raw: null, slot2_start_raw: null, slot2_end_raw: null,
|
|
27
|
-
slot3_start_raw: null, slot3_end_raw: null,
|
|
28
|
-
window_start_raw: null, window_end_raw: null,
|
|
29
|
-
timezone_detected: null, timezone_applied: null,
|
|
30
|
-
next_start_locale: null,
|
|
31
|
-
slot1_start_locale: null, slot1_end_locale: null,
|
|
32
|
-
slot2_start_locale: null, slot2_end_locale: null,
|
|
33
|
-
slot3_start_locale: null, slot3_end_locale: null,
|
|
34
|
-
window_start_locale: null, window_end_locale: null,
|
|
35
|
-
smart_charging: smartChargingSuspended === null ? null : !smartChargingSuspended,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
// lib/payload.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
function mergePayload(base, newData) {
|
|
5
|
+
return Object.assign({}, base, newData);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function buildDefaultPayload(state) {
|
|
9
|
+
if (!state || typeof state !== 'object') {
|
|
10
|
+
throw new TypeError('buildDefaultPayload: state must be an object');
|
|
11
|
+
}
|
|
12
|
+
const { confirmedLimit, confirmedTime, pendingLimit, pendingTime, chargingNow, smartChargingSuspended } = state;
|
|
13
|
+
return {
|
|
14
|
+
// V1 fields — unchanged
|
|
15
|
+
next_start: null, total_energy: 0, next_kwh: '0', next_source: 'unknown',
|
|
16
|
+
confirmed_limit: confirmedLimit, confirmed_time: confirmedTime,
|
|
17
|
+
pending_limit: pendingLimit, pending_time: pendingTime,
|
|
18
|
+
charging_now: chargingNow,
|
|
19
|
+
next_poll: null, next_poll_raw: null,
|
|
20
|
+
refresh_available_at: null,
|
|
21
|
+
api_requests_hour: 0, api_complexity_hour: 0, api_complexity_percent: 0,
|
|
22
|
+
slot1_start: null, slot1_end: null, slot2_start: null, slot2_end: null,
|
|
23
|
+
slot3_start: null, slot3_end: null,
|
|
24
|
+
window_start: null, window_end: null,
|
|
25
|
+
next_start_raw: null,
|
|
26
|
+
slot1_start_raw: null, slot1_end_raw: null, slot2_start_raw: null, slot2_end_raw: null,
|
|
27
|
+
slot3_start_raw: null, slot3_end_raw: null,
|
|
28
|
+
window_start_raw: null, window_end_raw: null,
|
|
29
|
+
timezone_detected: null, timezone_applied: null,
|
|
30
|
+
next_start_locale: null,
|
|
31
|
+
slot1_start_locale: null, slot1_end_locale: null,
|
|
32
|
+
slot2_start_locale: null, slot2_end_locale: null,
|
|
33
|
+
slot3_start_locale: null, slot3_end_locale: null,
|
|
34
|
+
window_start_locale: null, window_end_locale: null,
|
|
35
|
+
smart_charging: smartChargingSuspended === null ? null : !smartChargingSuspended,
|
|
36
|
+
charge_cap: null,
|
|
37
|
+
boosting: null, boost_error: null,
|
|
38
|
+
// V2 fields — new categories
|
|
39
|
+
electricity_unit_rate: null, electricity_standing_charge: null,
|
|
40
|
+
electricity_tariff_code: null, electricity_valid_from: null, electricity_valid_to: null,
|
|
41
|
+
electricity_export_unit_rate: null, electricity_export_standing_charge: null,
|
|
42
|
+
electricity_export_tariff_code: null, electricity_export_valid_from: null, electricity_export_valid_to: null,
|
|
43
|
+
electricity_consumption_kwh: null, electricity_consumption_cost: null,
|
|
44
|
+
electricity_consumption_from: null, electricity_consumption_to: null,
|
|
45
|
+
electricity_export_consumption_kwh: null,
|
|
46
|
+
electricity_export_consumption_from: null, electricity_export_consumption_to: null,
|
|
47
|
+
electricity_export_rate: [], electricity_export_rate_count: 0,
|
|
48
|
+
electricity_export_rate_current_pence: null, electricity_export_rate_current_gbp: null,
|
|
49
|
+
electricity_export_rate_prev_pence: null, electricity_export_rate_prev_gbp: null, electricity_export_rate_prev_to: null,
|
|
50
|
+
electricity_export_rate_next_pence: null, electricity_export_rate_next_gbp: null, electricity_export_rate_next_from: null,
|
|
51
|
+
electricity_export_rate_error: null,
|
|
52
|
+
electricity_rates_error: null, electricity_consumption_error: null,
|
|
53
|
+
gas_unit_rate: null, gas_standing_charge: null,
|
|
54
|
+
gas_tariff_code: null, gas_valid_from: null, gas_valid_to: null,
|
|
55
|
+
gas_consumption_kwh: null, gas_consumption_cost: null,
|
|
56
|
+
gas_consumption_from: null, gas_consumption_to: null,
|
|
57
|
+
gas_rates_error: null, gas_consumption_error: null,
|
|
58
|
+
wheel_of_fortune_electricity_spins: null, wheel_of_fortune_electricity_max: null,
|
|
59
|
+
wheel_of_fortune_electricity_used: null,
|
|
60
|
+
wheel_of_fortune_gas_spins: null, wheel_of_fortune_gas_max: null,
|
|
61
|
+
wheel_of_fortune_gas_used: null, wheel_of_fortune_error: null,
|
|
62
|
+
mini_demand_kw: null, mini_consumption_delta_kwh: null, mini_read_at: null, home_mini_error: null,
|
|
63
|
+
saving_session_available: null, saving_session_start: null, saving_session_end: null,
|
|
64
|
+
saving_session_joined: null, saving_session_window_active: null, saving_session_active: null,
|
|
65
|
+
saving_session_points: null, saving_sessions_error: null,
|
|
66
|
+
free_electricity_active: null, free_electricity_available: null,
|
|
67
|
+
free_electricity_start: null, free_electricity_end: null, free_electricity_error: null,
|
|
68
|
+
intelligent_error: null,
|
|
69
|
+
completed_dispatches: [], completed_dispatches_count: 0, completed_dispatches_error: null,
|
|
70
|
+
flex_planned_dispatches: [], flex_planned_dispatches_count: 0, flex_planned_dispatches_error: null,
|
|
71
|
+
applicable_rates: [], applicable_rates_count: 0,
|
|
72
|
+
applicable_rates_current_pence: null, applicable_rates_error: null,
|
|
73
|
+
account_balance_pence: null, account_balance_pounds: null, account_error: null,
|
|
74
|
+
octoplus_enrolled: null, octoplus_enrollment_status: null,
|
|
75
|
+
octoplus_loyalty_points_user: null, octoplus_error: null,
|
|
76
|
+
// v1.5 — tariff rate-band fields (electricity)
|
|
77
|
+
electricity_day_rate: null, electricity_night_rate: null,
|
|
78
|
+
electricity_ev_peak_rate: null, electricity_ev_off_peak_rate: null,
|
|
79
|
+
// v1.5 — 24h schedule stats
|
|
80
|
+
applicable_rates_min_pence: null, applicable_rates_max_pence: null,
|
|
81
|
+
applicable_rates_median_pence: null, applicable_rates_avg_pence: null,
|
|
82
|
+
electricity_export_rate_min_pence: null, electricity_export_rate_max_pence: null,
|
|
83
|
+
electricity_export_rate_median_pence: null, electricity_export_rate_avg_pence: null,
|
|
84
|
+
// v1.7.0 — SmartFlex vehicle / charge point (smartflex_vehicle category)
|
|
85
|
+
vehicle_make: null, vehicle_model: null,
|
|
86
|
+
vehicle_battery_kwh: null, charger_power_kw: null,
|
|
87
|
+
device_state: null, device_alerts: [],
|
|
88
|
+
last_session: null, charging_sessions: [],
|
|
89
|
+
last_session_kwh: null, last_session_soc_final: null, last_session_problem: null,
|
|
90
|
+
smartflex_vehicle_error: null,
|
|
91
|
+
// v1.7.0 — SmartFlex inverter, experimental (smartflex_inverter category)
|
|
92
|
+
inverter_make: null, inverter_model: null, solar_peak_kw: null,
|
|
93
|
+
inverter_power_kw: null, inverter_connection_status: null, inverter_online: null,
|
|
94
|
+
home_battery_power_kw: null, home_battery_soc: null,
|
|
95
|
+
solar_power_kw: null, grid_power_kw: null,
|
|
96
|
+
site_consumption_kw: null, site_export_kw: null,
|
|
97
|
+
smartflex_inverter_error: null,
|
|
98
|
+
// v1.5 — update check
|
|
99
|
+
installed_version: null, latest_version: null,
|
|
100
|
+
update_available: false, update_check_at: null, update_check_error: null
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { mergePayload, buildDefaultPayload };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Quick retry of the charging poll (1.6.2). Octopus fails flexPlannedDispatches (KT-CT-4340)
|
|
4
|
+
// and sometimes devices.preferences (KT-CT-4339) for a few minutes around :00 and :30.
|
|
5
|
+
const RETRY_CODES = ['KT-CT-4340', 'KT-CT-4339'];
|
|
6
|
+
|
|
7
|
+
// Delay before retry 1, 2, 3; every later retry waits as long as the last one.
|
|
8
|
+
const RETRY_DELAYS_MS = [30000, 60000, 120000];
|
|
9
|
+
|
|
10
|
+
// Octopus allows 5 flexPlannedDispatches queries a minute; stay at 3 or fewer.
|
|
11
|
+
const DISPATCH_RATE_MAX = 3;
|
|
12
|
+
const DISPATCH_RATE_WINDOW_MS = 60000;
|
|
13
|
+
|
|
14
|
+
// A backward wall-clock step of at least this much is a real clock change (lib/scheduler.js
|
|
15
|
+
// re-polls after one); a smaller step is an NTP correction and is ignored.
|
|
16
|
+
const CLOCK_STEP_BACK_MS = 60 * 1000;
|
|
17
|
+
|
|
18
|
+
function retryDelay(retryNo) {
|
|
19
|
+
const i = Math.min(Math.max(retryNo, 1), RETRY_DELAYS_MS.length) - 1;
|
|
20
|
+
return RETRY_DELAYS_MS[i];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isRetryable(err) {
|
|
24
|
+
return !!err && RETRY_CODES.includes(err.code);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Start of the next interval slot, counted from the epoch as the scheduler's isDue does.
|
|
28
|
+
function nextSlotStart(nowMs, intervalMs) {
|
|
29
|
+
return (Math.floor(nowMs / intervalMs) + 1) * intervalMs;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Sliding record of query times. A query at time t counts against every closed window
|
|
33
|
+
// [t, t + windowMs]; nextAllowedAt gives the earliest time one more query keeps every
|
|
34
|
+
// such window at or under max.
|
|
35
|
+
function createRateWindow({ max = DISPATCH_RATE_MAX, windowMs = DISPATCH_RATE_WINDOW_MS } = {}) {
|
|
36
|
+
let times = [];
|
|
37
|
+
// A time after `now` is left from before the wall clock stepped back. After a large step
|
|
38
|
+
// it no longer counts; after a small one (an NTP correction) it counts as made now.
|
|
39
|
+
const recentAt = (now) => times
|
|
40
|
+
.filter((t) => t - now < CLOCK_STEP_BACK_MS)
|
|
41
|
+
.map((t) => Math.min(t, now))
|
|
42
|
+
.filter((t) => now - t <= windowMs);
|
|
43
|
+
const prune = (now) => { times = recentAt(now); };
|
|
44
|
+
return {
|
|
45
|
+
record(now = Date.now()) {
|
|
46
|
+
prune(now);
|
|
47
|
+
times.push(now);
|
|
48
|
+
},
|
|
49
|
+
// Read-only: asking must not change the window.
|
|
50
|
+
nextAllowedAt(now = Date.now()) {
|
|
51
|
+
const recent = recentAt(now).sort((a, b) => a - b);
|
|
52
|
+
if (recent.length < max) return now;
|
|
53
|
+
return recent[recent.length - max] + windowMs + 1;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function formatDuration(ms) {
|
|
59
|
+
const total = Math.max(0, Math.round(ms / 1000));
|
|
60
|
+
const h = Math.floor(total / 3600);
|
|
61
|
+
const m = Math.floor((total % 3600) / 60);
|
|
62
|
+
const s = total % 60;
|
|
63
|
+
if (h > 0) return `${h}h ${m}m ${s}s`;
|
|
64
|
+
if (m > 0) return `${m}m ${s}s`;
|
|
65
|
+
return `${s}s`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = {
|
|
69
|
+
RETRY_CODES, RETRY_DELAYS_MS, DISPATCH_RATE_MAX, DISPATCH_RATE_WINDOW_MS, CLOCK_STEP_BACK_MS,
|
|
70
|
+
retryDelay, isRetryable, nextSlotStart, createRateWindow, formatDuration,
|
|
71
|
+
};
|