@pst-on-npm/homebridge-enocean
Version:
Integrate EnOcean® devices into Homebridge.
82 lines • 3.11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.HbConfigUpdater = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const fs = __importStar(require("fs/promises"));
/**
* EnOcean Device Configuration
*/
class HbConfigUpdater {
configFilePath;
platformName;
constructor(configFilePath, platformName) {
this.configFilePath = configFilePath;
this.platformName = platformName;
}
// Reads the JSON file and returns the parsed object
async readJsonFile() {
const data = await fs.readFile(this.configFilePath, 'utf-8');
return JSON.parse(data);
}
// Writes the updated object back to the JSON file
async writeJsonFile(data) {
const jsonData = JSON.stringify(data, null, 2);
await fs.writeFile(this.configFilePath, jsonData, 'utf-8');
}
// Adds an item to the array and updates the JSON file
async addDeviceToHomebridgeConfig(newItem) {
const config = await this.readJsonFile();
const plugin = config.platforms.find((p) => p.platform === this.platformName);
if (plugin === undefined) {
throw new Error(`${this.platformName}: no such platform in platforms in ${this.configFilePath}`);
}
if (plugin.devices === undefined) {
plugin.devices = [];
}
;
const newEntry = {
name: newItem.name,
id: newItem.id,
eep: newItem.eep,
model: newItem.model,
manufacturer: newItem.manufacturer,
};
plugin.devices.push(newEntry);
await this.writeJsonFile(config);
}
}
exports.HbConfigUpdater = HbConfigUpdater;
//# sourceMappingURL=HbConfigUpdater.js.map