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.
Files changed (47) hide show
  1. package/CHANGELOG.md +597 -442
  2. package/README.md +1234 -980
  3. package/examples/basic-monitoring.json +1 -1
  4. package/examples/boost-on-cheap-or-surplus.json +171 -0
  5. package/examples/charging-and-error-alerts.json +95 -0
  6. package/examples/cheapest-window-planner.json +165 -0
  7. package/examples/daily-charging-summary.json +165 -0
  8. package/examples/ha-dashboard.yaml +667 -599
  9. package/examples/mqtt-integration.json +1 -1
  10. package/examples/node-red-dashboard-flow.json +1377 -1199
  11. package/examples/set-preferences.json +1 -1
  12. package/lib/active-now.js +33 -0
  13. package/lib/api-metrics.js +31 -31
  14. package/lib/auth.js +25 -20
  15. package/lib/boost.js +85 -0
  16. package/lib/categories/account.js +32 -32
  17. package/lib/categories/applicable-rates.js +137 -127
  18. package/lib/categories/completed-dispatches.js +36 -35
  19. package/lib/categories/electricity.js +156 -146
  20. package/lib/categories/flex-planned-dispatches.js +36 -36
  21. package/lib/categories/free-electricity.js +73 -61
  22. package/lib/categories/gas.js +102 -95
  23. package/lib/categories/home-mini.js +45 -37
  24. package/lib/categories/intelligent.js +221 -124
  25. package/lib/categories/octoplus.js +35 -32
  26. package/lib/categories/saving-sessions.js +78 -64
  27. package/lib/categories/smartflex-inverter.js +95 -0
  28. package/lib/categories/smartflex-vehicle.js +198 -0
  29. package/lib/categories/wheel-of-fortune.js +50 -49
  30. package/lib/commands.js +69 -0
  31. package/lib/discovery-retry.js +16 -0
  32. package/lib/discovery.js +154 -85
  33. package/lib/graphql-errors.js +174 -0
  34. package/lib/graphql.js +141 -44
  35. package/lib/http-get-json.js +80 -22
  36. package/lib/payload.js +104 -88
  37. package/lib/poll-retry.js +71 -0
  38. package/lib/poll-tick.js +112 -44
  39. package/lib/preferences.js +34 -0
  40. package/lib/scheduler.js +85 -47
  41. package/lib/sensor-definitions.js +269 -0
  42. package/lib/timezone.js +67 -49
  43. package/lib/update-check.js +38 -38
  44. package/lib/values.js +47 -0
  45. package/octopus-intelligent.html +258 -205
  46. package/octopus-intelligent.js +2816 -1807
  47. package/package.json +54 -54
