homebridge-deconz-converter
Version:
Homebridge plugin for converting Deconz roller shutters interpreted as light into Homekit Window Covering type.
117 lines • 6.62 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeconzConverterPlatform = void 0;
const settings_1 = require("./settings");
const rollerShutter_1 = require("./accessories/rollerShutter");
const nodonMultifonctions_1 = require("./accessories/nodonMultifonctions");
const httpClient_1 = __importDefault(require("./httpClient"));
const wsClient_1 = __importDefault(require("./wsClient"));
const contactSensor_1 = require("./accessories/contactSensor");
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
class DeconzConverterPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// This is used to track restored cached accessories
this.accessories = [];
this.log.debug('Finished initializing platform:', this.config.name);
// Instantiate httpClient
const { host, useHTTPS, apiKey, port } = this.config;
if (!host || !apiKey) {
this.log.error('No host or apiKey found. Please fill configuration');
}
this.client = new httpClient_1.default(host, useHTTPS, apiKey);
try {
this.wsClient = new wsClient_1.default(host, port).connect();
this.log.info('Connected to websocket');
}
catch (e) {
throw new Error(`Cannot connect to websocket: ${e}`);
}
this.api.on("didFinishLaunching" /* DID_FINISH_LAUNCHING */, () => {
this.log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
discoverDevices() {
var _a, _b, _c;
const rollerShutters = (_a = this.config.rollerShutters) !== null && _a !== void 0 ? _a : [];
const nodonMultifonctions = (_b = this.config.nodonMultifonctions) !== null && _b !== void 0 ? _b : [];
const contactSensors = (_c = this.config.contactSensors) !== null && _c !== void 0 ? _c : [];
for (const rollerShutter of rollerShutters) {
const uuid = this.api.hap.uuid.generate(rollerShutter.uniqueId);
const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing roller shutter from cache:', existingAccessory.displayName);
new rollerShutter_1.RollerShutterAccessory(this, existingAccessory);
}
else {
this.log.info('Adding new roller shutter:', rollerShutter.displayName);
const accessory = new this.api.platformAccessory(rollerShutter.displayName, uuid);
accessory.context.device = rollerShutter;
new rollerShutter_1.RollerShutterAccessory(this, accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
for (const multifonctionsModule of nodonMultifonctions) {
const uuid = this.api.hap.uuid.generate(multifonctionsModule.uniqueId);
const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing NodOn multifonctions module from cache:', existingAccessory.displayName);
new nodonMultifonctions_1.NodonMultifonctionsAccessory(this, existingAccessory);
}
else {
this.log.info('Adding new NodOn multifonctions module:', multifonctionsModule.displayName);
const accessory = new this.api.platformAccessory(multifonctionsModule.displayName, uuid);
accessory.context.device = multifonctionsModule;
new nodonMultifonctions_1.NodonMultifonctionsAccessory(this, accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
for (const contactSensor of contactSensors) {
const uuid = this.api.hap.uuid.generate(contactSensor.uniqueId);
const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing Contact Sensor from cache:', existingAccessory.displayName);
new contactSensor_1.ContactSensorAccessory(this, existingAccessory);
}
else {
this.log.info('Adding new Contact Sensor:', contactSensor.displayName);
const accessory = new this.api.platformAccessory(contactSensor.displayName, uuid);
accessory.context.device = contactSensor;
new contactSensor_1.ContactSensorAccessory(this, accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
// Remove undeclared accessories
for (const accessory of Object.values(this.accessories)) {
if (!rollerShutters.find((rollerShutter) => rollerShutter.uniqueId === accessory.context.device.uniqueId) &&
!nodonMultifonctions.find((multifonctionsModule) => multifonctionsModule.uniqueId === accessory.context.device.uniqueId) &&
!contactSensors.find((contactSensor) => contactSensor.uniqueId === accessory.context.device.uniqueId)) {
this.log.info('Removing undeclared accessory:', accessory.displayName);
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
}
}
exports.DeconzConverterPlatform = DeconzConverterPlatform;
//# sourceMappingURL=platform.js.map