homebridge-sonnenbatterie-v2
Version:
A Homebridge plugin for a SonnenBatterie with v2 firmware.
78 lines • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SonnenAccessoryFactory = exports.AccessoryType = void 0;
const settings_1 = require("./settings");
const sonnenProductionAccessory_1 = require("./sonnenProductionAccessory");
const sonnenConsumptionAccessory_1 = require("./sonnenConsumptionAccessory");
const sonnenGridAccessory_1 = require("./sonnenGridAccessory");
exports.AccessoryType = {
Production: 'production',
Consumption: 'consumption',
Grid: 'grid',
};
class SonnenAccessoryFactory {
platform;
api;
log;
constructor(platform) {
this.platform = platform;
this.api = platform.api;
this.log = platform.log;
}
makePlugin(displayName, uuid) {
// the accessory does not yet exist, so we need to create it
this.log.debug(`Adding new accessory: ${displayName} (${uuid})`);
// create a new accessory
const accessory = new this.api.platformAccessory(displayName, uuid);
let updatable;
switch (displayName) {
case exports.AccessoryType.Production:
updatable = new sonnenProductionAccessory_1.SonnenBatterieProductionAccessory(this.platform, accessory);
break;
case exports.AccessoryType.Consumption:
updatable = new sonnenConsumptionAccessory_1.SonnenBatterieConsumptionAccessory(this.platform, accessory);
break;
case exports.AccessoryType.Grid:
updatable = new sonnenGridAccessory_1.SonnenBatterieGridAccessory(this.platform, accessory);
break;
default:
updatable = null;
break;
}
// link the accessory to your platform
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
return updatable;
}
registerPlugin(existingAccessory) {
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
// if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
// this is imported from `platformAccessory.ts`
this.log.debug(`creating accessory handler for restored accessory: ${existingAccessory.UUID}, ${existingAccessory.displayName}`);
// FIXME: looks like this is replicated code - let's put this into a function
let updatable;
switch (existingAccessory.displayName) {
case exports.AccessoryType.Production:
updatable = new sonnenProductionAccessory_1.SonnenBatterieProductionAccessory(this.platform, existingAccessory);
break;
case exports.AccessoryType.Consumption:
updatable = new sonnenConsumptionAccessory_1.SonnenBatterieConsumptionAccessory(this.platform, existingAccessory);
break;
case exports.AccessoryType.Grid:
updatable = new sonnenGridAccessory_1.SonnenBatterieGridAccessory(this.platform, existingAccessory);
break;
default:
updatable = null;
break;
}
return updatable;
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.:
// remove platform accessories when no longer present
// this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
// this.log.info('Removing existing accessory from cache:', existingAccessory.displayName);
}
}
exports.SonnenAccessoryFactory = SonnenAccessoryFactory;
//# sourceMappingURL=sonnenAccessory.js.map