@@ -1,32 +1,35 @@
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 };
1
+ // lib/categories/octoplus.js
2
+ 'use strict';
3
+ const { capStrings } = require('../values');
4
+
5
+ const QUERY = `
6
+ query getOctoplusInfo($account: String!) {
7
+ octoplusAccountInfo(accountNumber: $account) {
8
+ enrollmentStatus isLoyaltyPointsUser
9
+ }
10
+ }`;
11
+
12
+ function buildQuery(account) {
13
+ return { query: QUERY, variables: { account } };
14
+ }
15
+
16
+ function parseResponse(data) {
17
+ const info = data.octoplusAccountInfo || {};
18
+ return {
19
+ // isOctoplusEnrolled is deprecated by Kraken, so derive it from enrollmentStatus.
20
+ octoplus_enrolled: info.enrollmentStatus ? info.enrollmentStatus === 'ENROLLED' : null,
21
+ octoplus_enrollment_status: info.enrollmentStatus || null,
22
+ octoplus_loyalty_points_user: info.isLoyaltyPointsUser ?? null,
23
+ octoplus_error: null
24
+ };
25
+ }
26
+
27
+ const defaultData = {
28
+ octoplus_enrolled: null,
29
+ octoplus_enrollment_status: null,
30
+ octoplus_loyalty_points_user: null,
31
+ octoplus_error: null
32
+ };
33
+
34
+ // Text fields cut to the HA state limit (lib/values.js).
35
+ module.exports = { buildQuery, parseResponse: capStrings(parseResponse), defaultData };
@@ -1,64 +1,78 @@
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 now = new Date();
30
- const ss = data.savingSessions || {};
31
- const acct = ss.account || {};
32
- const events = ss.events || [];
33
-
34
- // Filter to events not yet ended; sort earliest-first; take the first
35
- const upcoming = events
36
- .filter(e => new Date(e.endAt) > now)
37
- .sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
38
- const next = upcoming[0] || null;
39
-
40
- const joinedIds = (acct.joinedEvents || []).map(e => e.eventId);
41
- const isJoined = next ? joinedIds.includes(next.id) : false;
42
-
43
- const inWindow = next
44
- && new Date(next.startAt) <= now
45
- && new Date(next.endAt) > now;
46
-
47
- return {
48
- saving_session_available: next !== null,
49
- saving_session_start: next ? next.startAt : null,
50
- saving_session_end: next ? next.endAt : null,
51
- saving_session_joined: isJoined,
52
- saving_session_window_active: !!inWindow,
53
- saving_session_active: !!(inWindow && isJoined),
54
- saving_sessions_error: null
55
- };
56
- }
57
-
58
- const defaultData = {
59
- saving_session_available: null, saving_session_start: null, saving_session_end: null,
60
- saving_session_joined: null, saving_session_window_active: null, saving_session_active: null,
61
- saving_sessions_error: null
62
- };
63
-
64
- module.exports = { buildQuery, parseResponse, defaultData };
1
+ 'use strict';
2
+ const { capStrings } = require('../values');
3
+
4
+ // savingSessions moved to api.backend.octopus.energy — the field was removed
5
+ // from api.octopus.energy. Requires User-Agent header (set in lib/graphql.js)
6
+ // to avoid edge 403 on the backend endpoint.
7
+ // octopoints removed from SavingSessionsAccountType — fetch via octoplus category instead.
8
+ const QUERY = `
9
+ query getSavingSessions($account: String!) {
10
+ savingSessions {
11
+ events(includeDev: false) {
12
+ id code rewardPerKwhInOctoPoints startAt endAt
13
+ }
14
+ account(accountNumber: $account) {
15
+ hasJoinedCampaign
16
+ joinedEvents { eventId startAt endAt rewardGivenInOctoPoints }
17
+ }
18
+ }
19
+ }`;
20
+
21
+ function buildQuery(account) {
22
+ return {
23
+ query: QUERY,
24
+ variables: { account },
25
+ hostname: 'api.backend.octopus.energy'
26
+ };
27
+ }
28
+
29
+ function parseResponse(data) {
30
+ const now = new Date();
31
+ const ss = data.savingSessions || {};
32
+ const acct = ss.account || {};
33
+ const events = ss.events || [];
34
+
35
+ // Filter to events not yet ended; sort earliest-first; take the first
36
+ const upcoming = events
37
+ .filter(e => new Date(e.endAt) > now)
38
+ .sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
39
+ const next = upcoming[0] || null;
40
+
41
+ const joinedIds = (acct.joinedEvents || []).map(e => e.eventId);
42
+ const isJoined = next ? joinedIds.includes(next.id) : false;
43
+
44
+ const inWindow = next
45
+ && new Date(next.startAt) <= now
46
+ && new Date(next.endAt) > now;
47
+
48
+ return {
49
+ saving_session_available: next !== null,
50
+ saving_session_start: next ? next.startAt : null,
51
+ saving_session_end: next ? next.endAt : null,
52
+ saving_session_joined: isJoined,
53
+ saving_session_window_active: !!inWindow,
54
+ saving_session_active: !!(inWindow && isJoined),
55
+ saving_sessions_error: null
56
+ };
57
+ }
58
+
59
+ // Window/active flags for `nowMs` from the stored next session (state from parseResponse).
60
+ // Used on the node's 10 s tick so they switch at the session start/end, not at the next poll.
61
+ function activeFields(state, nowMs) {
62
+ if (!state.saving_session_start || !state.saving_session_end) return {};
63
+ const inWindow = new Date(state.saving_session_start).getTime() <= nowMs
64
+ && new Date(state.saving_session_end).getTime() > nowMs;
65
+ return {
66
+ saving_session_window_active: inWindow,
67
+ saving_session_active: inWindow && state.saving_session_joined === true,
68
+ };
69
+ }
70
+
71
+ const defaultData = {
72
+ saving_session_available: null, saving_session_start: null, saving_session_end: null,
73
+ saving_session_joined: null, saving_session_window_active: null, saving_session_active: null,
74
+ saving_sessions_error: null
75
+ };
76
+
77
+ // Text fields cut to the HA state limit (lib/values.js).
78
+ module.exports = { buildQuery, parseResponse: capStrings(parseResponse), activeFields, defaultData };
@@ -0,0 +1,95 @@
1
+ // lib/categories/smartflex-inverter.js
2
+ 'use strict';
3
+ const { toNumber, capStrings } = require('../values');
4
+
5
+ // v1.7.0 EXPERIMENTAL: SmartFlex inverter details and live telemetry.
6
+ // Field names come from live schema introspection (6 Oct 2026), but no account we can
7
+ // test has an inverter, so the response shape is NOT live-verified. This category is
8
+ // off by default and only runs when the editor option is ticked.
9
+ // Kraken sends Decimal fields as strings, so every number goes through toNumber().
10
+
11
+ const QUERY = `
12
+ query smartflexInverter($account: String!) {
13
+ devices(accountNumber: $account) {
14
+ __typename
15
+ id
16
+ ... on SmartFlexInverter {
17
+ make model
18
+ details { solar { peakSolarPowerInKw } }
19
+ telemetry {
20
+ inverter { connectionStatus powerInKw }
21
+ battery { connectionStatus powerInKw stateOfCharge }
22
+ solar { powerInKw }
23
+ grid { powerInKw }
24
+ site { consumptionInKw exportInKw }
25
+ }
26
+ }
27
+ }
28
+ }`;
29
+
30
+ // Connection status values treated as online. The enum has not been seen live.
31
+ const ONLINE_STATES = ['CONNECTED', 'ONLINE'];
32
+
33
+ function buildQuery(account) {
34
+ return { query: QUERY, variables: { account } };
35
+ }
36
+
37
+ // The schema may return a single reading or a list of readings; use the latest.
38
+ function latest(x) {
39
+ if (Array.isArray(x)) return x.length ? (x[x.length - 1] || {}) : {};
40
+ return x || {};
41
+ }
42
+
43
+ function pickDevice(devices, deviceId) {
44
+ return devices.find(d => d && deviceId && d.id === deviceId)
45
+ || devices.find(d => d && d.__typename === 'SmartFlexInverter')
46
+ || null;
47
+ }
48
+
49
+ function parseResponse(data, deviceId) {
50
+ const device = pickDevice((data && data.devices) || [], deviceId);
51
+ if (!device) return { ...defaultData };
52
+
53
+ const details = latest(device.details);
54
+ const telemetry = latest(device.telemetry);
55
+ const inv = latest(telemetry.inverter);
56
+ const battery = latest(telemetry.battery);
57
+ const solar = latest(telemetry.solar);
58
+ const grid = latest(telemetry.grid);
59
+ const site = latest(telemetry.site);
60
+ const connection = inv.connectionStatus || null;
61
+
62
+ return {
63
+ inverter_make: device.make || null,
64
+ inverter_model: device.model || null,
65
+ solar_peak_kw: toNumber(latest(details.solar).peakSolarPowerInKw),
66
+ inverter_power_kw: toNumber(inv.powerInKw),
67
+ inverter_connection_status: connection,
68
+ inverter_online: connection === null ? null : ONLINE_STATES.includes(connection),
69
+ home_battery_power_kw: toNumber(battery.powerInKw),
70
+ home_battery_soc: toNumber(battery.stateOfCharge),
71
+ solar_power_kw: toNumber(solar.powerInKw),
72
+ grid_power_kw: toNumber(grid.powerInKw),
73
+ site_consumption_kw: toNumber(site.consumptionInKw),
74
+ site_export_kw: toNumber(site.exportInKw),
75
+ smartflex_inverter_error: null,
76
+ };
77
+ }
78
+
79
+ const defaultData = {
80
+ inverter_make: null,
81
+ inverter_model: null,
82
+ solar_peak_kw: null,
83
+ inverter_power_kw: null,
84
+ inverter_connection_status: null,
85
+ inverter_online: null,
86
+ home_battery_power_kw: null,
87
+ home_battery_soc: null,
88
+ solar_power_kw: null,
89
+ grid_power_kw: null,
90
+ site_consumption_kw: null,
91
+ site_export_kw: null,
92
+ smartflex_inverter_error: null,
93
+ };
94
+
95
+ module.exports = { buildQuery, parseResponse: capStrings(parseResponse), defaultData };
@@ -0,0 +1,198 @@
1
+ // lib/categories/smartflex-vehicle.js
2
+ 'use strict';
3
+ const { toNumber, capStrings } = require('../values');
4
+
5
+ // v1.7.0: SmartFlex vehicle / charge point details, alerts and the last 5 charging
6
+ // sessions. Field names verified by live introspection (6 Oct 2026).
7
+ // Kraken sends Decimal fields as strings, so every number goes through toNumber().
8
+ // device_state is NOT read here: status is non-null on the device, so an error on it
9
+ // (KT-CT-4360 has been seen) nulls the device and then the whole list. The charging poll
10
+ // already reads status under a tolerated alias every refresh interval and sets device_state.
11
+ // A charge point is listed as ELECTRIC_VEHICLES too, so it always has a charging poll.
12
+
13
+ const { graphqlError, truncate } = require('../graphql-errors');
14
+
15
+ const SESSION_FIELDS = `
16
+ chargingSessions(last: 5) {
17
+ edges { node { ... on SmartFlexChargingSession {
18
+ start end stateOfChargeChange stateOfChargeFinal
19
+ energyAdded { value unit }
20
+ cost { amount currency }
21
+ type
22
+ problems { __typename }
23
+ } } }
24
+ }`;
25
+
26
+ const QUERY = `
27
+ query smartflexVehicle($account: String!) {
28
+ devices(accountNumber: $account) {
29
+ __typename
30
+ id
31
+ deviceType
32
+ alerts { message publishedAt }
33
+ ... on SmartFlexVehicle {
34
+ make model vehicleBatterySize chargePointPowerOutput${SESSION_FIELDS}
35
+ }
36
+ ... on SmartFlexChargePoint {
37
+ make model vehicleBatterySize chargePointPowerOutput${SESSION_FIELDS}
38
+ }
39
+ }
40
+ }`;
41
+
42
+ const VEHICLE_TYPENAMES = ['SmartFlexVehicle', 'SmartFlexChargePoint'];
43
+
44
+ function buildQuery(account) {
45
+ return { query: QUERY, variables: { account } };
46
+ }
47
+
48
+ // energyAdded comes with its unit. KILOWATT_HOUR is what Octopus sends today; WATT_HOUR is
49
+ // converted so kwh_added (and the kWh sensor) is never out by 1000.
50
+ function energyKwh(energy) {
51
+ if (!energy) return null;
52
+ const value = toNumber(energy.value);
53
+ if (value === null) return null;
54
+ return energy.unit === 'WATT_HOUR' ? value / 1000 : value;
55
+ }
56
+
57
+ function mapSession(n) {
58
+ return {
59
+ start: n.start || null,
60
+ end: n.end || null,
61
+ kwh_added: energyKwh(n.energyAdded),
62
+ soc_change: toNumber(n.stateOfChargeChange),
63
+ soc_final: toNumber(n.stateOfChargeFinal),
64
+ type: n.type || null,
65
+ cost: n.cost ? { amount: toNumber(n.cost.amount), currency: n.cost.currency || null } : null,
66
+ problems: (n.problems || []).filter(Boolean).map(p => p.__typename),
67
+ };
68
+ }
69
+
70
+ function startTime(s) {
71
+ const t = Date.parse(s.start);
72
+ return Number.isNaN(t) ? -Infinity : t;
73
+ }
74
+
75
+ // Alerts go into the retained state JSON and into Home Assistant attributes (16 KiB recorder
76
+ // limit), so keep only the newest few and cut long messages.
77
+ const MAX_ALERTS = 10;
78
+ const MAX_ALERT_MESSAGE = 250;
79
+
80
+ function alertTime(a) {
81
+ const t = Date.parse(a.published_at);
82
+ return Number.isNaN(t) ? -Infinity : t;
83
+ }
84
+
85
+ function mapAlerts(alerts) {
86
+ return (alerts || [])
87
+ .filter(Boolean)
88
+ .map(a => ({
89
+ message: a.message ? truncate(a.message, MAX_ALERT_MESSAGE) : null,
90
+ published_at: a.publishedAt || null,
91
+ }))
92
+ .sort((a, b) => alertTime(b) - alertTime(a))
93
+ .slice(0, MAX_ALERTS);
94
+ }
95
+
96
+ function pickDevice(devices, deviceId) {
97
+ return devices.find(d => d && deviceId && d.id === deviceId)
98
+ || devices.find(d => d && VEHICLE_TYPENAMES.includes(d.__typename))
99
+ || null;
100
+ }
101
+
102
+ function freshDefaults() {
103
+ return { ...defaultData, device_alerts: [], charging_sessions: [] };
104
+ }
105
+
106
+ function parseResponse(data, deviceId) {
107
+ const device = pickDevice((data && data.devices) || [], deviceId);
108
+ if (!device) return freshDefaults();
109
+
110
+ const edges = (device.chargingSessions && device.chargingSessions.edges) || [];
111
+ const sessions = edges
112
+ .map(e => e && e.node)
113
+ .filter(n => n && n.start)
114
+ .map(mapSession)
115
+ .sort((a, b) => startTime(b) - startTime(a));
116
+ const last = sessions.find(s => s.end) || null;
117
+
118
+ return {
119
+ vehicle_make: device.make || null,
120
+ vehicle_model: device.model || null,
121
+ vehicle_battery_kwh: toNumber(device.vehicleBatterySize),
122
+ charger_power_kw: toNumber(device.chargePointPowerOutput),
123
+ device_alerts: mapAlerts(device.alerts),
124
+ last_session: last,
125
+ charging_sessions: sessions,
126
+ last_session_kwh: last ? last.kwh_added : null,
127
+ last_session_soc_final: last ? last.soc_final : null,
128
+ last_session_problem: last ? last.problems.length > 0 : null,
129
+ smartflex_vehicle_error: null,
130
+ };
131
+ }
132
+
133
+ // Device fields of the query and the payload fields built from each. When Kraken fails one
134
+ // (a partial error nulls it), its payload fields are left out so the last values are kept.
135
+ const FIELD_OUTPUTS = {
136
+ make: ['vehicle_make'],
137
+ model: ['vehicle_model'],
138
+ vehicleBatterySize: ['vehicle_battery_kwh'],
139
+ chargePointPowerOutput: ['charger_power_kw'],
140
+ alerts: ['device_alerts'],
141
+ chargingSessions: ['charging_sessions', 'last_session', 'last_session_kwh', 'last_session_soc_final', 'last_session_problem'],
142
+ };
143
+
144
+ // Parses the whole GraphQL body ({ data, errors }) and tolerates partial errors: an error on
145
+ // one field of the vehicle (e.g. chargingSessions) keeps everything else, leaves that field's
146
+ // payload fields out (the merge keeps the last values) and sets a short "KT-CT-xxxx: message"
147
+ // smartflex_vehicle_error. Errors on other devices are ignored. Throws when there is no
148
+ // usable vehicle data at all.
149
+ function parseGraphqlResponse(body, deviceId) {
150
+ const errors = Array.isArray(body && body.errors) && body.errors.length ? body.errors : null;
151
+ const data = (body && body.data) || null;
152
+ if (!data) {
153
+ if (errors) throw graphqlError(errors, 'SmartFlex vehicle query failed');
154
+ throw new Error('Response missing data');
155
+ }
156
+ if (!errors) return parseResponse(data, deviceId);
157
+
158
+ const devices = Array.isArray(data.devices) ? data.devices : null;
159
+ // With errors, the discovered device must be the one in the list: when the error nulled
160
+ // it, falling back to another vehicle would show that vehicle's data as this one's.
161
+ // Throwing keeps the last values.
162
+ const device = !devices ? null
163
+ : deviceId ? (devices.find(d => d && d.id === deviceId) || null)
164
+ : pickDevice(devices, null);
165
+ if (!device) throw graphqlError(errors, 'SmartFlex vehicle query failed');
166
+
167
+ const index = devices.indexOf(device);
168
+ const relevant = errors.filter((e) => !(Array.isArray(e && e.path) && e.path[0] === 'devices'
169
+ && Number.isInteger(e.path[1]) && e.path[1] !== index));
170
+ const result = parseResponse(data, deviceId);
171
+ if (relevant.length === 0) return result;
172
+
173
+ for (const e of relevant) {
174
+ const field = Array.isArray(e && e.path) && e.path[1] === index ? e.path[2] : null;
175
+ // A field that still came back (the error was deeper) is used as it is.
176
+ if (field && FIELD_OUTPUTS[field] && (device[field] === null || device[field] === undefined)) {
177
+ for (const k of FIELD_OUTPUTS[field]) delete result[k];
178
+ }
179
+ }
180
+ result.smartflex_vehicle_error = graphqlError(relevant, 'SmartFlex vehicle query failed').message;
181
+ return result;
182
+ }
183
+
184
+ const defaultData = {
185
+ vehicle_make: null,
186
+ vehicle_model: null,
187
+ vehicle_battery_kwh: null,
188
+ charger_power_kw: null,
189
+ device_alerts: [],
190
+ last_session: null,
191
+ charging_sessions: [],
192
+ last_session_kwh: null,
193
+ last_session_soc_final: null,
194
+ last_session_problem: null,
195
+ smartflex_vehicle_error: null,
196
+ };
197
+
198
+ module.exports = { buildQuery, parseResponse: capStrings(parseResponse), parseGraphqlResponse: capStrings(parseGraphqlResponse), defaultData };
@@ -1,49 +1,50 @@
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 };
1
+ 'use strict';
2
+ const { toNumber } = require('../values');
3
+
4
+ // wheelOfFortuneSpinsAllowed is the non-deprecated successor to wheelOfFortuneSpins.
5
+ // It lives on api.backend.octopus.energy (requires User-Agent header — handled in
6
+ // lib/graphql.js). The maxSpinsPerMonth / usedSpinsThisMonth fields from the old
7
+ // query were not migrated and return null.
8
+ const QUERY = `
9
+ query getWheelOfFortuneSpinsAllowed($accountNumber: String!) {
10
+ electricity: wheelOfFortuneSpinsAllowed(fuelType: ELECTRICITY, accountNumber: $accountNumber) {
11
+ spinsAllowed
12
+ }
13
+ gas: wheelOfFortuneSpinsAllowed(fuelType: GAS, accountNumber: $accountNumber) {
14
+ spinsAllowed
15
+ }
16
+ }`;
17
+
18
+ function buildQuery(account) {
19
+ return {
20
+ query: QUERY,
21
+ variables: { accountNumber: account },
22
+ hostname: 'api.backend.octopus.energy'
23
+ };
24
+ }
25
+
26
+ function parseResponse(data) {
27
+ const elec = data.electricity || {};
28
+ const gas = data.gas || {};
29
+ return {
30
+ wheel_of_fortune_electricity_spins: toNumber(elec.spinsAllowed) ?? 0,
31
+ wheel_of_fortune_electricity_max: null,
32
+ wheel_of_fortune_electricity_used: null,
33
+ wheel_of_fortune_gas_spins: toNumber(gas.spinsAllowed) ?? 0,
34
+ wheel_of_fortune_gas_max: null,
35
+ wheel_of_fortune_gas_used: null,
36
+ wheel_of_fortune_error: null
37
+ };
38
+ }
39
+
40
+ const defaultData = {
41
+ wheel_of_fortune_electricity_spins: null,
42
+ wheel_of_fortune_electricity_max: null,
43
+ wheel_of_fortune_electricity_used: null,
44
+ wheel_of_fortune_gas_spins: null,
45
+ wheel_of_fortune_gas_max: null,
46
+ wheel_of_fortune_gas_used: null,
47
+ wheel_of_fortune_error: null
48
+ };
49
+
50
+ module.exports = { buildQuery, parseResponse, defaultData };