UNPKG

homebridge-hca

Version:

HCA plugin for Homebridge

124 lines (103 loc) 4.08 kB
const itemType = require('node-hca/lib/Design/itemType'); const itemKind = require('node-hca/lib/Design/itemKind'); const controllerKind = require('node-hca/lib/Design/controllerKind'); let Accessory, Service, Characteristic, uuid; let self; const AccessoryFactory = function (Homebridge, log, client) { self = this; this.log = log; this.client = client; Accessory = Homebridge.platformAccessory; Service = Homebridge.hap.Service; Characteristic = Homebridge.hap.Characteristic; uuid = Homebridge.hap.uuid; HcaFan = require('./accessories/Fan')(Accessory, Service, Characteristic, uuid); HcaGarageDoorOpener = require('./accessories/GarageDoorOpener')(Accessory, Service, Characteristic, uuid); HcaLeakSensor = require('./accessories/LeakSensor')(Accessory, Service, Characteristic, uuid); HcaLightbulb = require('./accessories/Lightbulb')(Accessory, Service, Characteristic, uuid); HcaMotionSensor = require('./accessories/MotionSensor')(Accessory, Service, Characteristic, uuid); HcaOutlet = require('./accessories/Outlet')(Accessory, Service, Characteristic, uuid); HcaSwitch = require('./accessories/Switch')(Accessory, Service, Characteristic, uuid); }; AccessoryFactory.prototype.getFactory = function (item) { let factory; switch (item.kind) { case itemKind.fan: factory = HcaFan; break; case itemKind.test_garage: // factory = HcaGarageDoorOpener; break; case itemKind.light: factory = HcaLightbulb; break; case itemKind.module: if (item.isDimmable) { factory = HcaLightbulb; break; } factory = HcaOutlet; break; case itemKind.switch: case itemKind.keypadWithLoad: case itemKind.irOutput: if (item.isDimmable) { factory = HcaLightbulb; break; } factory = HcaSwitch; break; case itemKind.input: if (item.subType === controllerKind.motionSensor) { factory = HcaMotionSensor; break; } if (item.subType === controllerKind.other) { if (item.model.indexOf('2852-222') > -1) { // leak sensor factory = HcaLeakSensor; break; } break; } break; case itemKind.lock: // not supported case itemKind.camera: // not supported case itemKind.keypad: // not supported case itemKind.thermostat: // not supported case itemKind.other: // not supported default: break; } return factory; } AccessoryFactory.prototype.restoreAccessory = function (accessory) { const item = accessory.context.item; const factory = this.getFactory(item); if (!factory) return; factory.init(accessory); }; AccessoryFactory.prototype.createAccessory = function (item) { const factory = this.getFactory(item); if (!factory) return; const accessory = new factory(self.log, item, self.client); if (!accessory) return; // TODO: Remove condition after all factory methonds have been updated. if (factory.init) factory.init(accessory); return accessory; }; // module.exports = function (accessory, service, characteristic, ouuid) { // Accessory = accessory; // Service = service; // Characteristic = characteristic; // uuid = ouuid; // HcaFan = require('./accessories/Fan')(Accessory, Service, Characteristic, uuid); // HcaGarageDoorOpener = require('./accessories/GarageDoorOpener')(Accessory, Service, Characteristic, uuid); // HcaLeakSensor = require('./accessories/LeakSensor')(Accessory, Service, Characteristic, uuid); // HcaLightbulb = require('./accessories/Lightbulb')(Accessory, Service, Characteristic, uuid); // HcaMotionSensor = require('./accessories/MotionSensor')(Accessory, Service, Characteristic, uuid); // HcaOutlet = require('./accessories/Outlet')(Accessory, Service, Characteristic, uuid); // HcaSwitch = require('./accessories/Switch')(Accessory, Service, Characteristic, uuid); // return AccessoryFactory; // } module.exports = AccessoryFactory; // module.exports.AccessoryFactory = AccessoryFactory;