UNPKG

homebridge-yalehome

Version:
162 lines 8.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YaleSyncAlarmPlatformAccessory = void 0; const Model_1 = require("@jagcesar/yalesyncalarm/dist/Model"); /** * Platform Accessory * An instance of this class is created for each accessory your platform registers * Each accessory may expose multiple services of different service types. */ class YaleSyncAlarmPlatformAccessory { constructor(platform, accessory) { this.platform = platform; this.accessory = accessory; /** * These are just used to create a working example * You should implement your own code to track the state of your accessory */ this.ArmStates = { arm: this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM, disarm: this.platform.Characteristic.SecuritySystemTargetState.DISARM, home: this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM, }; this.stateToPanelState = (state) => { switch (state) { case state === this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM: return "arm" /* Armed */; break; case state === this.platform.Characteristic.SecuritySystemTargetState.DISARM: return "disarm" /* Disarmed */; break; case state === this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM: // Yale uses Home arm for both 'home' and 'night' return "home" /* Home */; break; case state === this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM: return "home" /* Home */; break; default: return "home" /* Home */; break; } }; this.stateToDoorLockState = (state) => { switch (state) { case state === this.platform.Characteristic.LockTargetState.UNSECURED: return Model_1.DoorLock.State.unlocked; case state === this.platform.Characteristic.LockTargetState.SECURED: return Model_1.DoorLock.State.locked; default: return Model_1.DoorLock.State.unlocked; } }; this.AccessoryTypes = { panel: this.platform.Service.SecuritySystem, motionSensor: this.platform.Service.MotionSensor, contactSensor: this.platform.Service.ContactSensor, doorLock: this.platform.Service.LockMechanism, }; const { name, type } = accessory.context.device; this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Yale') .setCharacteristic(this.platform.Characteristic.Model, type) .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.UUID); // eslint-disable-next-line max-len this.service = this.accessory.getService(this.AccessoryTypes[type]) || this.accessory.addService(this.AccessoryTypes[type]); // set the service name, this is what is displayed as the default name on the Home app // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method. this.service.setCharacteristic(this.platform.Characteristic.Name, `${name} ${type}`); // each service must implement at-minimum the "required characteristics" for the given service type // see https://developers.homebridge.io/#/service/Lightbulb // create handlers for required characteristics if (type === 'panel') { this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState) .onGet(this.getPanelState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState) .onSet(this.setPanelState.bind(this)); } else if (type === 'motionSensor') { this.service.getCharacteristic(this.platform.Characteristic.MotionDetected) .onGet(this.getMotionSensorState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.MotionDetected) .onSet(this.setMotionSensorState.bind(this)); } else if (type === 'contactSensor') { this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState) .onGet(this.getContactSensorState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState) .onSet(this.setContactSensorState.bind(this)); } else if (type === 'doorLock') { this.service.getCharacteristic(this.platform.Characteristic.LockCurrentState) .onGet(this.getLockState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.LockTargetState) .onSet(this.setLockState.bind(this)); } } getPanelState() { this.platform.log.debug('Getting Panel State:'); let currentState; this.platform.yaleAPI.getPanelState().then((state) => { currentState = this.ArmStates[state]; }); return currentState; } setPanelState(state) { this.platform.log.info('Setting Panel State:', state); const targetState = this.stateToPanelState(state); this.platform.yaleAPI.setPanelState(targetState); this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState).updateValue(this.ArmStates[state]); } getLockState() { this.platform.log.debug('Getting Door Lock State:'); return this.accessory.context.state ? this.platform.Characteristic.LockTargetState.UNSECURED : this.platform.Characteristic.LockTargetState.SECURED; } setLockState(state) { this.platform.log.info('Setting Lock State:', state); const targetState = this.stateToDoorLockState(state); this.platform.yaleAPI.doorLocks().then(doorLocks => { const doorLock = doorLocks[this.accessory.context.device.identifier]; this.platform.yaleAPI.setDoorLockState(doorLock, targetState).then(() => { this.platform.yaleAPI.update().then(() => { this.platform.yaleAPI.doorLocks().then(doorLocks => { const doorLock = doorLocks[this.accessory.context.device.identifier]; const state = doorLock.state; switch (state) { case 0: this.service.getCharacteristic(this.platform.Characteristic.LockCurrentState).updateValue(this.platform.Characteristic.LockCurrentState.SECURED); break; default: this.service.getCharacteristic(this.platform.Characteristic.LockCurrentState).updateValue(this.platform.Characteristic.LockCurrentState.UNSECURED); break; } }); }); }); }); } getContactSensorState() { this.platform.log.debug('Getting Contact Sensor State:'); return this.accessory.context.state ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; } setContactSensorState(state) { this.platform.log.debug('Setting Contact Sensor State:', state); this.platform.yaleAPI.contactSensors().then(s => { const status = s[this.accessory.context.device.identifier].state; this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(status === 0 ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED); }); } getMotionSensorState() { this.platform.log.debug('Getting Motion Sensor State:'); return this.accessory.context.state !== 0; } setMotionSensorState(state) { this.platform.log.debug('Setting Motion Sensor State:', state); this.platform.yaleAPI.contactSensors().then(s => { const status = s[this.accessory.context.device.identifier].state; this.service.getCharacteristic(this.platform.Characteristic.MotionDetected).updateValue(status === 1); }); } } exports.YaleSyncAlarmPlatformAccessory = YaleSyncAlarmPlatformAccessory; //# sourceMappingURL=platformAccessory.js.map