node-red-contrib-octopus-intelligent 1.2.1 → 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.
@@ -0,0 +1,470 @@
1
+ type: sections
2
+ title: Octopus Intelligent
3
+ path: octopus-intelligent
4
+ icon: mdi:lightning-bolt
5
+ max_columns: 4
6
+ # ────────────────────────────────────────────────────────────────────────────────
7
+ # Octopus Intelligent — comprehensive v1.3.0 dashboard
8
+ #
9
+ # Each section is a single focused card so the sections view packs them into a
10
+ # uniform grid without big empty gaps. On wide screens HA renders up to 4
11
+ # columns; narrower viewports collapse to fewer columns automatically.
12
+ #
13
+ # Covers every MQTT entity published by node-red-contrib-octopus-intelligent
14
+ # v1.3.0. Fresh install entity-id prefix: octopus_intelligent_octopus_*.
15
+ # Upgraders from pre-v1.0 may have node_red_octopus_intelligent_octopus_*
16
+ # on carry-over entities (see Dev Tools → States to look up the actual ID).
17
+ # ────────────────────────────────────────────────────────────────────────────────
18
+ sections:
19
+ # ════════════════════════════════════════════════════════════════════════════
20
+ # EV
21
+ # ════════════════════════════════════════════════════════════════════════════
22
+ - type: grid
23
+ cards:
24
+ - type: entities
25
+ title: EV — controls
26
+ icon: mdi:car-electric
27
+ entities:
28
+ - entity: number.octopus_intelligent_octopus_target_charge
29
+ name: Target Charge Limit
30
+ - entity: select.octopus_intelligent_octopus_ready_time
31
+ name: Ready By
32
+ - entity: button.octopus_intelligent_octopus_apply_changes
33
+ name: Apply Changes
34
+ - entity: switch.octopus_intelligent_smart_charging
35
+ name: Smart Charging
36
+ - entity: button.octopus_intelligent_octopus_refresh_api
37
+ name: Refresh API
38
+
39
+ - type: grid
40
+ cards:
41
+ - type: glance
42
+ title: EV — now
43
+ entities:
44
+ - entity: binary_sensor.octopus_intelligent_octopus_charging_now
45
+ name: Charging
46
+ - entity: sensor.octopus_intelligent_octopus_charge_source
47
+ name: Source
48
+ - entity: sensor.octopus_intelligent_octopus_total_planned_energy
49
+ name: Planned
50
+ - entity: sensor.octopus_intelligent_octopus_next_slot_energy
51
+ name: Next slot
52
+
53
+ - type: grid
54
+ cards:
55
+ - type: entities
56
+ title: EV — confirmed targets
57
+ icon: mdi:check-circle
58
+ entities:
59
+ - entity: sensor.octopus_intelligent_octopus_confirmed_charge_limit
60
+ name: Confirmed limit
61
+ - entity: sensor.octopus_intelligent_octopus_confirmed_ready_time
62
+ name: Confirmed ready time
63
+
64
+ - type: grid
65
+ cards:
66
+ - type: entities
67
+ title: EV — next charge / window
68
+ icon: mdi:timer-outline
69
+ entities:
70
+ - entity: sensor.octopus_intelligent_octopus_next_charge_time_locale
71
+ name: Next charge starts
72
+ - entity: sensor.octopus_intelligent_octopus_overall_window_start_locale
73
+ name: Window opens
74
+ - entity: sensor.octopus_intelligent_octopus_overall_window_end_locale
75
+ name: Window closes
76
+
77
+ - type: grid
78
+ cards:
79
+ - type: entities
80
+ title: EV — planned slots
81
+ icon: mdi:format-list-numbered
82
+ entities:
83
+ - entity: sensor.octopus_intelligent_octopus_slot_1_start_locale
84
+ name: Slot 1 — starts
85
+ - entity: sensor.octopus_intelligent_octopus_slot_1_end_locale
86
+ name: Slot 1 — ends
87
+ - entity: sensor.octopus_intelligent_octopus_slot_2_start_locale
88
+ name: Slot 2 — starts
89
+ - entity: sensor.octopus_intelligent_octopus_slot_2_end_locale
90
+ name: Slot 2 — ends
91
+ - entity: sensor.octopus_intelligent_octopus_slot_3_start_locale
92
+ name: Slot 3 — starts
93
+ - entity: sensor.octopus_intelligent_octopus_slot_3_end_locale
94
+ name: Slot 3 — ends
95
+ - entity: sensor.octopus_intelligent_octopus_flex_planned_dispatches
96
+ name: Planned dispatches
97
+ - entity: sensor.octopus_intelligent_octopus_completed_dispatches
98
+ name: Completed (12h)
99
+
100
+ # ════════════════════════════════════════════════════════════════════════════
101
+ # Electricity — import
102
+ # ════════════════════════════════════════════════════════════════════════════
103
+ - type: grid
104
+ cards:
105
+ - type: glance
106
+ title: Electricity import — live
107
+ entities:
108
+ - entity: sensor.octopus_intelligent_octopus_current_electricity_rate
109
+ name: Rate
110
+ - entity: sensor.octopus_intelligent_octopus_electricity_standing_charge
111
+ name: Standing
112
+ - entity: sensor.octopus_intelligent_octopus_electricity_consumption
113
+ name: Today (kWh)
114
+
115
+ - type: grid
116
+ cards:
117
+ - type: entities
118
+ title: Electricity import — tariff
119
+ icon: mdi:flash
120
+ entities:
121
+ - entity: sensor.octopus_intelligent_octopus_electricity_tariff_code
122
+ name: Tariff code
123
+ - entity: sensor.octopus_intelligent_octopus_electricity_unit_rate
124
+ name: Unit rate (fixed only)
125
+ - entity: sensor.octopus_intelligent_octopus_electricity_tariff_valid_from
126
+ name: Valid from
127
+ - entity: sensor.octopus_intelligent_octopus_electricity_tariff_valid_to
128
+ name: Valid to
129
+
130
+ - type: grid
131
+ cards:
132
+ - type: entities
133
+ title: Electricity import — consumption window
134
+ icon: mdi:calendar-clock
135
+ entities:
136
+ - entity: sensor.octopus_intelligent_octopus_electricity_consumption_from
137
+ name: From
138
+ - entity: sensor.octopus_intelligent_octopus_electricity_consumption_to
139
+ name: To
140
+ - entity: sensor.octopus_intelligent_octopus_electricity_rate
141
+ name: Rate (GBP/kWh — Energy Dashboard)
142
+
143
+ - type: grid
144
+ cards:
145
+ - type: entities
146
+ title: Applicable rates — import
147
+ icon: mdi:cash-clock
148
+ entities:
149
+ - entity: sensor.octopus_intelligent_octopus_current_electricity_rate
150
+ name: Current rate (p/kWh)
151
+ - entity: sensor.octopus_intelligent_octopus_electricity_rate
152
+ name: Current rate (GBP/kWh)
153
+ - entity: sensor.octopus_intelligent_octopus_applicable_rates_slots
154
+ name: Slots received
155
+
156
+ # ════════════════════════════════════════════════════════════════════════════
157
+ # Electricity — export
158
+ # ════════════════════════════════════════════════════════════════════════════
159
+ - type: grid
160
+ cards:
161
+ - type: glance
162
+ title: Electricity export — live
163
+ entities:
164
+ - entity: sensor.octopus_intelligent_octopus_current_electricity_export_rate
165
+ name: Rate
166
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_standing_charge
167
+ name: Standing
168
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_consumption
169
+ name: Today (kWh)
170
+
171
+ - type: grid
172
+ cards:
173
+ - type: entities
174
+ title: Electricity export — tariff
175
+ icon: mdi:transmission-tower-export
176
+ entities:
177
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_tariff_code
178
+ name: Tariff code
179
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_unit_rate
180
+ name: Unit rate (fixed only)
181
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_tariff_valid_from
182
+ name: Valid from
183
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_tariff_valid_to
184
+ name: Valid to
185
+
186
+ - type: grid
187
+ cards:
188
+ - type: entities
189
+ title: Electricity export — consumption window
190
+ icon: mdi:calendar-clock
191
+ entities:
192
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_consumption_from
193
+ name: From
194
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_consumption_to
195
+ name: To
196
+
197
+ - type: grid
198
+ cards:
199
+ - type: entities
200
+ title: Applicable rates — export
201
+ icon: mdi:transmission-tower-export
202
+ entities:
203
+ - entity: sensor.octopus_intelligent_octopus_current_electricity_export_rate
204
+ name: Current rate (p/kWh)
205
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate
206
+ name: Current rate (GBP/kWh)
207
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_slots
208
+ name: Slots received
209
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_prev
210
+ name: Previous rate
211
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_prev_ends
212
+ name: Previous ended
213
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_next
214
+ name: Next rate
215
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_next_from
216
+ name: Next from
217
+
218
+ # ════════════════════════════════════════════════════════════════════════════
219
+ # Gas
220
+ # ════════════════════════════════════════════════════════════════════════════
221
+ - type: grid
222
+ cards:
223
+ - type: glance
224
+ title: Gas — live
225
+ entities:
226
+ - entity: sensor.octopus_intelligent_octopus_gas_unit_rate
227
+ name: Rate
228
+ - entity: sensor.octopus_intelligent_octopus_gas_standing_charge
229
+ name: Standing
230
+ - entity: sensor.octopus_intelligent_octopus_gas_consumption
231
+ name: Today (kWh)
232
+
233
+ - type: grid
234
+ cards:
235
+ - type: entities
236
+ title: Gas — tariff
237
+ icon: mdi:fire
238
+ entities:
239
+ - entity: sensor.octopus_intelligent_octopus_gas_tariff_code
240
+ name: Tariff code
241
+ - entity: sensor.octopus_intelligent_octopus_gas_tariff_valid_from
242
+ name: Valid from
243
+ - entity: sensor.octopus_intelligent_octopus_gas_tariff_valid_to
244
+ name: Valid to
245
+
246
+ - type: grid
247
+ cards:
248
+ - type: entities
249
+ title: Gas — consumption window
250
+ icon: mdi:calendar-clock
251
+ entities:
252
+ - entity: sensor.octopus_intelligent_octopus_gas_consumption_from
253
+ name: From
254
+ - entity: sensor.octopus_intelligent_octopus_gas_consumption_to
255
+ name: To
256
+
257
+ # ════════════════════════════════════════════════════════════════════════════
258
+ # Account / Octoplus / Rewards / Mini
259
+ # ════════════════════════════════════════════════════════════════════════════
260
+ - type: grid
261
+ cards:
262
+ - type: entities
263
+ title: Account
264
+ icon: mdi:account-cash
265
+ entities:
266
+ - entity: sensor.octopus_intelligent_octopus_account_balance
267
+ name: Balance (£)
268
+ - entity: sensor.octopus_intelligent_octopus_account_balance_pence
269
+ name: Balance (pence)
270
+
271
+ - type: grid
272
+ cards:
273
+ - type: entities
274
+ title: Octoplus
275
+ icon: mdi:star-circle
276
+ entities:
277
+ # Enrolled / Loyalty Points are booleans → binary_sensor in v1.3+
278
+ - entity: binary_sensor.octopus_intelligent_octopus_octoplus_enrolled
279
+ name: Enrolled
280
+ - entity: sensor.octopus_intelligent_octopus_octoplus_status
281
+ name: Status
282
+ - entity: binary_sensor.octopus_intelligent_octopus_octoplus_loyalty_points
283
+ name: Loyalty points user
284
+
285
+ - type: grid
286
+ cards:
287
+ - type: entities
288
+ title: Saving Sessions
289
+ icon: mdi:lightning-bolt-circle
290
+ entities:
291
+ - entity: binary_sensor.octopus_intelligent_octopus_saving_session_available
292
+ name: Next session available
293
+ - entity: sensor.octopus_intelligent_octopus_saving_session_start
294
+ name: Next session — starts
295
+ - entity: sensor.octopus_intelligent_octopus_saving_session_end
296
+ name: Next session — ends
297
+ - entity: sensor.octopus_intelligent_octopus_saving_session_joined
298
+ name: Joined
299
+
300
+ - type: grid
301
+ cards:
302
+ - type: entities
303
+ title: Free Electricity
304
+ icon: mdi:flash-circle
305
+ entities:
306
+ - entity: binary_sensor.octopus_intelligent_octopus_free_electricity_active
307
+ name: Active now
308
+ - entity: binary_sensor.octopus_intelligent_octopus_free_electricity_available
309
+ name: Upcoming
310
+ - entity: sensor.octopus_intelligent_octopus_free_electricity_start
311
+ name: Starts
312
+ - entity: sensor.octopus_intelligent_octopus_free_electricity_end
313
+ name: Ends
314
+
315
+ - type: grid
316
+ cards:
317
+ - type: glance
318
+ title: Wheel of Fortune
319
+ entities:
320
+ - entity: sensor.octopus_intelligent_octopus_wheel_of_fortune_electricity_spins
321
+ name: Elec spins
322
+ - entity: sensor.octopus_intelligent_octopus_wheel_of_fortune_gas_spins
323
+ name: Gas spins
324
+
325
+ - type: grid
326
+ cards:
327
+ - type: gauge
328
+ entity: sensor.octopus_intelligent_octopus_home_mini_demand
329
+ name: Home Mini — live demand
330
+ unit: kW
331
+ min: 0
332
+ max: 10
333
+ severity:
334
+ green: 0
335
+ yellow: 3
336
+ red: 6
337
+
338
+ - type: grid
339
+ cards:
340
+ - type: entities
341
+ title: Home Mini — readings
342
+ icon: mdi:home-lightning-bolt
343
+ entities:
344
+ - entity: sensor.octopus_intelligent_octopus_home_mini_period_consumption
345
+ name: Half-hour kWh
346
+ - entity: sensor.octopus_intelligent_octopus_home_mini_reading_time
347
+ name: Last reading
348
+
349
+ # ════════════════════════════════════════════════════════════════════════════
350
+ # Diagnostics — system, errors, raw / UTC timestamps
351
+ # ════════════════════════════════════════════════════════════════════════════
352
+ - type: grid
353
+ cards:
354
+ - type: entities
355
+ title: System
356
+ icon: mdi:cog
357
+ entities:
358
+ - entity: select.octopus_intelligent_timezone
359
+ name: Timezone (override)
360
+ - entity: sensor.octopus_intelligent_octopus_timezone_detected
361
+ name: Timezone — detected
362
+ - entity: sensor.octopus_intelligent_octopus_timezone_applied
363
+ name: Timezone — applied
364
+ - entity: sensor.octopus_intelligent_octopus_next_poll_time
365
+ name: Next poll
366
+ - entity: sensor.octopus_intelligent_octopus_refresh_available_at
367
+ name: Refresh available at
368
+
369
+ - type: grid
370
+ cards:
371
+ - type: entities
372
+ title: API usage
373
+ icon: mdi:api
374
+ entities:
375
+ - entity: sensor.octopus_intelligent_octopus_api_requests_last_hour
376
+ name: Requests (last hour)
377
+ - entity: sensor.octopus_intelligent_octopus_api_complexity_last_hour
378
+ name: Complexity (last hour)
379
+ - entity: sensor.octopus_intelligent_octopus_api_complexity_usage
380
+ name: Complexity vs limit
381
+
382
+ - type: grid
383
+ cards:
384
+ - type: entities
385
+ title: Diagnostic — slot UTC
386
+ icon: mdi:clock-outline
387
+ entities:
388
+ - entity: sensor.octopus_intelligent_octopus_next_charge_time
389
+ name: Next charge
390
+ - entity: sensor.octopus_intelligent_octopus_overall_window_start
391
+ name: Window start
392
+ - entity: sensor.octopus_intelligent_octopus_overall_window_end
393
+ name: Window end
394
+ - entity: sensor.octopus_intelligent_octopus_slot_1_start
395
+ name: Slot 1 start
396
+ - entity: sensor.octopus_intelligent_octopus_slot_1_end
397
+ name: Slot 1 end
398
+ - entity: sensor.octopus_intelligent_octopus_slot_2_start
399
+ name: Slot 2 start
400
+ - entity: sensor.octopus_intelligent_octopus_slot_2_end
401
+ name: Slot 2 end
402
+ - entity: sensor.octopus_intelligent_octopus_slot_3_start
403
+ name: Slot 3 start
404
+ - entity: sensor.octopus_intelligent_octopus_slot_3_end
405
+ name: Slot 3 end
406
+
407
+ - type: grid
408
+ cards:
409
+ - type: entities
410
+ title: Diagnostic — raw API timestamps
411
+ icon: mdi:code-tags
412
+ entities:
413
+ - entity: sensor.octopus_intelligent_octopus_next_charge_time_raw
414
+ name: Next charge
415
+ - entity: sensor.octopus_intelligent_octopus_next_poll_time_raw
416
+ name: Next poll
417
+ - entity: sensor.octopus_intelligent_octopus_overall_window_start_raw
418
+ name: Window start
419
+ - entity: sensor.octopus_intelligent_octopus_overall_window_end_raw
420
+ name: Window end
421
+ - entity: sensor.octopus_intelligent_octopus_slot_1_start_raw
422
+ name: Slot 1 start
423
+ - entity: sensor.octopus_intelligent_octopus_slot_1_end_raw
424
+ name: Slot 1 end
425
+ - entity: sensor.octopus_intelligent_octopus_slot_2_start_raw
426
+ name: Slot 2 start
427
+ - entity: sensor.octopus_intelligent_octopus_slot_2_end_raw
428
+ name: Slot 2 end
429
+ - entity: sensor.octopus_intelligent_octopus_slot_3_start_raw
430
+ name: Slot 3 start
431
+ - entity: sensor.octopus_intelligent_octopus_slot_3_end_raw
432
+ name: Slot 3 end
433
+
434
+ - type: grid
435
+ column_span: 2
436
+ cards:
437
+ - type: entities
438
+ title: Diagnostic — errors
439
+ icon: mdi:alert-circle
440
+ entities:
441
+ - entity: sensor.octopus_intelligent_octopus_electricity_rates_error
442
+ name: Electricity rates
443
+ - entity: sensor.octopus_intelligent_octopus_electricity_consumption_error
444
+ name: Electricity consumption
445
+ - entity: sensor.octopus_intelligent_octopus_gas_rates_error
446
+ name: Gas rates
447
+ - entity: sensor.octopus_intelligent_octopus_gas_consumption_error
448
+ name: Gas consumption
449
+ - entity: sensor.octopus_intelligent_octopus_applicable_rates_error
450
+ name: Applicable rates
451
+ - entity: sensor.octopus_intelligent_octopus_electricity_export_rate_error
452
+ name: Applicable rates — export
453
+ - entity: sensor.octopus_intelligent_octopus_account_error
454
+ name: Account
455
+ - entity: sensor.octopus_intelligent_octopus_octoplus_error
456
+ name: Octoplus
457
+ - entity: sensor.octopus_intelligent_octopus_wof_error
458
+ name: WoF
459
+ - entity: sensor.octopus_intelligent_octopus_home_mini_error
460
+ name: Home Mini
461
+ - entity: sensor.octopus_intelligent_octopus_saving_sessions_error
462
+ name: Saving sessions
463
+ - entity: sensor.octopus_intelligent_octopus_free_electricity_error
464
+ name: Free electricity
465
+ - entity: sensor.octopus_intelligent_octopus_intelligent_error
466
+ name: Intelligent
467
+ - entity: sensor.octopus_intelligent_octopus_completed_dispatches_error
468
+ name: Completed dispatches
469
+ - entity: sensor.octopus_intelligent_octopus_flex_planned_dispatches_error
470
+ name: Flex planned dispatches
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+ const HOURLY_LIMIT = 50000;
3
+ const ONE_HOUR_MS = 60 * 60 * 1000;
4
+
5
+ function createApiMetrics() {
6
+ const history = [];
7
+
8
+ function recordPoll(complexity) {
9
+ history.push({ timestamp: Date.now(), complexity: complexity || 0 });
10
+ pruneOld();
11
+ }
12
+
13
+ function pruneOld() {
14
+ const cutoff = Date.now() - ONE_HOUR_MS;
15
+ while (history.length > 0 && history[0].timestamp < cutoff) history.shift();
16
+ }
17
+
18
+ function getMetrics() {
19
+ pruneOld();
20
+ const total = history.reduce((sum, e) => sum + e.complexity, 0);
21
+ return {
22
+ requests_last_hour: history.length,
23
+ complexity_last_hour: total,
24
+ complexity_percent: ((total / HOURLY_LIMIT) * 100).toFixed(1)
25
+ };
26
+ }
27
+
28
+ return { recordPoll, getMetrics, _history: history };
29
+ }
30
+
31
+ module.exports = { createApiMetrics };
package/lib/auth.js ADDED
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ const { graphqlPost } = require('./graphql');
3
+
4
+ async function obtainToken(apiKey) {
5
+ const response = await graphqlPost({
6
+ query: `mutation obtainToken($input: ObtainJSONWebTokenInput!) {
7
+ obtainKrakenToken(input: $input) { token }
8
+ }`,
9
+ variables: { input: { APIKey: apiKey } }
10
+ });
11
+ if (response.data.errors) {
12
+ throw new Error(`Auth failed: ${JSON.stringify(response.data.errors)}`);
13
+ }
14
+ if (!response.data.data || !response.data.data.obtainKrakenToken) {
15
+ throw new Error('Auth response missing token data');
16
+ }
17
+ return response.data.data.obtainKrakenToken.token;
18
+ }
19
+
20
+ module.exports = { obtainToken };
@@ -0,0 +1,30 @@
1
+ // lib/categories/account.js
2
+ 'use strict';
3
+
4
+ const QUERY = `
5
+ query getAccountBalance($account: String!) {
6
+ account(accountNumber: $account) {
7
+ balance
8
+ }
9
+ }`;
10
+
11
+ function buildQuery(account) {
12
+ return { query: QUERY, variables: { account } };
13
+ }
14
+
15
+ function parseResponse(data) {
16
+ const balancePence = ((data.account || {}).balance) ?? null;
17
+ return {
18
+ account_balance_pence: balancePence,
19
+ account_balance_pounds: balancePence !== null ? parseFloat((balancePence / 100).toFixed(2)) : null,
20
+ account_error: null
21
+ };
22
+ }
23
+
24
+ const defaultData = {
25
+ account_balance_pence: null,
26
+ account_balance_pounds: null,
27
+ account_error: null
28
+ };
29
+
30
+ module.exports = { buildQuery, parseResponse, defaultData };
@@ -0,0 +1,91 @@
1
+ // lib/categories/applicable-rates.js
2
+ // Half-hourly time-of-use rates for a given electricity supply point.
3
+ // Requires MPAN (or MPRN). Returns 30-min slots for the next 24h.
4
+ //
5
+ // Same parser handles import and export — pass { fieldPrefix } to parseResponse
6
+ // and exportFields() to swap the output naming to electricity_export_rate_*.
7
+ 'use strict';
8
+
9
+ const QUERY = `
10
+ query getApplicableRates($account: String!, $mpxn: String!, $startAt: DateTime!, $endAt: DateTime!) {
11
+ applicableRates(accountNumber: $account, mpxn: $mpxn, startAt: $startAt, endAt: $endAt, first: 48) {
12
+ edges { node { validFrom validTo value } }
13
+ }
14
+ }`;
15
+
16
+ function buildQuery(account, mpan) {
17
+ const startAt = new Date().toISOString();
18
+ const endAt = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString();
19
+ return { query: QUERY, variables: { account, mpxn: mpan, startAt, endAt } };
20
+ }
21
+
22
+ function _emit(prefix, slots, current, prev, next) {
23
+ const pence = current ? current.value_pence_per_kwh : null;
24
+ const prevPence = prev ? prev.value_pence_per_kwh : null;
25
+ const nextPence = next ? next.value_pence_per_kwh : null;
26
+ const out = {};
27
+ out[`${prefix}`] = slots;
28
+ out[`${prefix}_count`] = slots.length;
29
+ out[`${prefix}_current_pence`] = pence;
30
+ out[`${prefix}_current_gbp`] = pence !== null ? parseFloat((pence / 100).toFixed(4)) : null;
31
+ out[`${prefix}_prev_pence`] = prevPence;
32
+ out[`${prefix}_prev_gbp`] = prevPence !== null ? parseFloat((prevPence / 100).toFixed(4)) : null;
33
+ out[`${prefix}_prev_to`] = prev ? prev.valid_to : null;
34
+ out[`${prefix}_next_pence`] = nextPence;
35
+ out[`${prefix}_next_gbp`] = nextPence !== null ? parseFloat((nextPence / 100).toFixed(4)) : null;
36
+ out[`${prefix}_next_from`] = next ? next.valid_from : null;
37
+ out[`${prefix}_error`] = null;
38
+ return out;
39
+ }
40
+
41
+ function parseResponse(data, options) {
42
+ const prefix = (options && options.fieldPrefix) || 'applicable_rates';
43
+ const edges = ((data.applicableRates || {}).edges) || [];
44
+ const slots = edges.map(e => ({
45
+ valid_from: e.node.validFrom,
46
+ valid_to: e.node.validTo,
47
+ value_pence_per_kwh: e.node.value !== null ? parseFloat(e.node.value) : null
48
+ }));
49
+
50
+ slots.sort((a, b) => new Date(a.valid_from) - new Date(b.valid_from));
51
+
52
+ const now = Date.now();
53
+ const currentIdx = slots.findIndex(s => {
54
+ const from = new Date(s.valid_from).getTime();
55
+ const to = new Date(s.valid_to).getTime();
56
+ return now >= from && now < to;
57
+ });
58
+
59
+ const current = currentIdx !== -1 ? slots[currentIdx] : null;
60
+ const prev = currentIdx > 0 ? slots[currentIdx - 1] : null;
61
+ const next = (currentIdx !== -1 && currentIdx < slots.length - 1) ? slots[currentIdx + 1] : null;
62
+
63
+ return _emit(prefix, slots, current, prev, next);
64
+ }
65
+
66
+ const defaultData = {
67
+ applicable_rates: [],
68
+ applicable_rates_count: 0,
69
+ applicable_rates_current_pence: null,
70
+ applicable_rates_current_gbp: null,
71
+ applicable_rates_prev_pence: null,
72
+ applicable_rates_prev_gbp: null,
73
+ applicable_rates_prev_to: null,
74
+ applicable_rates_next_pence: null,
75
+ applicable_rates_next_gbp: null,
76
+ applicable_rates_next_from: null,
77
+ applicable_rates_error: null,
78
+ electricity_export_rate: [],
79
+ electricity_export_rate_count: 0,
80
+ electricity_export_rate_current_pence: null,
81
+ electricity_export_rate_current_gbp: null,
82
+ electricity_export_rate_prev_pence: null,
83
+ electricity_export_rate_prev_gbp: null,
84
+ electricity_export_rate_prev_to: null,
85
+ electricity_export_rate_next_pence: null,
86
+ electricity_export_rate_next_gbp: null,
87
+ electricity_export_rate_next_from: null,
88
+ electricity_export_rate_error: null
89
+ };
90
+
91
+ module.exports = { buildQuery, parseResponse, defaultData };
@@ -0,0 +1,35 @@
1
+ // lib/categories/completed-dispatches.js
2
+ 'use strict';
3
+
4
+ const QUERY = `
5
+ query getCompletedDispatches($account: String!) {
6
+ completedDispatches(accountNumber: $account) {
7
+ start end delta meta { source location }
8
+ }
9
+ }`;
10
+
11
+ function buildQuery(account) {
12
+ return { query: QUERY, variables: { account } };
13
+ }
14
+
15
+ function parseResponse(data) {
16
+ const dispatches = data.completedDispatches || [];
17
+ return {
18
+ completed_dispatches: dispatches.map(d => ({
19
+ start: d.start,
20
+ end: d.end,
21
+ delta_kwh: d.delta !== null && d.delta !== undefined ? parseFloat(d.delta) : null,
22
+ source: (d.meta || {}).source || null
23
+ })),
24
+ completed_dispatches_count: dispatches.length,
25
+ completed_dispatches_error: null
26
+ };
27
+ }
28
+
29
+ const defaultData = {
30
+ completed_dispatches: [],
31
+ completed_dispatches_count: 0,
32
+ completed_dispatches_error: null
33
+ };
34
+
35
+ module.exports = { buildQuery, parseResponse, defaultData };