homebridge-awattar
Version:
Plugin exposes virtual (switch, light, presence, temperature) accessories and enables HomeKit automation by aWattar electricity pricing in Austria.
116 lines • 6.33 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.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)();
const tomorrowKey = (0, settings_1.fnc_tomorrowKey)();
const currentHour = (0, settings_1.fnc_currentHour)();
// did precision config change?
// if changed: clear cache and reload the data from Nordpool prices provider
const decimalPrecisionCache = this.pricesCache.getSync('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 value changed from ${decimalPrecisionCache} to ${this.decimalPrecision}`);
this.pricesCache.set('decimalPrecision', this.decimalPrecision);
}
}
const areaCache = this.pricesCache.getSync('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);
}
}
settings_1.pricing.today = this.pricesCache.getSync(todayKey, []);
if (settings_1.pricing.today.length === 0
|| (currentHour >= 18 && !this.pricesCache.getSync(tomorrowKey))) {
this.fnc.pullNordpoolData()
.then((results) => {
if (results) {
const todayResults = results.filter(result => result.day === todayKey);
const tomorrowResults = results.filter(result => result.day === tomorrowKey);
if (todayResults.length === 24 || todayResults.length === 23) {
this.pricesCache.set(todayKey, todayResults);
settings_1.pricing.today = todayResults;
this.pricesCache.setSync(`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 }))));
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 === 24 || tomorrowResults.length === 23) {
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');
//this.platform.log.info('Raw response:', results);
}
})
.catch((error) => {
this.platform.log.error(`ERR: Failed to get todays's prices, will retry in 1 hour. ${error}`);
});
}
else {
settings_1.pricing.today = this.pricesCache.getSync(todayKey, []);
this.fnc.analyze_and_setServices(currentHour);
}
}
}
exports.NordpoolPlatformAccessory = NordpoolPlatformAccessory;
//# sourceMappingURL=platformAccessory.js.map