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/timezone.js
CHANGED
|
@@ -1,49 +1,67 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
const { shown } = require('./values');
|
|
3
|
+
|
|
4
|
+
// Longest IANA names are about 30 characters; the value is also an HA select state.
|
|
5
|
+
const MAX_TIMEZONE_LENGTH = 64;
|
|
6
|
+
|
|
7
|
+
function convertToTimezone(dateStr, tz) {
|
|
8
|
+
if (!dateStr || typeof dateStr !== 'string') return dateStr;
|
|
9
|
+
try {
|
|
10
|
+
const date = new Date(dateStr.replace(' ', 'T'));
|
|
11
|
+
if (isNaN(date.getTime())) return dateStr;
|
|
12
|
+
const parts = new Intl.DateTimeFormat('en-CA', {
|
|
13
|
+
timeZone: tz,
|
|
14
|
+
year: 'numeric', month: '2-digit', day: '2-digit',
|
|
15
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit',
|
|
16
|
+
hour12: false, hourCycle: 'h23'
|
|
17
|
+
}).formatToParts(date);
|
|
18
|
+
const get = (type) => (parts.find(p => p.type === type) || {}).value || '00';
|
|
19
|
+
const y = get('year'), mo = get('month'), d = get('day');
|
|
20
|
+
const h = get('hour'), mi = get('minute'), s = get('second');
|
|
21
|
+
const localAsUtc = Date.UTC(
|
|
22
|
+
parseInt(y, 10), parseInt(mo, 10) - 1, parseInt(d, 10),
|
|
23
|
+
parseInt(h, 10), parseInt(mi, 10), parseInt(s, 10)
|
|
24
|
+
);
|
|
25
|
+
const offsetMins = Math.round((localAsUtc - date.getTime()) / 60000);
|
|
26
|
+
const sign = offsetMins >= 0 ? '+' : '-';
|
|
27
|
+
const absMin = Math.abs(offsetMins);
|
|
28
|
+
const offsetStr = `${sign}${String(Math.floor(absMin / 60)).padStart(2, '0')}:${String(absMin % 60).padStart(2, '0')}`;
|
|
29
|
+
return `${y}-${mo}-${d} ${h}:${mi}:${s}${offsetStr}`;
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return dateStr;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function resolveTimezone(nd) {
|
|
36
|
+
try {
|
|
37
|
+
const persisted = nd.context().get('timezone');
|
|
38
|
+
if (persisted && typeof persisted === 'string' && persisted.trim().length > 0) {
|
|
39
|
+
return persisted.trim();
|
|
40
|
+
}
|
|
41
|
+
} catch (e) {}
|
|
42
|
+
const override = nd.timezoneOverride;
|
|
43
|
+
if (override && typeof override === 'string' && override.trim().length > 0) {
|
|
44
|
+
return override.trim();
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return 'UTC';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A timezone from set_timezone (input or MQTT): a non-empty IANA name that Intl knows, at most
|
|
54
|
+
// MAX_TIMEZONE_LENGTH characters. Trimmed. A wrong name used to be stored as it was, and every
|
|
55
|
+
// display time then kept the raw UTC text.
|
|
56
|
+
function validateTimezone(value) {
|
|
57
|
+
const tz = typeof value === 'string' ? value.trim() : '';
|
|
58
|
+
if (tz.length > 0 && tz.length <= MAX_TIMEZONE_LENGTH) {
|
|
59
|
+
try {
|
|
60
|
+
new Intl.DateTimeFormat('en-GB', { timeZone: tz });
|
|
61
|
+
return { ok: true, timezone: tz };
|
|
62
|
+
} catch (e) { /* unknown zone: fall through */ }
|
|
63
|
+
}
|
|
64
|
+
return { ok: false, error: `Invalid timezone '${shown(value)}' — must be an IANA name such as Europe/London` };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { convertToTimezone, resolveTimezone, validateTimezone, MAX_TIMEZONE_LENGTH };
|
package/lib/update-check.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const NPM_LATEST_URL = 'https://registry.npmjs.org/node-red-contrib-octopus-intelligent/latest';
|
|
4
|
-
|
|
5
|
-
function compareSemver(a, b) {
|
|
6
|
-
// Strict numeric X.Y.Z with optional pre-release suffix.
|
|
7
|
-
// Pre-release (1.5.0-dev.1) is OLDER than the base release (1.5.0).
|
|
8
|
-
const parse = v => {
|
|
9
|
-
const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)(-.+)?$/);
|
|
10
|
-
if (!m) return null;
|
|
11
|
-
return { major: +m[1], minor: +m[2], patch: +m[3], pre: m[4] || null };
|
|
12
|
-
};
|
|
13
|
-
const pa = parse(a), pb = parse(b);
|
|
14
|
-
if (!pa || !pb) return 0;
|
|
15
|
-
if (pa.major !== pb.major) return pa.major - pb.major;
|
|
16
|
-
if (pa.minor !== pb.minor) return pa.minor - pb.minor;
|
|
17
|
-
if (pa.patch !== pb.patch) return pa.patch - pb.patch;
|
|
18
|
-
if (pa.pre && !pb.pre) return -1;
|
|
19
|
-
if (!pa.pre && pb.pre) return 1;
|
|
20
|
-
return 0;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function checkUpdate(httpGetJson, installedVersion) {
|
|
24
|
-
const data = await httpGetJson(NPM_LATEST_URL);
|
|
25
|
-
const latest = data && data.version;
|
|
26
|
-
if (typeof latest !== 'string' || !/^\d+\.\d+\.\d+/.test(latest)) {
|
|
27
|
-
throw new Error('npm registry response missing version');
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
installed_version: installedVersion,
|
|
31
|
-
latest_version: latest,
|
|
32
|
-
update_available: compareSemver(latest, installedVersion) > 0,
|
|
33
|
-
update_check_at: new Date().toISOString(),
|
|
34
|
-
update_check_error: null,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
module.exports = { checkUpdate, compareSemver, NPM_LATEST_URL };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const NPM_LATEST_URL = 'https://registry.npmjs.org/node-red-contrib-octopus-intelligent/latest';
|
|
4
|
+
|
|
5
|
+
function compareSemver(a, b) {
|
|
6
|
+
// Strict numeric X.Y.Z with optional pre-release suffix.
|
|
7
|
+
// Pre-release (1.5.0-dev.1) is OLDER than the base release (1.5.0).
|
|
8
|
+
const parse = v => {
|
|
9
|
+
const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)(-.+)?$/);
|
|
10
|
+
if (!m) return null;
|
|
11
|
+
return { major: +m[1], minor: +m[2], patch: +m[3], pre: m[4] || null };
|
|
12
|
+
};
|
|
13
|
+
const pa = parse(a), pb = parse(b);
|
|
14
|
+
if (!pa || !pb) return 0;
|
|
15
|
+
if (pa.major !== pb.major) return pa.major - pb.major;
|
|
16
|
+
if (pa.minor !== pb.minor) return pa.minor - pb.minor;
|
|
17
|
+
if (pa.patch !== pb.patch) return pa.patch - pb.patch;
|
|
18
|
+
if (pa.pre && !pb.pre) return -1;
|
|
19
|
+
if (!pa.pre && pb.pre) return 1;
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function checkUpdate(httpGetJson, installedVersion) {
|
|
24
|
+
const data = await httpGetJson(NPM_LATEST_URL);
|
|
25
|
+
const latest = data && data.version;
|
|
26
|
+
if (typeof latest !== 'string' || !/^\d+\.\d+\.\d+/.test(latest)) {
|
|
27
|
+
throw new Error('npm registry response missing version');
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
installed_version: installedVersion,
|
|
31
|
+
latest_version: latest,
|
|
32
|
+
update_available: compareSemver(latest, installedVersion) > 0,
|
|
33
|
+
update_check_at: new Date().toISOString(),
|
|
34
|
+
update_check_error: null,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { checkUpdate, compareSemver, NPM_LATEST_URL };
|
package/lib/values.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Kraken numeric scalars can arrive as numbers or as JSON strings ("12.5"). A value that is
|
|
4
|
+
// not a finite number (null, "abc", "Infinity", 1e400 in the JSON, a list) becomes null, so
|
|
5
|
+
// NaN and Infinity never reach msg.payload or Home Assistant.
|
|
6
|
+
function toNumber(value) {
|
|
7
|
+
if (typeof value === 'number') return Number.isFinite(value) ? value : null;
|
|
8
|
+
if (typeof value !== 'string') return null;
|
|
9
|
+
const n = parseFloat(value);
|
|
10
|
+
return Number.isFinite(n) ? n : null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A value as text for an error message, cut to `max` characters. Never throws: a Symbol, an
|
|
14
|
+
// object with no prototype or a throwing toString still gives some text.
|
|
15
|
+
function shown(value, max = 40) {
|
|
16
|
+
let s;
|
|
17
|
+
try {
|
|
18
|
+
s = String(value);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
s = Object.prototype.toString.call(value);
|
|
21
|
+
}
|
|
22
|
+
return s.length > max ? `${s.slice(0, max)}...` : s;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Home Assistant rejects a state of 255 characters or more, and the entity then stops updating.
|
|
26
|
+
const MAX_STATE_LENGTH = 254;
|
|
27
|
+
|
|
28
|
+
// Text from the API that becomes an entity state (a tariff code, a time Octopus sent that could
|
|
29
|
+
// not be read, a charge source) is cut to MAX_STATE_LENGTH.
|
|
30
|
+
function capText(value) {
|
|
31
|
+
return typeof value === 'string' && value.length > MAX_STATE_LENGTH
|
|
32
|
+
? `${value.slice(0, MAX_STATE_LENGTH - 3)}...` : value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// `parse` with every top-level text field of its result cut by capText. Lists and objects
|
|
36
|
+
// (attributes such as the rate list) are left as they are.
|
|
37
|
+
function capStrings(parse) {
|
|
38
|
+
return function capped(...args) {
|
|
39
|
+
const out = parse(...args);
|
|
40
|
+
if (!out || typeof out !== 'object') return out;
|
|
41
|
+
const copy = Object.assign({}, out);
|
|
42
|
+
for (const k of Object.keys(copy)) copy[k] = capText(copy[k]);
|
|
43
|
+
return copy;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = { toNumber, shown, capText, capStrings, MAX_STATE_LENGTH };
|