homebridge-econet-rheem
Version:
Homebridge plugin based on pyeconet for control of Rheem water heaters
98 lines • 4.19 kB
JavaScript
import path from 'path';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import { ThermostatAccessory } from '../homebridge/thermostatAccessory.js';
import { WaterHeaterAccessory } from '../homebridge/waterHeaterAccessory.js';
import strings from '../lang/en.js';
import { EconetApi } from '../model/api.js';
import { EquipmentType } from '../model/constants.js';
import { Log } from '../tools/log.js';
import { STORAGE_FILE_NAME } from '../tools/storage.js';
import getVersion from '../tools/version.js';
export class EconetRheemPlatform {
config;
api;
Service;
Characteristic;
log;
accessories = new Map();
econetApi = null;
constructor(logger, config, api) {
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.log = new Log(logger, config.verbose);
this.log.always('v%s | System %s | Node %s | HB v%s | HAPNodeJS v%s', getVersion(), process.platform, process.version, api.serverVersion, api.hap.HAPLibraryVersion());
this.api.on('didFinishLaunching', () => {
this.setup();
});
this.api.on('shutdown', () => {
this.teardown();
});
}
configureAccessory(accessory) {
this.log.always(strings.restoringDevice, accessory.displayName);
this.accessories.set(accessory.context.serialNumber, accessory);
}
teardown() {
this.econetApi?.teardown();
}
async setup() {
const email = this.config.email;
const password = this.config.password;
const debugMQTT = this.config.mqtt_debug;
if (!email || !password) {
this.log.error(strings.badConfig);
return;
}
try {
const storageFilePath = path.join(this.api.user.storagePath(), STORAGE_FILE_NAME);
this.econetApi = await EconetApi.connect(this.log, email, password, storageFilePath, debugMQTT);
const equipments = Array.from(this.econetApi.equipments.values());
if (equipments.length === 0) {
this.log.warning(strings.noEquipment);
this.accessories.forEach(accessory => this.removeAccessory(accessory));
this.teardown();
return;
}
const keepSerials = new Set();
equipments.forEach(equipment => {
keepSerials.add(equipment.serialNumber);
this.initializeAccessory(equipment);
});
this.accessories.forEach(accessory => {
if (!keepSerials.has(accessory.context.serialNumber)) {
this.removeAccessory(accessory);
}
});
}
catch (error) {
this.log.error(strings.setupFailed, error instanceof Error ? error.message : String(error));
}
}
initializeAccessory(equipment) {
let accessory = this.accessories.get(equipment.serialNumber);
if (!accessory) {
const deviceName = equipment.deviceName;
this.log.always(strings.newEquipment, deviceName);
const uuid = this.api.hap.uuid.generate(equipment.serialNumber);
accessory = new this.api.platformAccessory(deviceName, uuid);
accessory.context.serialNumber = equipment.serialNumber;
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
this.accessories.set(equipment.serialNumber, accessory);
}
switch (equipment.type) {
case EquipmentType.THERMOSTAT:
new ThermostatAccessory(this, accessory, equipment);
break;
case EquipmentType.WATER_HEATER:
new WaterHeaterAccessory(this, accessory, equipment, this.config.wh_sim_disable);
}
}
removeAccessory(accessory) {
this.log.always(strings.removeDevice, accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
this.accessories.delete(accessory.context.serialNumber);
}
}
//# sourceMappingURL=platform.js.map