UNPKG

homebridge-hca

Version:

HCA plugin for Homebridge

102 lines (74 loc) 3.03 kB
// 'use strict'; const inherits = require('util').inherits; const AccessoryBase = require('../AccessoryBase'); // const itemType = require('node-hca/lib/Design/itemType'); class HcaOutlet extends AccessoryBase { constructor(log, item, client) { super(log, item, client); } identify(paired, callback) { const item = this.context.item; this.log.info('Identifying %s (%s).', this.displayName, item.id); callback(); } } function init(accessory) { const service = accessory.getService(Service.Outlet) || accessory.addService(Service.Outlet, accessory.displayName); service .getCharacteristic(Characteristic.On) .onGet(getPowerState.bind(accessory)) .onSet(setPowerState.bind(accessory)); bindUpdates(accessory); } // Inform HomeKit about changes that occurred outside of HomeKit function bindUpdates(accessory) { const item = accessory.context.item; accessory.client.designManager .on('Design:Updated:' + item.id, function (e) { const lastKnownState = accessory.context.lastKnownState; const currentState = item.state; // Ignore duplicate messages. (HCA may send the same message when device or room state changes, based on a change from the other). if (lastKnownState === currentState) return; accessory.context.lastKnownState = currentState; const isReachable = e.errorState == 0; const isOn = item.state > 0; accessory.log.debug('%s (%s) has been updated:', accessory.displayName, item.id, JSON.stringify(item)); if (!isReachable) accessory.log.warn('%s (%s) did not acknowledge receipt of this request.', accessory.displayName, item.id) accessory.log.info("%s (%s) has been turned %s.", accessory.displayName, item.id, isOn ? "on" : "off"); accessory.log.debug('Updating %s (%s) power state to \'%s\'.', accessory.displayName, item.id, isOn); accessory .getService(Service.Outlet) .getCharacteristic(Characteristic.On) .updateValue(isOn); }); } async function getPowerState() { const accessory = this; const item = accessory.context.item; const isOn = item.state > 0; accessory.log.debug("%s (%s) is %s.", accessory.displayName, item.id, isOn ? "on" : "off"); return Promise.resolve(isOn); } async function setPowerState(value) { const accessory = this; const item = accessory.context.item; const targetState = Boolean(value); const command = targetState == true ? 'Device.On' : 'Device.Off', params = ['HCAObject', command, item.id]; accessory.log("Turning %s (%s) %s.", accessory.displayName, item.id, targetState ? "on" : "off"); accessory.client.send(params); return Promise.resolve(); } module.exports = function (accessory, service, characteristic, ouuid) { this.Accessory = accessory; this.Service = service; this.Characteristic = characteristic; this.uuid = ouuid; inherits(HcaOutlet, Accessory); return HcaOutlet; }; module.exports.HcaOutlet = HcaOutlet; module.exports.HcaOutlet.init = init;