UNPKG

@homebridge-plugins/homebridge-aladdin-connect

Version:
221 lines 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GenieAladdinConnectGarageDoorAccessory = void 0; const aladdinConnect_1 = require("./aladdinConnect"); const settings_1 = require("./settings"); class GenieAladdinConnectGarageDoorAccessory { platform; accessory; log; hap; aladdinConnect; context; door; id; targetStateCharacteristic; currentStateCharacteristic; obstructionDetectedCharacteristic; batteryLevelCharacteristic = null; statusLowBatteryCharacteristic = null; _currentStatus = aladdinConnect_1.AladdinDoorStatus.UNKNOWN; _desiredStatus = aladdinConnect_1.AladdinDesiredDoorStatus.NONE; _obstructionDetected = false; _batteryLevel = 100; _statusLowBattery = false; constructor(platform, accessory) { this.platform = platform; this.accessory = accessory; this.log = this.platform.log; this.hap = this.platform.api.hap; this.aladdinConnect = this.platform.aladdinConnect; this.context = this.accessory.context; this.door = this.context.door; this.id = `${this.door.deviceId}:${this.door.index}`; this.accessory .getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Genie') .setCharacteristic(this.platform.Characteristic.Model, 'Aladdin Connect') .setCharacteristic(this.platform.Characteristic.SerialNumber, this.door.id); const service = (this.accessory.getService(this.platform.Service.GarageDoorOpener) || this.accessory.addService(this.platform.Service.GarageDoorOpener, this.door.name, this.id)).setCharacteristic(this.platform.Characteristic.Name, this.door.name); this.targetStateCharacteristic = service .getCharacteristic(this.platform.Characteristic.TargetDoorState) .onSet(this.setTargetDoorState.bind(this)) .onGet(this.getTargetDoorState.bind(this)); this.currentStateCharacteristic = service .getCharacteristic(this.platform.Characteristic.CurrentDoorState) .onGet(this.getCurrentDoorState.bind(this)); this.obstructionDetectedCharacteristic = service .getCharacteristic(this.platform.Characteristic.ObstructionDetected) .onGet(this.getObstructionDetected.bind(this)); if (this.door.hasBatteryLevel) { this.batteryLevelCharacteristic = service .getCharacteristic(this.platform.Characteristic.BatteryLevel) .onGet(this.getBatteryLevel.bind(this)); this.statusLowBatteryCharacteristic = service .getCharacteristic(this.platform.Characteristic.StatusLowBattery) .onGet(this.getStatusLowBattery.bind(this)); } this.aladdinConnect.subscribe(this.door, (info) => { this.currentStatus = info.status; this.obstructionDetected = // A fault happens when a door fails to open/close twice in a row and then must be operated // manually. info.fault || // If the door does not go into its desired state after some timeout it goes into one of // these states. The first time it is recoverable. [aladdinConnect_1.AladdinDoorStatus.TIMEOUT_CLOSING, aladdinConnect_1.AladdinDoorStatus.TIMEOUT_OPENING].includes(info.status); if (this.door.hasBatteryLevel && info.batteryPercent !== null) { this.batteryLevel = info.batteryPercent; } this.desiredStatus = this.convertStatusToDesiredStatus(this.currentStatus); }); } get currentStatus() { return this._currentStatus; } set currentStatus(value) { if (this._currentStatus === value) { return; } this.log.debug('[%s] Update Characteristic CurrentDoorState: %s -> %s', this.door.name, aladdinConnect_1.AladdinDoorStatus[this._currentStatus], aladdinConnect_1.AladdinDoorStatus[value]); this._currentStatus = value; this.currentStateCharacteristic.updateValue(this.convertStatusToCurrentStateValue(this._currentStatus)); } get desiredStatus() { return this._desiredStatus; } set desiredStatus(value) { if (this._desiredStatus === value) { return; } this.log.debug('[%s] Update Characteristic TargetDoorState: %s -> %s', this.door.name, aladdinConnect_1.AladdinDesiredDoorStatus[this._desiredStatus], aladdinConnect_1.AladdinDesiredDoorStatus[value]); this._desiredStatus = value; this.targetStateCharacteristic.updateValue(this.convertDesiredStatusToTargetStateValue(this._desiredStatus)); } get obstructionDetected() { return this._obstructionDetected; } set obstructionDetected(value) { if (this._obstructionDetected === value) { return; } this.log.debug('[%s] Update Characteristic ObstructionDetected: %s -> %s', this.door.name, this._obstructionDetected ? 'YES' : 'NO', value ? 'YES' : 'NO'); this._obstructionDetected = value; this.obstructionDetectedCharacteristic.updateValue(this._obstructionDetected); } get batteryLevel() { return this._batteryLevel; } set batteryLevel(value) { const batteryLevel = Math.min(100, Math.max(0, value)); const statusLowBattery = batteryLevel <= (this.platform.config?.batteryLowLevel ?? settings_1.DEFAULT_STATUS_LOW_BATTERY_PERCENT); if (this._batteryLevel !== batteryLevel && this.batteryLevelCharacteristic !== null) { this.log.debug('[%s] Update Characteristic BatteryLevel: %s -> %s', this.door.name, this._batteryLevel, batteryLevel); this.batteryLevelCharacteristic.updateValue(batteryLevel); } this._batteryLevel = batteryLevel; if (this._statusLowBattery !== statusLowBattery && this.statusLowBatteryCharacteristic !== null) { this.log.debug('[%s] Update Characteristic StatusLowBattery: %s -> %s', this.door.name, this._statusLowBattery ? 'YES' : 'NO', statusLowBattery ? 'YES' : 'NO'); this.statusLowBatteryCharacteristic.updateValue(this._statusLowBattery); } this._statusLowBattery = statusLowBattery; } async setTargetDoorState(value) { const desiredStatus = this.convertTargetStateValueToDesiredStatus(value); if (desiredStatus === this.desiredStatus) { this.log.debug('[%s] Set Characteristic TargetDoorState -> %s, already set TargetDoorState -> %s. Cancelling.', this.door.name, aladdinConnect_1.AladdinDesiredDoorStatus[desiredStatus], aladdinConnect_1.AladdinDesiredDoorStatus[this.desiredStatus]); return; } this.log.debug('[%s] Set Characteristic TargetDoorState ->', this.door.name, aladdinConnect_1.AladdinDesiredDoorStatus[desiredStatus]); try { await this.aladdinConnect.setDoorStatus(this.door, desiredStatus); } catch (error) { throw new this.hap.HapStatusError(-70402 /* this.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */); } } async getTargetDoorState() { this.log.debug('[%s] Get Characteristic TargetDoorState ->', this.door.name, aladdinConnect_1.AladdinDesiredDoorStatus[this.desiredStatus]); return this.convertDesiredStatusToTargetStateValue(this.desiredStatus); } getCurrentDoorState() { this.log.debug('[%s] Get Characteristic CurrentDoorState ->', this.door.name, aladdinConnect_1.AladdinDoorStatus[this.currentStatus]); return this.convertStatusToCurrentStateValue(this.currentStatus); } getObstructionDetected() { this.log.debug('[%s] Get Characteristic ObstructionDetected ->', this.door.name, this.obstructionDetected ? 'YES' : 'NO'); return this.obstructionDetected; } getBatteryLevel() { this.log.debug('[%s] Get Characteristic BatteryLevel ->', this.door.name, this.batteryLevel); return this.batteryLevel; } getStatusLowBattery() { this.log.debug('[%s] Get Characteristic StatusLowBattery ->', this.door.name, this._statusLowBattery ? 'YES' : 'NO'); return this._statusLowBattery; } convertTargetStateValueToDesiredStatus(value) { switch (value) { case this.platform.Characteristic.TargetDoorState.OPEN: return aladdinConnect_1.AladdinDesiredDoorStatus.OPEN; case this.platform.Characteristic.TargetDoorState.CLOSED: return aladdinConnect_1.AladdinDesiredDoorStatus.CLOSED; default: this.log.debug('[%s] Unknown TargetDoorState Characteristic value -> %d', this.door.name, value); throw new this.hap.HapStatusError(-70410 /* this.hap.HAPStatus.INVALID_VALUE_IN_REQUEST */); } } convertStatusToCurrentStateValue(status) { switch (status) { case aladdinConnect_1.AladdinDoorStatus.OPEN: return this.platform.Characteristic.CurrentDoorState.OPEN; case aladdinConnect_1.AladdinDoorStatus.OPENING: return this.platform.Characteristic.CurrentDoorState.OPENING; case aladdinConnect_1.AladdinDoorStatus.CLOSED: return this.platform.Characteristic.CurrentDoorState.CLOSED; case aladdinConnect_1.AladdinDoorStatus.CLOSING: return this.platform.Characteristic.CurrentDoorState.CLOSING; case aladdinConnect_1.AladdinDoorStatus.TIMEOUT_OPENING: case aladdinConnect_1.AladdinDoorStatus.TIMEOUT_CLOSING: case aladdinConnect_1.AladdinDoorStatus.UNKNOWN: case aladdinConnect_1.AladdinDoorStatus.NOT_CONFIGURED: return this.platform.Characteristic.CurrentDoorState.STOPPED; default: this.log.debug('[%s] Unknown Aladdin door status -> %d', this.door.name, status); throw new this.hap.HapStatusError(-70410 /* this.hap.HAPStatus.INVALID_VALUE_IN_REQUEST */); } } convertDesiredStatusToTargetStateValue(status) { switch (status) { case aladdinConnect_1.AladdinDesiredDoorStatus.OPEN: return this.platform.Characteristic.TargetDoorState.OPEN; case aladdinConnect_1.AladdinDesiredDoorStatus.CLOSED: return this.platform.Characteristic.TargetDoorState.CLOSED; default: this.log.debug('[%s] Unknown Aladdin door desired status -> %d', this.door.name, status); throw new this.hap.HapStatusError(-70410 /* this.hap.HAPStatus.INVALID_VALUE_IN_REQUEST */); } } convertStatusToDesiredStatus(status) { switch (status) { case aladdinConnect_1.AladdinDoorStatus.OPEN: case aladdinConnect_1.AladdinDoorStatus.OPENING: case aladdinConnect_1.AladdinDoorStatus.TIMEOUT_OPENING: return aladdinConnect_1.AladdinDesiredDoorStatus.OPEN; case aladdinConnect_1.AladdinDoorStatus.CLOSED: case aladdinConnect_1.AladdinDoorStatus.CLOSING: case aladdinConnect_1.AladdinDoorStatus.TIMEOUT_CLOSING: return aladdinConnect_1.AladdinDesiredDoorStatus.CLOSED; case aladdinConnect_1.AladdinDoorStatus.UNKNOWN: case aladdinConnect_1.AladdinDoorStatus.NOT_CONFIGURED: return aladdinConnect_1.AladdinDesiredDoorStatus.NONE; default: this.log.debug('[%s] Unknown Aladdin door status -> %d', this.door.name, status); throw new this.hap.HapStatusError(-70410 /* this.hap.HAPStatus.INVALID_VALUE_IN_REQUEST */); } } } exports.GenieAladdinConnectGarageDoorAccessory = GenieAladdinConnectGarageDoorAccessory; //# sourceMappingURL=platformAccessory.js.map