homebridge-nordpool-baltics
Version:
Plugin exposes virtual accessories (switch, light, presence, temperature) and enables HomeKit automation by Nordpool electricity price in Lithuania, Latvia, Estonia, Finland, Sweden, Norway, Denmark, Germany, Luxembourg, Austria.
134 lines • 7.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NordpoolPlatformAccessory = void 0;
const settings_1 = require("./settings");
const functions_1 = require("./functions");
const node_cron_1 = require("node-cron");
class NordpoolPlatformAccessory {
constructor(platform, accessory, api) {
var _a, _b;
this.platform = platform;
this.accessory = accessory;
this.api = api;
this.decimalPrecision = (_a = this.platform.config.decimalPrecision) !== null && _a !== void 0 ? _a : 1;
this.dynamicCheapestConsecutiveHours = (_b = this.platform.config.dynamicCheapestConsecutiveHours) !== null && _b !== void 0 ? _b : false;
this.service = settings_1.defaultService;
this.pricesCache = (0, settings_1.defaultPricesCache)(this.api, this.platform.log);
this.fnc = new functions_1.Functions(this.platform, this.accessory, this.service, this.api);
this.fnc.initAccessories()
.then(() => {
this.fnc.checkSystemTimezone();
this.getPrices();
(0, node_cron_1.schedule)('0 * * * *', () => {
this.getPrices();
});
})
.catch((error) => {
this.platform.log.error(error);
});
}
async getPrices() {
const todayKey = (0, settings_1.fnc_todayKey)(this.platform.config);
const tomorrowKey = (0, settings_1.fnc_tomorrowKey)(this.platform.config);
const currentHour = (0, settings_1.fnc_currentHour)(this.platform.config);
// did precision config change?
// if changed: clear cache and reload the data from Nordpool prices provider
const decimalPrecisionCache = await this.pricesCache.get('decimalPrecision');
if (decimalPrecisionCache !== this.decimalPrecision) {
try {
await this.pricesCache.remove(todayKey);
await this.pricesCache.remove(tomorrowKey);
await this.pricesCache.remove('5consecutiveUpdated');
}
catch (error) {
this.platform.log.error(`ERR: failed clearing pricesCache: ${JSON.stringify(error)}`);
}
finally {
this.platform.log.warn(`Configured Decimal Precision changed from ${decimalPrecisionCache} to ${this.decimalPrecision}`);
this.pricesCache.set('decimalPrecision', this.decimalPrecision);
}
// 1s wait to ensure cache files are deleted before proceeding
await new Promise(resolve => setTimeout(resolve, 1000));
}
const areaCache = await this.pricesCache.get('area');
if (this.platform.config.area !== undefined && areaCache !== this.platform.config.area) {
try {
await this.pricesCache.remove(todayKey);
await this.pricesCache.remove(tomorrowKey);
await this.pricesCache.remove('5consecutiveUpdated');
}
catch (error) {
this.platform.log.error(`ERR: failed clearing pricesCache: ${JSON.stringify(error)}`);
}
finally {
this.platform.log.warn(`Configured Nordpool area changed from ${areaCache} to ${this.platform.config.area}`);
this.pricesCache.set('area', this.platform.config.area);
}
// 1s wait to ensure cache files are deleted before proceeding
await new Promise(resolve => setTimeout(resolve, 1000));
}
const cachedToday = await this.pricesCache.get(todayKey);
const cachedTomorrow = await this.pricesCache.get(tomorrowKey);
const todayExists = Array.isArray(cachedToday) && cachedToday.length > 0;
const tomorrowExists = Array.isArray(cachedTomorrow) && cachedTomorrow.length > 0;
this.platform.log.debug(`Cache stats: ${JSON.stringify({
todayExists: todayExists,
tomorrowExists: tomorrowExists,
cacheDir: this.pricesCache.basePath,
})}`);
if (!todayExists || (currentHour >= 18 && !tomorrowExists)) {
this.fnc.pullNordpoolData()
.then(async (results) => {
if (results) {
let todayResults = results.filter(result => result.day === todayKey);
let tomorrowResults = results.filter(result => result.day === tomorrowKey);
if (todayResults.length === 23) {
// DST switch to summer time
todayResults = this.fnc.fillMissingHours(todayResults, todayKey);
}
if (todayResults.length === 25 || todayResults.length === 24) {
this.pricesCache.set(todayKey, todayResults);
settings_1.pricing.today = todayResults;
this.pricesCache.set(`solarOverrideApplied_${todayKey}`, false);
this.platform.log.debug(`OK: pulled Nordpool prices in ${this.platform.config.area} area for TODAY (${todayKey})`);
this.platform.log.debug(JSON.stringify(todayResults.map(({ hour, price }) => ({ hour, price }))));
await this.fnc.analyze_and_setServices(currentHour);
}
else {
this.platform.log.warn('WARN: Something is incorrect with API response. Unable to determine today\'s Nordpool prices.');
this.platform.log.warn(`Raw response: ${JSON.stringify(todayResults)}`);
}
if (tomorrowResults.length === 23) {
// DST switch to summer time
tomorrowResults = this.fnc.fillMissingHours(tomorrowResults, tomorrowKey);
}
if (tomorrowResults.length === 24) {
this.pricesCache.set(tomorrowKey, tomorrowResults);
// keep decimalPrecision and area cache fresh so it does not ttl/expire
this.pricesCache.set('decimalPrecision', this.decimalPrecision);
this.pricesCache.set('area', this.platform.config.area);
this.platform.log.debug(`OK: pulled Nordpool prices in ${this.platform.config.area} area for TOMORROW (${tomorrowKey})`);
this.platform.log.debug(JSON.stringify(tomorrowResults.map(({ hour, price }) => ({ hour, price }))));
if (this.dynamicCheapestConsecutiveHours) {
setTimeout(() => {
this.fnc.getCheapestHoursIn2days();
}, 2000);
}
}
}
else {
this.platform.log.warn('WARN: API returned no or abnormal results for todays\'s Nordpool prices data. Will retry in 1 hour');
}
})
.catch((error) => {
this.platform.log.error(`ERR: Failed to get todays's prices, will retry in 1 hour. ${error}`);
});
}
else {
settings_1.pricing.today = await this.pricesCache.get(todayKey, []);
await this.fnc.analyze_and_setServices(currentHour);
}
}
}
exports.NordpoolPlatformAccessory = NordpoolPlatformAccessory;
//# sourceMappingURL=platformAccessory.js.map