UNPKG

homebridge-hca

Version:

HCA plugin for Homebridge

119 lines (91 loc) 4.17 kB
// 'use strict'; const inherits = require('util').inherits; const AccessoryBase = require('../AccessoryBase'); class HcaGarageDoorOpener extends AccessoryBase { constructor(log, item, client) { super(log, item, client); } init() { const self = this; const item = this.item; const client = this.client; this .addService(Service.GarageDoorOpener, this.name) .getCharacteristic(Characteristic.CurrentDoorState) .on('get', function (callback) { self.getCurrentDoorState(callback); }); this .getService(Service.GarageDoorOpener) .getCharacteristic(Characteristic.TargetDoorState) .on('get', function (callback) { self.getTargetDoorState(callback); }); this .getService(Service.GarageDoorOpener) .getCharacteristic(Characteristic.TargetDoorState) .on('set', function (value, callback) { self.setTargetDoorState(value, callback); }); } // Inform HomeKit about changes that occurred outside of HomeKit bindUpdates(accessory) { const me = accessory || this; const item = me.context.item; me.client.designManager .on('Design:Updated:' + item.id, function (e) { const isReachable = e.errorState == 0; const isClosed = item.state == 100; // const targetDoorState = item.state == 100 ? Characteristic.TargetDoorState.CLOSED : Characteristic.TargetDoorState.OPEN; me.log.debug('%s (%s) has been updated:', me.name, item.id, JSON.stringify(item)); if (!isReachable) me.log.warn('%s (%s) did not acknowledge receipt of this request.', me.name, item.id) me.log("%s (%s) has been %s.", me.name, item.id, isClosed ? "closed" : "opened"); me.log.debug('Updating %s (%s) door state to \'%s\'.', me.name, item.id, isClosed); me .getService(Service.GarageDoorOpener) .getCharacteristic(Characteristic.CurrentDoorState) .updateValue(isClosed); // me // .getService(Service.GarageDoorOpener) // .getCharacteristic(Characteristic.TargetDoorState) // .updateValue(targetDoorState); }); } identify(paired, callback) { const item = this.context.item; this.log('Identifying %s (%s).', this.name, item.id); callback(); } getCurrentDoorState(callback) { const item = this.context.item; const currentDoorState = item.state == 100 ? Characteristic.CurrentDoorState.CLOSED : Characteristic.CurrentDoorState.OPEN; const isClosed = item.state == 100; this.log("%s (%s) is %s.", this.name, this.item.id, isClosed ? "closed" : "open"); callback(null, currentDoorState); } getTargetDoorState(callback) { const item = this.context.item; const targetDoorState = item.state == 100 ? Characteristic.TargetDoorState.CLOSED : Characteristic.TargetDoorState.OPEN; const isClosing = item.state == 100; this.log("%s (%s) is %s.", this.name, item.id, isClosing ? "closing" : "opening"); callback(null, targetDoorState); } setTargetDoorState(value, callback) { const item = this.context.item; const isClosing = value == Characteristic.TargetDoorState.CLOSED; this.log("%s (%s) is %s.", this.name, item.id, isClosing ? "closing" : "opening"); // TODO: Implement HCA action. callback(); } } module.exports = function (accessory, service, characteristic, ouuid) { Accessory = accessory; Service = service; Characteristic = characteristic; uuid = ouuid; inherits(HcaGarageDoorOpener, Accessory); return HcaGarageDoorOpener; }; module.exports.HcaGarageDoorOpener = HcaGarageDoorOpener; module.exports.HcaGarageDoorOpener.bindUpdates = HcaGarageDoorOpener.prototype.bindUpdates;