UNPKG

@uboness/homebridge-unifi-access

Version:
107 lines 5.47 kB
"use strict"; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.Door = void 0; const Device_js_1 = require("./Device.js"); var Door; (function (Door) { Door.create = async (platform, client, accessory, device) => { const asGarageDoor = accessory.context.asGarageDoor; if (asGarageDoor) { return GarageDoor.create(platform, client, accessory, device); } return Lock.create(platform, client, accessory, device); }; })(Door || (exports.Door = Door = {})); class Lock extends Device_js_1.Device { constructor(platform, client, accessory, device) { var _c; super(platform, client, accessory, device, (_c = accessory.getService(platform.Service.LockMechanism)) !== null && _c !== void 0 ? _c : accessory.addService(platform.Service.LockMechanism)); this.device.locked = true; this.targetState = this.primaryService.getCharacteristic(platform.Characteristic.LockTargetState) .setValue(this.device.locked ? platform.Characteristic.LockTargetState.SECURED : platform.Characteristic.LockTargetState.UNSECURED) .onSet(async (value, context) => { if (!(context === null || context === void 0 ? void 0 : context.fromUnifi)) { const locked = value === platform.Characteristic.LockTargetState.SECURED; if (!locked) { await client.unlockDoor(device.id); } } }); this.currentState = this.primaryService.getCharacteristic(platform.Characteristic.LockCurrentState) .setValue(platform.Characteristic.LockCurrentState.SECURED); } update(door) { this.device.locked = door.locked; } async doClose() { } onMessage(msg, platform) { if (msg.type === 'door-update' && msg.id === this.device.id) { if (msg.locked !== undefined) { this.device.locked = msg.locked; const currestState = msg.locked ? platform.Characteristic.LockCurrentState.SECURED : platform.Characteristic.LockCurrentState.UNSECURED; this.currentState.setValue(currestState, { fromUnifi: true }); const targetState = msg.locked ? platform.Characteristic.LockTargetState.SECURED : platform.Characteristic.LockTargetState.UNSECURED; this.targetState.setValue(targetState, { fromUnifi: true }); } this.statusFault.setValue(!msg.available); } } } _a = Lock; Lock.create = async (platform, client, accessory, device) => { return new _a(platform, client, accessory, device); }; class GarageDoor extends Device_js_1.Device { constructor(platform, client, accessory, device) { var _c; super(platform, client, accessory, device, (_c = accessory.getService(platform.Service.GarageDoorOpener)) !== null && _c !== void 0 ? _c : accessory.addService(platform.Service.GarageDoorOpener)); this.device.locked = true; this.targetState = this.primaryService.getCharacteristic(platform.Characteristic.TargetDoorState) .setValue(this.device.locked ? platform.Characteristic.TargetDoorState.CLOSED : platform.Characteristic.TargetDoorState.OPEN) .onSet(async (value) => { const open = value === platform.Characteristic.TargetDoorState.OPEN; if (open) { setTimeout(() => this.targetState.setValue(platform.Characteristic.TargetDoorState.CLOSED), 500); this.currentState.setValue(platform.Characteristic.CurrentDoorState.OPENING); await client.unlockDoor(device.id); } }); this.currentState = this.primaryService.getCharacteristic(platform.Characteristic.CurrentDoorState) .setValue(platform.Characteristic.CurrentDoorState.CLOSED) .onSet(async (value) => { // looks like, when the trigger duration of the gate hub is set too low (e.g. 0.5 second), the // "locked" event is not sent. We want to make sure the current state is always reset to "closed", so // we'll set a timer to resent it anyway after 10 seconds if (value === platform.Characteristic.CurrentDoorState.OPEN) { setTimeout(() => { if (this.currentState.value !== platform.Characteristic.CurrentDoorState.CLOSED) { this.logger.debug(`looks like the 'locked' event didn't arrive, setting current state to 'closed' anyway.`); this.currentState.setValue(platform.Characteristic.CurrentDoorState.CLOSED); } }, 10000); } }); } update(door) { this.device.locked = door.locked; } async doClose() { } onMessage(msg, platform) { if (msg.type === 'door-update' && msg.id === this.device.id) { if (msg.locked !== undefined) { this.device.locked = msg.locked; const currestState = msg.locked ? platform.Characteristic.CurrentDoorState.CLOSED : platform.Characteristic.CurrentDoorState.OPEN; this.currentState.setValue(currestState); } this.statusFault.setValue(!msg.available); } } } _b = GarageDoor; GarageDoor.create = async (platform, client, accessory, device) => { return new _b(platform, client, accessory, device); }; //# sourceMappingURL=Door.js.map