@balmli/homey-grid-prices 0.0.0-stage → 1.1.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,39 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ import moment from "moment-timezone";
4
+ import { Device } from "homey";
5
+ import { GridPricesFetchClient } from "./GridPricesFetchClient";
6
+ import { GridOptions } from "./types";
7
+ export declare class GridPriceFetcher extends EventEmitter {
8
+ private logger;
9
+ private pricesFetchClient;
10
+ private device;
11
+ private priceFetcherOptions;
12
+ private forcePriceUpdate?;
13
+ private fetchTimeout?;
14
+ private updatePriceTimeout?;
15
+ private deleted?;
16
+ constructor({ logger, pricesFetchClient, device }: {
17
+ logger: any;
18
+ pricesFetchClient: GridPricesFetchClient;
19
+ device: Device;
20
+ });
21
+ start(): void;
22
+ stop(): void;
23
+ destroy(): void;
24
+ setGridOptions(gridOptions: GridOptions): void;
25
+ setFetchTime(fetchTime: number): void;
26
+ private clearFetchData;
27
+ private scheduleFetchData;
28
+ private doFetchData;
29
+ private clearUpdatePrice;
30
+ private scheduleUpdatePrice;
31
+ private doUpdatePrice;
32
+ private onGridPrices;
33
+ private getTimezone;
34
+ private getFetchParameters;
35
+ }
36
+ /**
37
+ * Seconds until the next midnight in the given timezone (Homey apps run in UTC, so the local time must be explicit).
38
+ */
39
+ export declare const secondsToNextLocalMidnight: (timezone?: string, now?: moment.Moment) => number;
@@ -0,0 +1,241 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.secondsToNextLocalMidnight = exports.GridPriceFetcher = void 0;
58
+ var events_1 = __importDefault(require("events"));
59
+ var moment_timezone_1 = __importDefault(require("moment-timezone"));
60
+ var types_1 = require("./types");
61
+ var GridPriceFetcher = /** @class */ (function (_super) {
62
+ __extends(GridPriceFetcher, _super);
63
+ function GridPriceFetcher(_a) {
64
+ var logger = _a.logger, pricesFetchClient = _a.pricesFetchClient, device = _a.device;
65
+ var _this = _super.call(this) || this;
66
+ _this.priceFetcherOptions = new types_1.GridPriceFetcherOptions({});
67
+ _this.logger = logger;
68
+ _this.pricesFetchClient = pricesFetchClient;
69
+ _this.device = device;
70
+ return _this;
71
+ }
72
+ GridPriceFetcher.prototype.start = function () {
73
+ this.forcePriceUpdate = true;
74
+ this.scheduleFetchData(3);
75
+ };
76
+ GridPriceFetcher.prototype.stop = function () {
77
+ this.clearFetchData();
78
+ this.clearUpdatePrice();
79
+ };
80
+ GridPriceFetcher.prototype.destroy = function () {
81
+ this.deleted = true;
82
+ this.stop();
83
+ };
84
+ GridPriceFetcher.prototype.setGridOptions = function (gridOptions) {
85
+ this.priceFetcherOptions.gridOptions = gridOptions;
86
+ };
87
+ GridPriceFetcher.prototype.setFetchTime = function (fetchTime) {
88
+ this.priceFetcherOptions.fetchTime = fetchTime;
89
+ };
90
+ GridPriceFetcher.prototype.clearFetchData = function () {
91
+ if (this.fetchTimeout) {
92
+ this.device.homey.clearTimeout(this.fetchTimeout);
93
+ this.fetchTimeout = undefined;
94
+ }
95
+ };
96
+ GridPriceFetcher.prototype.scheduleFetchData = function (seconds) {
97
+ if (this.deleted) {
98
+ return;
99
+ }
100
+ this.clearFetchData();
101
+ if (seconds === undefined) {
102
+ var syncTime = this.priceFetcherOptions.fetchTime;
103
+ var now = new Date();
104
+ seconds = syncTime - (now.getMinutes() * 60 + now.getSeconds());
105
+ seconds = seconds <= 0 ? seconds + 3600 : seconds;
106
+ this.logger.verbose("Grid Price Fetcher: fetch time: ".concat(syncTime));
107
+ }
108
+ this.logger.debug("Grid Price Fetcher: fetch data in ".concat(seconds, " seconds"));
109
+ this.fetchTimeout = this.device.homey.setTimeout(this.doFetchData.bind(this), seconds * 1000);
110
+ };
111
+ GridPriceFetcher.prototype.doFetchData = function () {
112
+ return __awaiter(this, void 0, void 0, function () {
113
+ var options, prices, err_1;
114
+ return __generator(this, function (_a) {
115
+ switch (_a.label) {
116
+ case 0:
117
+ if (this.deleted) {
118
+ return [2 /*return*/];
119
+ }
120
+ _a.label = 1;
121
+ case 1:
122
+ _a.trys.push([1, 3, 4, 5]);
123
+ this.clearFetchData();
124
+ if (this.forcePriceUpdate) {
125
+ this.clearUpdatePrice();
126
+ }
127
+ options = this.getFetchParameters().options;
128
+ return [4 /*yield*/, this.pricesFetchClient.fetchGridPrices(this.device, options)];
129
+ case 2:
130
+ prices = _a.sent();
131
+ if (prices) {
132
+ this.logger.verbose("Grid Price Fetcher: fetched grid prices: ".concat(prices === null || prices === void 0 ? void 0 : prices.id, " ").concat(prices === null || prices === void 0 ? void 0 : prices.description));
133
+ }
134
+ else {
135
+ this.logger.warn("Grid Price Fetcher: no grid prices fetched for ".concat(JSON.stringify(options)));
136
+ }
137
+ return [3 /*break*/, 5];
138
+ case 3:
139
+ err_1 = _a.sent();
140
+ this.logger.error(err_1);
141
+ return [3 /*break*/, 5];
142
+ case 4:
143
+ this.scheduleFetchData();
144
+ if (this.forcePriceUpdate) {
145
+ this.forcePriceUpdate = false;
146
+ this.scheduleUpdatePrice(1);
147
+ }
148
+ return [7 /*endfinally*/];
149
+ case 5: return [2 /*return*/];
150
+ }
151
+ });
152
+ });
153
+ };
154
+ GridPriceFetcher.prototype.clearUpdatePrice = function () {
155
+ if (this.updatePriceTimeout) {
156
+ this.device.homey.clearTimeout(this.updatePriceTimeout);
157
+ this.updatePriceTimeout = undefined;
158
+ }
159
+ };
160
+ GridPriceFetcher.prototype.scheduleUpdatePrice = function (seconds) {
161
+ if (this.deleted) {
162
+ return;
163
+ }
164
+ this.clearUpdatePrice();
165
+ if (seconds === undefined) {
166
+ seconds = (0, exports.secondsToNextLocalMidnight)(this.getTimezone()) + 3;
167
+ }
168
+ this.logger.debug("Grid Price Fetcher: update price in ".concat(seconds, " seconds"));
169
+ this.updatePriceTimeout = this.device.homey.setTimeout(this.doUpdatePrice.bind(this), seconds * 1000);
170
+ };
171
+ GridPriceFetcher.prototype.doUpdatePrice = function () {
172
+ return __awaiter(this, void 0, void 0, function () {
173
+ var options, prices, err_2;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
176
+ case 0:
177
+ if (this.deleted) {
178
+ return [2 /*return*/];
179
+ }
180
+ _a.label = 1;
181
+ case 1:
182
+ _a.trys.push([1, 4, 5, 6]);
183
+ this.clearUpdatePrice();
184
+ options = this.getFetchParameters().options;
185
+ prices = this.pricesFetchClient.getPrices(this.device, options);
186
+ if (!prices) return [3 /*break*/, 3];
187
+ return [4 /*yield*/, this.onGridPrices(prices)];
188
+ case 2:
189
+ _a.sent();
190
+ _a.label = 3;
191
+ case 3: return [3 /*break*/, 6];
192
+ case 4:
193
+ err_2 = _a.sent();
194
+ this.logger.error(err_2);
195
+ return [3 /*break*/, 6];
196
+ case 5:
197
+ this.scheduleUpdatePrice();
198
+ return [7 /*endfinally*/];
199
+ case 6: return [2 /*return*/];
200
+ }
201
+ });
202
+ });
203
+ };
204
+ GridPriceFetcher.prototype.onGridPrices = function (prices) {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ return __generator(this, function (_a) {
207
+ try {
208
+ this.emit('gridPrices', prices);
209
+ }
210
+ catch (err) {
211
+ this.logger.error(err);
212
+ }
213
+ return [2 /*return*/];
214
+ });
215
+ });
216
+ };
217
+ GridPriceFetcher.prototype.getTimezone = function () {
218
+ try {
219
+ return this.device.homey.clock.getTimezone();
220
+ }
221
+ catch (err) {
222
+ return undefined;
223
+ }
224
+ };
225
+ GridPriceFetcher.prototype.getFetchParameters = function () {
226
+ var options = this.priceFetcherOptions.gridOptions;
227
+ return { options: options };
228
+ };
229
+ return GridPriceFetcher;
230
+ }(events_1.default));
231
+ exports.GridPriceFetcher = GridPriceFetcher;
232
+ /**
233
+ * Seconds until the next midnight in the given timezone (Homey apps run in UTC, so the local time must be explicit).
234
+ */
235
+ var secondsToNextLocalMidnight = function (timezone, now) {
236
+ if (now === void 0) { now = (0, moment_timezone_1.default)(); }
237
+ var local = timezone ? now.clone().tz(timezone) : now.clone();
238
+ var nextMidnight = local.clone().add(1, 'day').startOf('day');
239
+ return (nextMidnight.valueOf() - now.valueOf()) / 1000;
240
+ };
241
+ exports.secondsToNextLocalMidnight = secondsToNextLocalMidnight;
@@ -0,0 +1,11 @@
1
+ import { GridSettingsConfigMap } from "./types";
2
+ export declare class GridPricesApi {
3
+ logger: any;
4
+ constructor({ logger }: {
5
+ logger: any;
6
+ });
7
+ /**
8
+ * Fetch grid prices from GitHub.
9
+ */
10
+ fetchPrices: () => Promise<GridSettingsConfigMap>;
11
+ }
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.GridPricesApi = void 0;
40
+ var http = require('http.min');
41
+ var GridPricesApi = /** @class */ (function () {
42
+ function GridPricesApi(_a) {
43
+ var logger = _a.logger;
44
+ var _this = this;
45
+ /**
46
+ * Fetch grid prices from GitHub.
47
+ */
48
+ this.fetchPrices = function () { return __awaiter(_this, void 0, void 0, function () {
49
+ var uri, out, response, data;
50
+ return __generator(this, function (_a) {
51
+ switch (_a.label) {
52
+ case 0:
53
+ uri = 'https://raw.githubusercontent.com/balmli/no.almli.utilitycost/main/data/gridprices.json';
54
+ return [4 /*yield*/, http.get({ uri: uri, timeout: 30000 })];
55
+ case 1:
56
+ out = _a.sent();
57
+ response = out.response;
58
+ //console.log(response);
59
+ if (response.statusCode === 404) {
60
+ throw new Error("Grid prices not found: \"".concat(uri, "\""));
61
+ }
62
+ else if (response.statusCode !== 200) {
63
+ throw new Error("Fetching grid prices failed: ".concat(response.statusCode, " ").concat(response.statusMessage));
64
+ }
65
+ try {
66
+ data = JSON.parse(out.data);
67
+ return [2 /*return*/, data];
68
+ }
69
+ catch (err) {
70
+ throw new Error("Parsing grid prices failed: ".concat(err));
71
+ }
72
+ return [2 /*return*/];
73
+ }
74
+ });
75
+ }); };
76
+ this.logger = logger;
77
+ }
78
+ return GridPricesApi;
79
+ }());
80
+ exports.GridPricesApi = GridPricesApi;
@@ -0,0 +1,35 @@
1
+ import { Device } from "homey";
2
+ import { GridPricesApi } from "./GridPricesApi";
3
+ import { GridOptions, GridSettingsConfig } from "./types";
4
+ export declare class GridPricesFetchClient {
5
+ logger: any;
6
+ gridPrices: GridPricesApi;
7
+ constructor({ logger }: {
8
+ logger: any;
9
+ });
10
+ /**
11
+ * Fetch grid prices and store in storage.
12
+ *
13
+ * @param device Homey.device
14
+ * @param options
15
+ */
16
+ fetchGridPrices(device: Device, options: GridOptions): Promise<GridSettingsConfig | undefined>;
17
+ /**
18
+ * Get grid prices from storage.
19
+ *
20
+ * @param device
21
+ * @param options
22
+ */
23
+ getPrices(device: Device, options: GridOptions): GridSettingsConfig | undefined;
24
+ private cachePrefix;
25
+ private cacheId;
26
+ private storePricesInStorage;
27
+ private getPricesFromStorage;
28
+ /**
29
+ * Clear all grid prices in the storage.
30
+ *
31
+ * @param device
32
+ * @private
33
+ */
34
+ clearStorage(device: Device): Promise<void>;
35
+ }
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.GridPricesFetchClient = void 0;
40
+ var GridPricesApi_1 = require("./GridPricesApi");
41
+ var STORE_PREFIX = 'gridprices-';
42
+ var GridPricesFetchClient = /** @class */ (function () {
43
+ function GridPricesFetchClient(_a) {
44
+ var logger = _a.logger;
45
+ this.logger = logger;
46
+ this.gridPrices = new GridPricesApi_1.GridPricesApi({ logger: logger });
47
+ }
48
+ /**
49
+ * Fetch grid prices and store in storage.
50
+ *
51
+ * @param device Homey.device
52
+ * @param options
53
+ */
54
+ GridPricesFetchClient.prototype.fetchGridPrices = function (device, options) {
55
+ return __awaiter(this, void 0, void 0, function () {
56
+ var allPrices, prices;
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0: return [4 /*yield*/, this.clearStorage(device)];
60
+ case 1:
61
+ _a.sent();
62
+ return [4 /*yield*/, this.gridPrices.fetchPrices()];
63
+ case 2:
64
+ allPrices = _a.sent();
65
+ if (allPrices && allPrices[options.gridCompany]) {
66
+ prices = allPrices[options.gridCompany];
67
+ if (prices) {
68
+ this.storePricesInStorage(device, prices, options);
69
+ }
70
+ }
71
+ return [2 /*return*/, this.getPrices(device, options)];
72
+ }
73
+ });
74
+ });
75
+ };
76
+ /**
77
+ * Get grid prices from storage.
78
+ *
79
+ * @param device
80
+ * @param options
81
+ */
82
+ GridPricesFetchClient.prototype.getPrices = function (device, options) {
83
+ return this.getPricesFromStorage(device, options);
84
+ };
85
+ GridPricesFetchClient.prototype.cachePrefix = function () {
86
+ return "".concat(STORE_PREFIX, "-");
87
+ };
88
+ GridPricesFetchClient.prototype.cacheId = function (options) {
89
+ return "".concat(this.cachePrefix()).concat(options.gridCompany);
90
+ };
91
+ GridPricesFetchClient.prototype.storePricesInStorage = function (device, prices, options) {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var key;
94
+ var _this = this;
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0:
98
+ key = this.cacheId(options);
99
+ return [4 /*yield*/, device.setStoreValue(key, prices).catch(function (err) { return _this.logger.error(err); })];
100
+ case 1:
101
+ _a.sent();
102
+ return [2 /*return*/];
103
+ }
104
+ });
105
+ });
106
+ };
107
+ GridPricesFetchClient.prototype.getPricesFromStorage = function (device, options) {
108
+ var key = this.cacheId(options);
109
+ return device.getStoreValue(key);
110
+ };
111
+ /**
112
+ * Clear all grid prices in the storage.
113
+ *
114
+ * @param device
115
+ * @private
116
+ */
117
+ GridPricesFetchClient.prototype.clearStorage = function (device) {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var keys, _i, keys_1, key;
120
+ var _this = this;
121
+ return __generator(this, function (_a) {
122
+ switch (_a.label) {
123
+ case 0:
124
+ keys = device.getStoreKeys();
125
+ _i = 0, keys_1 = keys;
126
+ _a.label = 1;
127
+ case 1:
128
+ if (!(_i < keys_1.length)) return [3 /*break*/, 4];
129
+ key = keys_1[_i];
130
+ if (!key.startsWith(STORE_PREFIX)) return [3 /*break*/, 3];
131
+ return [4 /*yield*/, device.unsetStoreValue(key).catch(function (err) { return _this.logger.error(err); })];
132
+ case 2:
133
+ _a.sent();
134
+ _a.label = 3;
135
+ case 3:
136
+ _i++;
137
+ return [3 /*break*/, 1];
138
+ case 4: return [2 /*return*/];
139
+ }
140
+ });
141
+ });
142
+ };
143
+ return GridPricesFetchClient;
144
+ }());
145
+ exports.GridPricesFetchClient = GridPricesFetchClient;
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export { GridPricesApi } from './GridPricesApi';
3
+ export { GridPricesFetchClient } from './GridPricesFetchClient';
4
+ export { GridPriceFetcher } from './GridPriceFetcher';
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.GridPriceFetcher = exports.GridPricesFetchClient = exports.GridPricesApi = void 0;
18
+ __exportStar(require("./types"), exports);
19
+ var GridPricesApi_1 = require("./GridPricesApi");
20
+ Object.defineProperty(exports, "GridPricesApi", { enumerable: true, get: function () { return GridPricesApi_1.GridPricesApi; } });
21
+ var GridPricesFetchClient_1 = require("./GridPricesFetchClient");
22
+ Object.defineProperty(exports, "GridPricesFetchClient", { enumerable: true, get: function () { return GridPricesFetchClient_1.GridPricesFetchClient; } });
23
+ var GridPriceFetcher_1 = require("./GridPriceFetcher");
24
+ Object.defineProperty(exports, "GridPriceFetcher", { enumerable: true, get: function () { return GridPriceFetcher_1.GridPriceFetcher; } });
@@ -0,0 +1,44 @@
1
+ export interface GridSettings {
2
+ validFrom?: string;
3
+ gridCapacity0_2: number;
4
+ gridCapacity2_5: number;
5
+ gridCapacity5_10: number;
6
+ gridCapacity10_15: number;
7
+ gridCapacity15_20: number;
8
+ gridCapacity20_25: number;
9
+ /** Optional tiers above 25 kW. 0 or missing: the 20-25 kW price is used. */
10
+ gridCapacity25_50?: number;
11
+ gridCapacity50_75?: number;
12
+ gridCapacity75_100?: number;
13
+ gridCapacity100_plus?: number;
14
+ gridCapacityAverage: string;
15
+ gridEnergyDay: number;
16
+ gridEnergyNight: number;
17
+ gridEnergyLowWeekends: boolean;
18
+ /** Low price on Norwegian public holidays. */
19
+ gridEnergyLowHoliday?: boolean;
20
+ /** Hour (local time) when the day price starts. Default 6. */
21
+ gridEnergyDayStart?: number;
22
+ /** Hour (local time) when the day price ends. Default 22. */
23
+ gridEnergyDayEnd?: number;
24
+ }
25
+ export type GridSettingsList = GridSettings[];
26
+ export interface GridSettingsConfig {
27
+ id: string;
28
+ description: string;
29
+ settings: GridSettings | GridSettingsList;
30
+ }
31
+ export type GridSettingsConfigMap = {
32
+ [key: string]: GridSettingsConfig;
33
+ };
34
+ export interface GridOptions {
35
+ gridCompany: string;
36
+ }
37
+ export declare class GridPriceFetcherOptions {
38
+ gridOptions?: GridOptions;
39
+ fetchTime?: number;
40
+ constructor({ gridOptions, fetchTime }: {
41
+ gridOptions?: GridOptions;
42
+ fetchTime?: number;
43
+ });
44
+ }
package/dist/types.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GridPriceFetcherOptions = void 0;
4
+ var GridPriceFetcherOptions = /** @class */ (function () {
5
+ function GridPriceFetcherOptions(_a) {
6
+ var gridOptions = _a.gridOptions, fetchTime = _a.fetchTime;
7
+ this.gridOptions = gridOptions;
8
+ this.fetchTime = fetchTime;
9
+ }
10
+ return GridPriceFetcherOptions;
11
+ }());
12
+ exports.GridPriceFetcherOptions = GridPriceFetcherOptions;
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
- "name": "@balmli/homey-grid-prices",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@balmli/homey-grid-prices",
3
+ "version": "1.1.0",
4
+ "description": "Grid price utility for Homey apps",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "commonjs",
8
+ "engines": {
9
+ "node": ">=16.0.0"
10
+ },
11
+ "scripts": {
12
+ "prebuild": "del dist/",
13
+ "build": "tsc",
14
+ "watch": "tsc -w",
15
+ "pretest": "npm run build",
16
+ "test": "env NODE_OPTIONS=--no-experimental-strip-types TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register --timeout 10000 'tests/**/*.*'",
17
+ "test2": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register --timeout 10000 'tests2/**/*.*'",
18
+ "prepublishOnly": "npm run test"
19
+ },
20
+ "keywords": [
21
+ "node",
22
+ "grid",
23
+ "prices"
24
+ ],
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/balmli/node-homey-grid-prices.git"
28
+ },
29
+ "author": "bjornar.almli@gmail.com",
30
+ "bugs": {
31
+ "url": "https://github.com/balmli/node-homey-grid-prices/issues"
32
+ },
33
+ "homepage": "https://github.com/balmli/node-homey-grid-prices#readme",
34
+ "license": "ISC",
35
+ "dependencies": {
36
+ "@balmli/homey-logger": "^1.0.0",
37
+ "http.min": "2.1.0",
38
+ "moment-timezone": "0.5.40"
39
+ },
40
+ "devDependencies": {
41
+ "@types/chai": "^4.3.0",
42
+ "@types/homey": "npm:homey-apps-sdk-v3-types@^0.3.3",
43
+ "@types/mocha": "^9.1.1",
44
+ "@types/node": "^16.11.12",
45
+ "chai": "^4.3.6",
46
+ "cpy-cli": "^4.2.0",
47
+ "del-cli": "^3.0.0",
48
+ "mocha": "^10.0.0",
49
+ "ts-mocha": "^10.0.0",
50
+ "ts-node": "^10.7.0",
51
+ "typescript": "^4.6.4"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public",
55
+ "registry": "https://registry.npmjs.org/"
56
+ }
57
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.