node-red-contrib-octopus-intelligent 1.2.0 → 1.3.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 +83 -0
- package/README.md +619 -222
- package/examples/ha-dashboard.yaml +470 -0
- package/lib/api-metrics.js +31 -0
- package/lib/auth.js +20 -0
- package/lib/categories/account.js +30 -0
- package/lib/categories/applicable-rates.js +91 -0
- package/lib/categories/completed-dispatches.js +35 -0
- package/lib/categories/electricity.js +132 -0
- package/lib/categories/flex-planned-dispatches.js +36 -0
- package/lib/categories/free-electricity.js +61 -0
- package/lib/categories/gas.js +94 -0
- package/lib/categories/home-mini.js +37 -0
- package/lib/categories/intelligent.js +123 -0
- package/lib/categories/octoplus.js +32 -0
- package/lib/categories/saving-sessions.js +50 -0
- package/lib/categories/wheel-of-fortune.js +49 -0
- package/lib/discovery.js +85 -0
- package/lib/graphql.js +44 -0
- package/lib/payload.js +76 -0
- package/lib/scheduler.js +33 -0
- package/lib/timezone.js +49 -0
- package/octopus-intelligent.html +71 -4
- package/octopus-intelligent.js +628 -386
- package/package.json +2 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// lib/categories/electricity.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const RATES_QUERY = `
|
|
5
|
+
query getElectricityRates($account: String!) {
|
|
6
|
+
account(accountNumber: $account) {
|
|
7
|
+
electricityAgreements {
|
|
8
|
+
validFrom validTo
|
|
9
|
+
tariff {
|
|
10
|
+
... on TariffType { productCode standingCharge }
|
|
11
|
+
... on StandardTariff { productCode standingCharge unitRate }
|
|
12
|
+
... on HalfHourlyTariff { productCode standingCharge }
|
|
13
|
+
... on DayNightTariff { productCode standingCharge }
|
|
14
|
+
... on FourRateEvTariff { productCode standingCharge }
|
|
15
|
+
... on PrepayTariff { productCode standingCharge unitRate }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}`;
|
|
20
|
+
|
|
21
|
+
const CONSUMPTION_QUERY = `
|
|
22
|
+
query getElectricityConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
|
|
23
|
+
account(accountNumber: $account) {
|
|
24
|
+
properties {
|
|
25
|
+
electricityMeterPoints {
|
|
26
|
+
mpan
|
|
27
|
+
meters {
|
|
28
|
+
consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
|
|
29
|
+
edges { node { startAt endAt value } }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}`;
|
|
36
|
+
|
|
37
|
+
function buildRatesQuery(account) {
|
|
38
|
+
return { query: RATES_QUERY, variables: { account } };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function buildConsumptionQuery(account, timezone) {
|
|
42
|
+
const tz = timezone || 'UTC';
|
|
43
|
+
const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
|
|
44
|
+
return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Tariff productCodes containing "OUTGOING" are export agreements (e.g. AGILE-OUTGOING-19-05-13).
|
|
48
|
+
// We surface both import and export tariff metadata as separate fields.
|
|
49
|
+
function parseRatesResponse(data) {
|
|
50
|
+
const all = (data.account || {}).electricityAgreements || [];
|
|
51
|
+
const isExport = (a) => ((a.tariff || {}).productCode || '').includes('OUTGOING');
|
|
52
|
+
const importAgreements = all.filter(a => !isExport(a));
|
|
53
|
+
const exportAgreements = all.filter(isExport);
|
|
54
|
+
|
|
55
|
+
const fieldsFor = (prefix, agreements) => {
|
|
56
|
+
const latest = agreements[0];
|
|
57
|
+
const tariff = (latest && latest.tariff) || {};
|
|
58
|
+
return {
|
|
59
|
+
[`${prefix}_unit_rate`]: tariff.unitRate ?? null,
|
|
60
|
+
[`${prefix}_standing_charge`]: tariff.standingCharge ?? null,
|
|
61
|
+
[`${prefix}_tariff_code`]: tariff.productCode || null,
|
|
62
|
+
[`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
|
|
63
|
+
[`${prefix}_valid_to`]: (latest && latest.validTo) || null,
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
...fieldsFor('electricity', importAgreements),
|
|
69
|
+
...fieldsFor('electricity_export', exportAgreements),
|
|
70
|
+
electricity_rates_error: null
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Picks the latest consumption edge for a given MPAN out of the query response.
|
|
75
|
+
function _findEntryForMpan(data, mpan) {
|
|
76
|
+
if (!mpan) return null;
|
|
77
|
+
const points = ((data.account || {}).properties || [])[0];
|
|
78
|
+
const meterPoints = points ? (points.electricityMeterPoints || []) : [];
|
|
79
|
+
const mp = meterPoints.find(p => p.mpan === mpan);
|
|
80
|
+
if (!mp) return null;
|
|
81
|
+
const meter = (mp.meters || [])[0];
|
|
82
|
+
const edges = ((meter && meter.consumption) || {}).edges || [];
|
|
83
|
+
return edges.length > 0 ? edges[edges.length - 1].node : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Parses both import and export consumption from one response, given both MPANs.
|
|
87
|
+
// Either MPAN may be null on accounts without that direction — the corresponding
|
|
88
|
+
// fields will be null without affecting the other direction.
|
|
89
|
+
function parseConsumptionResponse(data, importMpan, exportMpan) {
|
|
90
|
+
const importEntry = _findEntryForMpan(data, importMpan);
|
|
91
|
+
const exportEntry = _findEntryForMpan(data, exportMpan);
|
|
92
|
+
return {
|
|
93
|
+
electricity_consumption_kwh: importEntry ? parseFloat(importEntry.value) : null,
|
|
94
|
+
electricity_consumption_cost: null,
|
|
95
|
+
electricity_consumption_from: importEntry ? importEntry.startAt : null,
|
|
96
|
+
electricity_consumption_to: importEntry ? importEntry.endAt : null,
|
|
97
|
+
electricity_export_consumption_kwh: exportEntry ? parseFloat(exportEntry.value) : null,
|
|
98
|
+
electricity_export_consumption_from: exportEntry ? exportEntry.startAt : null,
|
|
99
|
+
electricity_export_consumption_to: exportEntry ? exportEntry.endAt : null,
|
|
100
|
+
electricity_consumption_error: null
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const defaultData = {
|
|
105
|
+
electricity_unit_rate: null,
|
|
106
|
+
electricity_standing_charge: null,
|
|
107
|
+
electricity_tariff_code: null,
|
|
108
|
+
electricity_valid_from: null,
|
|
109
|
+
electricity_valid_to: null,
|
|
110
|
+
electricity_export_unit_rate: null,
|
|
111
|
+
electricity_export_standing_charge: null,
|
|
112
|
+
electricity_export_tariff_code: null,
|
|
113
|
+
electricity_export_valid_from: null,
|
|
114
|
+
electricity_export_valid_to: null,
|
|
115
|
+
electricity_rates_error: null,
|
|
116
|
+
electricity_consumption_kwh: null,
|
|
117
|
+
electricity_consumption_cost: null,
|
|
118
|
+
electricity_consumption_from: null,
|
|
119
|
+
electricity_consumption_to: null,
|
|
120
|
+
electricity_export_consumption_kwh: null,
|
|
121
|
+
electricity_export_consumption_from: null,
|
|
122
|
+
electricity_export_consumption_to: null,
|
|
123
|
+
electricity_consumption_error: null
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
module.exports = {
|
|
127
|
+
buildRatesQuery,
|
|
128
|
+
buildConsumptionQuery,
|
|
129
|
+
parseRatesResponse,
|
|
130
|
+
parseConsumptionResponse,
|
|
131
|
+
defaultData
|
|
132
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// lib/categories/flex-planned-dispatches.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const QUERY = `
|
|
5
|
+
query getFlexPlannedDispatches($deviceId: String!) {
|
|
6
|
+
flexPlannedDispatches(deviceId: $deviceId) {
|
|
7
|
+
start end type energyAddedKwh
|
|
8
|
+
}
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
function buildQuery(deviceId) {
|
|
12
|
+
return { query: QUERY, variables: { deviceId } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseResponse(data) {
|
|
16
|
+
const dispatches = data.flexPlannedDispatches || [];
|
|
17
|
+
return {
|
|
18
|
+
flex_planned_dispatches: dispatches.map(d => ({
|
|
19
|
+
start: d.start,
|
|
20
|
+
end: d.end,
|
|
21
|
+
type: d.type || null,
|
|
22
|
+
energy_kwh: d.energyAddedKwh !== null && d.energyAddedKwh !== undefined
|
|
23
|
+
? parseFloat(d.energyAddedKwh) : null
|
|
24
|
+
})),
|
|
25
|
+
flex_planned_dispatches_count: dispatches.length,
|
|
26
|
+
flex_planned_dispatches_error: null
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultData = {
|
|
31
|
+
flex_planned_dispatches: [],
|
|
32
|
+
flex_planned_dispatches_count: 0,
|
|
33
|
+
flex_planned_dispatches_error: null
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// lib/categories/free-electricity.js
|
|
2
|
+
// Detects upcoming and active "Free Electricity" sessions (0p/kWh windows).
|
|
3
|
+
// Requires account number and MPAN from account discovery.
|
|
4
|
+
'use strict';
|
|
5
|
+
|
|
6
|
+
const QUERY = `
|
|
7
|
+
query getFreeElectricity($account: String!, $mpan: String!) {
|
|
8
|
+
customerFlexibilityCampaignEvents(
|
|
9
|
+
accountNumber: $account,
|
|
10
|
+
supplyPointIdentifier: $mpan,
|
|
11
|
+
campaignSlug: "free_electricity",
|
|
12
|
+
first: 50
|
|
13
|
+
) {
|
|
14
|
+
edges {
|
|
15
|
+
node {
|
|
16
|
+
code
|
|
17
|
+
startAt
|
|
18
|
+
endAt
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}`;
|
|
23
|
+
|
|
24
|
+
function buildQuery(account, mpan) {
|
|
25
|
+
return { query: QUERY, variables: { account, mpan } };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseResponse(data) {
|
|
29
|
+
const now = new Date();
|
|
30
|
+
|
|
31
|
+
const edges = ((data.customerFlexibilityCampaignEvents || {}).edges) || [];
|
|
32
|
+
const sessions = edges.map(e => ({ startAt: e.node.startAt, endAt: e.node.endAt }));
|
|
33
|
+
|
|
34
|
+
// Filter to sessions not yet ended, sort by startAt ascending, take the first
|
|
35
|
+
const future = sessions
|
|
36
|
+
.filter(s => new Date(s.endAt) > now)
|
|
37
|
+
.sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
|
|
38
|
+
|
|
39
|
+
const next = future[0] || null;
|
|
40
|
+
|
|
41
|
+
const active = next !== null && new Date(next.startAt) <= now && new Date(next.endAt) > now;
|
|
42
|
+
const available = next !== null;
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
free_electricity_active: active,
|
|
46
|
+
free_electricity_available: available,
|
|
47
|
+
free_electricity_start: next ? next.startAt : null,
|
|
48
|
+
free_electricity_end: next ? next.endAt : null,
|
|
49
|
+
free_electricity_error: null
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const defaultData = {
|
|
54
|
+
free_electricity_active: null,
|
|
55
|
+
free_electricity_available: null,
|
|
56
|
+
free_electricity_start: null,
|
|
57
|
+
free_electricity_end: null,
|
|
58
|
+
free_electricity_error: null
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// lib/categories/gas.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// GasTariffType is a single object type (not a union) — direct field access, no fragments.
|
|
5
|
+
const RATES_QUERY = `
|
|
6
|
+
query getGasRates($account: String!) {
|
|
7
|
+
account(accountNumber: $account) {
|
|
8
|
+
gasAgreements {
|
|
9
|
+
validFrom validTo
|
|
10
|
+
tariff {
|
|
11
|
+
productCode
|
|
12
|
+
standingCharge
|
|
13
|
+
unitRate
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}`;
|
|
18
|
+
|
|
19
|
+
const CONSUMPTION_QUERY = `
|
|
20
|
+
query getGasConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
|
|
21
|
+
account(accountNumber: $account) {
|
|
22
|
+
properties {
|
|
23
|
+
gasMeterPoints {
|
|
24
|
+
meters {
|
|
25
|
+
consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
|
|
26
|
+
edges { node { startAt endAt value } }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}`;
|
|
33
|
+
|
|
34
|
+
function buildRatesQuery(account) {
|
|
35
|
+
return { query: RATES_QUERY, variables: { account } };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function buildConsumptionQuery(account, timezone) {
|
|
39
|
+
const tz = timezone || 'UTC';
|
|
40
|
+
const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
|
|
41
|
+
return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseRatesResponse(data) {
|
|
45
|
+
const all = (data.account || {}).gasAgreements || [];
|
|
46
|
+
const agreements = all.filter(a => !((a.tariff || {}).productCode || '').includes('OUTGOING'));
|
|
47
|
+
if (agreements.length === 0) {
|
|
48
|
+
return {
|
|
49
|
+
gas_unit_rate: null, gas_standing_charge: null, gas_tariff_code: null,
|
|
50
|
+
gas_valid_from: null, gas_valid_to: null, gas_rates_error: null
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const latest = agreements[0];
|
|
54
|
+
const tariff = latest.tariff || {};
|
|
55
|
+
return {
|
|
56
|
+
gas_unit_rate: tariff.unitRate ?? null,
|
|
57
|
+
gas_standing_charge: tariff.standingCharge ?? null,
|
|
58
|
+
gas_tariff_code: tariff.productCode || null,
|
|
59
|
+
gas_valid_from: latest.validFrom || null,
|
|
60
|
+
gas_valid_to: latest.validTo || null,
|
|
61
|
+
gas_rates_error: null
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function parseConsumptionResponse(data) {
|
|
66
|
+
const points = ((data.account || {}).properties || [])[0];
|
|
67
|
+
const meterPoint = points ? (points.gasMeterPoints || [])[0] : null;
|
|
68
|
+
const meter = meterPoint ? (meterPoint.meters || [])[0] : null;
|
|
69
|
+
const connection = meter ? meter.consumption : null;
|
|
70
|
+
const edges = connection ? (connection.edges || []) : [];
|
|
71
|
+
const entry = edges.length > 0 ? edges[edges.length - 1].node : null;
|
|
72
|
+
if (!entry) {
|
|
73
|
+
return {
|
|
74
|
+
gas_consumption_kwh: null, gas_consumption_cost: null,
|
|
75
|
+
gas_consumption_from: null, gas_consumption_to: null, gas_consumption_error: null
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
gas_consumption_kwh: parseFloat(entry.value),
|
|
80
|
+
gas_consumption_cost: null,
|
|
81
|
+
gas_consumption_from: entry.startAt,
|
|
82
|
+
gas_consumption_to: entry.endAt,
|
|
83
|
+
gas_consumption_error: null
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const defaultData = {
|
|
88
|
+
gas_unit_rate: null, gas_standing_charge: null, gas_tariff_code: null,
|
|
89
|
+
gas_valid_from: null, gas_valid_to: null, gas_rates_error: null,
|
|
90
|
+
gas_consumption_kwh: null, gas_consumption_cost: null,
|
|
91
|
+
gas_consumption_from: null, gas_consumption_to: null, gas_consumption_error: null
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
module.exports = { buildRatesQuery, buildConsumptionQuery, parseRatesResponse, parseConsumptionResponse, defaultData };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const QUERY = `
|
|
4
|
+
query getSmartMeterTelemetry($deviceId: String!, $start: DateTime!, $end: DateTime!) {
|
|
5
|
+
smartMeterTelemetry(deviceId: $deviceId, grouping: HALF_HOURLY, start: $start, end: $end) {
|
|
6
|
+
readAt demand consumptionDelta
|
|
7
|
+
}
|
|
8
|
+
}`;
|
|
9
|
+
|
|
10
|
+
function buildQuery(smartMeterDeviceId) {
|
|
11
|
+
const end = new Date().toISOString();
|
|
12
|
+
const start = new Date(Date.now() - 30 * 60 * 1000).toISOString();
|
|
13
|
+
return { query: QUERY, variables: { deviceId: smartMeterDeviceId, start, end } };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function parseResponse(data) {
|
|
17
|
+
const readings = data.smartMeterTelemetry || [];
|
|
18
|
+
const latest = readings.length > 0 ? readings[readings.length - 1] : null;
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
// demand is in Watts from API — negative means exporting
|
|
22
|
+
mini_demand_kw: latest && latest.demand != null ? parseFloat(latest.demand) / 1000 : null,
|
|
23
|
+
// consumptionDelta is in Wh per half-hour period
|
|
24
|
+
mini_consumption_delta_kwh: latest && latest.consumptionDelta != null ? parseFloat(latest.consumptionDelta) / 1000 : null,
|
|
25
|
+
mini_read_at: latest ? latest.readAt ?? null : null,
|
|
26
|
+
home_mini_error: null
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultData = {
|
|
31
|
+
mini_demand_kw: null,
|
|
32
|
+
mini_consumption_delta_kwh: null,
|
|
33
|
+
mini_read_at: null,
|
|
34
|
+
home_mini_error: null
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// lib/categories/intelligent.js
|
|
2
|
+
'use strict';
|
|
3
|
+
const { convertToTimezone } = require('../timezone');
|
|
4
|
+
|
|
5
|
+
const WEEKDAYS = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
|
|
6
|
+
|
|
7
|
+
const DEVICES_QUERY = `
|
|
8
|
+
query getDevices($account: String!) {
|
|
9
|
+
devices(accountNumber: $account) {
|
|
10
|
+
id
|
|
11
|
+
deviceType
|
|
12
|
+
preferences {
|
|
13
|
+
... on SmartFlexDevicePreferences {
|
|
14
|
+
schedules {
|
|
15
|
+
dayOfWeek
|
|
16
|
+
time
|
|
17
|
+
max
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}`;
|
|
23
|
+
|
|
24
|
+
const DISPATCH_QUERY = `
|
|
25
|
+
query getDispatches($deviceId: String!) {
|
|
26
|
+
flexPlannedDispatches(deviceId: $deviceId) {
|
|
27
|
+
start end type energyAddedKwh
|
|
28
|
+
}
|
|
29
|
+
}`;
|
|
30
|
+
|
|
31
|
+
function buildDevicesQuery(account) {
|
|
32
|
+
return { query: DEVICES_QUERY, variables: { account } };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function buildDispatchQuery(deviceId) {
|
|
36
|
+
return { query: DISPATCH_QUERY, variables: { deviceId } };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function extractEvDevice(devices) {
|
|
40
|
+
return (devices || []).find(d => d.deviceType === 'ELECTRIC_VEHICLES') || null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function extractPreferences(devices) {
|
|
44
|
+
const ev = extractEvDevice(devices);
|
|
45
|
+
if (!ev || !ev.preferences) return { confirmed_limit: 80, confirmed_time: '07:30' };
|
|
46
|
+
|
|
47
|
+
const schedules = ev.preferences.schedules || [];
|
|
48
|
+
const weekday = schedules.find(s => WEEKDAYS.includes(s.dayOfWeek));
|
|
49
|
+
if (!weekday) return { confirmed_limit: 80, confirmed_time: '07:30' };
|
|
50
|
+
|
|
51
|
+
// time may arrive as 'HH:MM:SS' or 'HH:MM' — normalise to 'HH:MM'
|
|
52
|
+
const time = weekday.time ? weekday.time.substring(0, 5) : '07:30';
|
|
53
|
+
return {
|
|
54
|
+
confirmed_limit: weekday.max ?? 80,
|
|
55
|
+
confirmed_time: time,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// data must be merged result: { devices: [...], flexPlannedDispatches: [...] }
|
|
60
|
+
function parseResponse(data, { tz, serverTz }) {
|
|
61
|
+
const devices = data.devices || [];
|
|
62
|
+
const dispatches = data.flexPlannedDispatches || [];
|
|
63
|
+
const prefs = extractPreferences(devices);
|
|
64
|
+
|
|
65
|
+
const now = new Date();
|
|
66
|
+
const active = dispatches.filter(s => new Date(s.end) > now);
|
|
67
|
+
const nextSlot = active[0] || null;
|
|
68
|
+
const totalEnergy = active.reduce((sum, s) => sum + (s.energyAddedKwh || 0), 0);
|
|
69
|
+
|
|
70
|
+
function conv(dt) { return dt ? convertToTimezone(dt, tz) : null; }
|
|
71
|
+
function locale(dt) { return dt ? convertToTimezone(dt, serverTz) : null; }
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
confirmed_limit: prefs.confirmed_limit,
|
|
75
|
+
confirmed_time: prefs.confirmed_time,
|
|
76
|
+
next_start: nextSlot ? conv(nextSlot.start) : null,
|
|
77
|
+
next_start_raw: nextSlot ? nextSlot.start : null,
|
|
78
|
+
next_start_locale: nextSlot ? locale(nextSlot.start) : null,
|
|
79
|
+
total_energy: parseFloat(totalEnergy.toFixed(2)),
|
|
80
|
+
next_kwh: nextSlot ? (nextSlot.energyAddedKwh != null ? String(parseFloat(nextSlot.energyAddedKwh.toFixed(2))) : '0') : '0',
|
|
81
|
+
next_source: nextSlot ? (nextSlot.type || 'unknown').toLowerCase() : 'unknown',
|
|
82
|
+
slot1_start: active[0] ? conv(active[0].start) : null,
|
|
83
|
+
slot1_end: active[0] ? conv(active[0].end) : null,
|
|
84
|
+
slot2_start: active[1] ? conv(active[1].start) : null,
|
|
85
|
+
slot2_end: active[1] ? conv(active[1].end) : null,
|
|
86
|
+
slot3_start: active[2] ? conv(active[2].start) : null,
|
|
87
|
+
slot3_end: active[2] ? conv(active[2].end) : null,
|
|
88
|
+
window_start: active.length > 0 ? conv(active[0].start) : null,
|
|
89
|
+
window_end: active.length > 0 ? conv(active[active.length - 1].end) : null,
|
|
90
|
+
slot1_start_raw: active[0] ? active[0].start : null,
|
|
91
|
+
slot1_end_raw: active[0] ? active[0].end : null,
|
|
92
|
+
slot2_start_raw: active[1] ? active[1].start : null,
|
|
93
|
+
slot2_end_raw: active[1] ? active[1].end : null,
|
|
94
|
+
slot3_start_raw: active[2] ? active[2].start : null,
|
|
95
|
+
slot3_end_raw: active[2] ? active[2].end : null,
|
|
96
|
+
window_start_raw: active.length > 0 ? active[0].start : null,
|
|
97
|
+
window_end_raw: active.length > 0 ? active[active.length - 1].end : null,
|
|
98
|
+
slot1_start_locale: active[0] ? locale(active[0].start) : null,
|
|
99
|
+
slot1_end_locale: active[0] ? locale(active[0].end) : null,
|
|
100
|
+
slot2_start_locale: active[1] ? locale(active[1].start) : null,
|
|
101
|
+
slot2_end_locale: active[1] ? locale(active[1].end) : null,
|
|
102
|
+
slot3_start_locale: active[2] ? locale(active[2].start) : null,
|
|
103
|
+
slot3_end_locale: active[2] ? locale(active[2].end) : null,
|
|
104
|
+
window_start_locale: active.length > 0 ? locale(active[0].start) : null,
|
|
105
|
+
window_end_locale: active.length > 0 ? locale(active[active.length - 1].end) : null,
|
|
106
|
+
_activeSlots: active
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const defaultData = {
|
|
111
|
+
confirmed_limit: 80, confirmed_time: '07:30',
|
|
112
|
+
next_start: null, next_start_raw: null, next_start_locale: null,
|
|
113
|
+
total_energy: 0, next_kwh: '0', next_source: 'unknown',
|
|
114
|
+
slot1_start: null, slot1_end: null, slot2_start: null, slot2_end: null,
|
|
115
|
+
slot3_start: null, slot3_end: null, window_start: null, window_end: null,
|
|
116
|
+
slot1_start_raw: null, slot1_end_raw: null, slot2_start_raw: null, slot2_end_raw: null,
|
|
117
|
+
slot3_start_raw: null, slot3_end_raw: null, window_start_raw: null, window_end_raw: null,
|
|
118
|
+
slot1_start_locale: null, slot1_end_locale: null, slot2_start_locale: null, slot2_end_locale: null,
|
|
119
|
+
slot3_start_locale: null, slot3_end_locale: null, window_start_locale: null, window_end_locale: null,
|
|
120
|
+
_activeSlots: []
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
module.exports = { buildDevicesQuery, buildDispatchQuery, extractEvDevice, parseResponse, defaultData };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// lib/categories/octoplus.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const QUERY = `
|
|
5
|
+
query getOctoplusInfo($account: String!) {
|
|
6
|
+
octoplusAccountInfo(accountNumber: $account) {
|
|
7
|
+
isOctoplusEnrolled enrollmentStatus isLoyaltyPointsUser
|
|
8
|
+
}
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
function buildQuery(account) {
|
|
12
|
+
return { query: QUERY, variables: { account } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseResponse(data) {
|
|
16
|
+
const info = data.octoplusAccountInfo || {};
|
|
17
|
+
return {
|
|
18
|
+
octoplus_enrolled: info.isOctoplusEnrolled ?? null,
|
|
19
|
+
octoplus_enrollment_status: info.enrollmentStatus || null,
|
|
20
|
+
octoplus_loyalty_points_user: info.isLoyaltyPointsUser ?? null,
|
|
21
|
+
octoplus_error: null
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const defaultData = {
|
|
26
|
+
octoplus_enrolled: null,
|
|
27
|
+
octoplus_enrollment_status: null,
|
|
28
|
+
octoplus_loyalty_points_user: null,
|
|
29
|
+
octoplus_error: null
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// savingSessions moved to api.backend.octopus.energy — the field was removed
|
|
4
|
+
// from api.octopus.energy. Requires User-Agent header (set in lib/graphql.js)
|
|
5
|
+
// to avoid edge 403 on the backend endpoint.
|
|
6
|
+
// octopoints removed from SavingSessionsAccountType — fetch via octoplus category instead.
|
|
7
|
+
const QUERY = `
|
|
8
|
+
query getSavingSessions($account: String!) {
|
|
9
|
+
savingSessions {
|
|
10
|
+
events(includeDev: false) {
|
|
11
|
+
id code rewardPerKwhInOctoPoints startAt endAt
|
|
12
|
+
}
|
|
13
|
+
account(accountNumber: $account) {
|
|
14
|
+
hasJoinedCampaign
|
|
15
|
+
joinedEvents { eventId startAt endAt rewardGivenInOctoPoints }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}`;
|
|
19
|
+
|
|
20
|
+
function buildQuery(account) {
|
|
21
|
+
return {
|
|
22
|
+
query: QUERY,
|
|
23
|
+
variables: { account },
|
|
24
|
+
hostname: 'api.backend.octopus.energy'
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseResponse(data) {
|
|
29
|
+
const ss = data.savingSessions || {};
|
|
30
|
+
const acct = ss.account || {};
|
|
31
|
+
const events = ss.events || [];
|
|
32
|
+
const nextEvent = events[0] || null;
|
|
33
|
+
const joinedIds = (acct.joinedEvents || []).map(e => e.eventId);
|
|
34
|
+
const isJoined = nextEvent ? joinedIds.includes(nextEvent.id) : false;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
saving_session_available: events.length > 0,
|
|
38
|
+
saving_session_start: nextEvent ? nextEvent.startAt : null,
|
|
39
|
+
saving_session_end: nextEvent ? nextEvent.endAt : null,
|
|
40
|
+
saving_session_joined: isJoined,
|
|
41
|
+
saving_sessions_error: null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const defaultData = {
|
|
46
|
+
saving_session_available: null, saving_session_start: null, saving_session_end: null,
|
|
47
|
+
saving_session_joined: null, saving_sessions_error: null
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// wheelOfFortuneSpinsAllowed is the non-deprecated successor to wheelOfFortuneSpins.
|
|
4
|
+
// It lives on api.backend.octopus.energy (requires User-Agent header — handled in
|
|
5
|
+
// lib/graphql.js). The maxSpinsPerMonth / usedSpinsThisMonth fields from the old
|
|
6
|
+
// query were not migrated and return null.
|
|
7
|
+
const QUERY = `
|
|
8
|
+
query getWheelOfFortuneSpinsAllowed($accountNumber: String!) {
|
|
9
|
+
electricity: wheelOfFortuneSpinsAllowed(fuelType: ELECTRICITY, accountNumber: $accountNumber) {
|
|
10
|
+
spinsAllowed
|
|
11
|
+
}
|
|
12
|
+
gas: wheelOfFortuneSpinsAllowed(fuelType: GAS, accountNumber: $accountNumber) {
|
|
13
|
+
spinsAllowed
|
|
14
|
+
}
|
|
15
|
+
}`;
|
|
16
|
+
|
|
17
|
+
function buildQuery(account) {
|
|
18
|
+
return {
|
|
19
|
+
query: QUERY,
|
|
20
|
+
variables: { accountNumber: account },
|
|
21
|
+
hostname: 'api.backend.octopus.energy'
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parseResponse(data) {
|
|
26
|
+
const elec = data.electricity || {};
|
|
27
|
+
const gas = data.gas || {};
|
|
28
|
+
return {
|
|
29
|
+
wheel_of_fortune_electricity_spins: elec.spinsAllowed ?? 0,
|
|
30
|
+
wheel_of_fortune_electricity_max: null,
|
|
31
|
+
wheel_of_fortune_electricity_used: null,
|
|
32
|
+
wheel_of_fortune_gas_spins: gas.spinsAllowed ?? 0,
|
|
33
|
+
wheel_of_fortune_gas_max: null,
|
|
34
|
+
wheel_of_fortune_gas_used: null,
|
|
35
|
+
wheel_of_fortune_error: null
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const defaultData = {
|
|
40
|
+
wheel_of_fortune_electricity_spins: null,
|
|
41
|
+
wheel_of_fortune_electricity_max: null,
|
|
42
|
+
wheel_of_fortune_electricity_used: null,
|
|
43
|
+
wheel_of_fortune_gas_spins: null,
|
|
44
|
+
wheel_of_fortune_gas_max: null,
|
|
45
|
+
wheel_of_fortune_gas_used: null,
|
|
46
|
+
wheel_of_fortune_error: null
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|