UNPKG

homebridge-gpio-sensor-door

Version:

Raspberry Pi GPIO Doorsensor plugin for Homebridge forked from homebridge-gpio-garagedoor-d

54 lines 2.05 kB
"use strict"; const GPIOPort_1 = require("./GPIOPort"); const DoorStateExtension_1 = require("./DoorStateExtension"); var Characteristic; class DoorSensorPort extends GPIOPort_1.GPIOPort { constructor(pin, service, log, isNCSensor) { super(pin, 'in', 'both'); this.service = service; this.log = log; this.closedSensorValue = isNCSensor ? 0 : 1; var self = this; this.watch(function (err, value) { if (err) { log.error(err); return; } self.isClosed = value == self.closedSensorValue; self.handleStateChange(); }); this.reset(); } static init(exportTypes) { Characteristic = exportTypes.Characteristic; } handleStateChange() { var currentState = DoorStateExtension_1.getCurrentDoorState(this.service); switch (currentState) { case Characteristic.CurrentDoorState.CLOSING: case Characteristic.CurrentDoorState.OPENING: return; default: this.updateCurrentDoorState(); } // Handle external state change. var targetState = DoorStateExtension_1.getTargetDoorState(this.service); if ((this.isClosed && targetState == Characteristic.TargetDoorState.OPEN) || (!this.isClosed && targetState == Characteristic.TargetDoorState.CLOSED)) { this.service.getCharacteristic(Characteristic.TargetDoorState) .setValue(this.isClosed ? Characteristic.TargetDoorState.CLOSED : Characteristic.TargetDoorState.OPEN); } } ; reset() { this.isClosed = this.getState() == this.closedSensorValue; this.updateCurrentDoorState(); } ; updateCurrentDoorState() { this.service.getCharacteristic(Characteristic.CurrentDoorState) .setValue(this.isClosed ? Characteristic.CurrentDoorState.CLOSED : Characteristic.CurrentDoorState.OPEN); } ; } exports.DoorSensorPort = DoorSensorPort;