homebridge-programmable-http-switch
Version:
A Homebridge plugin that allows users to create Stateless Programmable Switches which can be controlled using a HTTP API.
59 lines (58 loc) • 3.06 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("./types");
var const_1 = require("./const");
var ProgrammableHTTPSwitchAccessory = /** @class */ (function () {
function ProgrammableHTTPSwitchAccessory(log, config, hap) {
var _this = this;
this.programmableSwitchServices = {};
this.getServices = function () {
return __spreadArray(__spreadArray([], Object.values(_this.programmableSwitchServices), true), [
_this.informationService
], false);
};
this.identify = function () {
_this.log.info("Identify triggered for accessory '" + _this.name + "'");
};
this.createService = function (button, index) {
var _a;
var service = new _this.hap.Service.StatelessProgrammableSwitch(button.name, button.identifier);
service.getCharacteristic(_this.hap.Characteristic.ProgrammableSwitchEvent)
.setProps({
validValues: ((_a = button.supportedActions) === null || _a === void 0 ? void 0 : _a.map(function (action) { return typeof action != 'number' ? types_1.Action[action] : action; })) || [0, 1, 2]
});
service.setCharacteristic(_this.hap.Characteristic.ServiceLabelIndex, index + 1);
return service;
};
this.setState = function (buttonIdentifier, action) {
_this.programmableSwitchServices[buttonIdentifier]
.setCharacteristic(_this.hap.Characteristic.ProgrammableSwitchEvent, action);
};
this.config = config;
this.log = log;
this.hap = hap;
this.name = this.config.name;
for (var _i = 0, _a = Object.entries(this.config.buttons); _i < _a.length; _i++) {
var _b = _a[_i], indexString = _b[0], button = _b[1];
var index = Number(indexString);
this.programmableSwitchServices[button.identifier] = this.createService(button, index);
}
this.informationService = new this.hap.Service.AccessoryInformation()
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(this.hap.Characteristic.SerialNumber, this.config.identifier)
.setCharacteristic(this.hap.Characteristic.Model, 'Programmable HTTP Switch')
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, const_1.VERSION);
this.log.info("Successfully initialized ProgrammableHTTPSwitch accessory '" + this.config.name + "'");
}
return ProgrammableHTTPSwitchAccessory;
}());
exports.default = ProgrammableHTTPSwitchAccessory;