UNPKG

@pst-on-npm/homebridge-enocean

Version:
89 lines • 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatelessProgrammableSwitchAccessory = void 0; const ButtonEventManager_1 = require("./ButtonEventManager"); const InformationServiceHelper_1 = require("../serviceHelper/InformationServiceHelper"); const EnoAccessory_1 = require("./EnoAccessory"); class SwitchButton { _platform; _service; _config; _labelIndex; _eventManager; switchEvent = 0; constructor(_platform, _service, _config, _labelIndex) { this._platform = _platform; this._service = _service; this._config = _config; this._labelIndex = _labelIndex; // Each service must implement at-minimum the "required characteristics" for the given service type // see https://developers.homebridge.io/#/service/StatelessProgrammableSwitch this._service.getCharacteristic(_platform.Characteristic.ServiceLabelIndex).setValue(this._labelIndex); this._service.getCharacteristic(_platform.Characteristic.ProgrammableSwitchEvent).onGet(() => { return this.switchEvent; }); this._eventManager = new ButtonEventManager_1.ButtonEventManager((switchEvent) => { _platform.log.info(`${this._service.displayName}: ` + `${switchEvent === 1 ? 'DOUBLE_PRESS' : switchEvent === 0 ? 'SINGLE_PRESS' : 'LONG_PRESS'}`); this.switchEvent = switchEvent; this._service.getCharacteristic(this._platform.Characteristic.ProgrammableSwitchEvent) .sendEventNotification(switchEvent); }, this._platform.api.hap); } buttonPressed() { this._eventManager.buttonPressed(); } buttonReleased() { this._eventManager.buttonReleased(); } } /** * EnoSwitchAccessory * An instance of this class is created for each Switch */ class StatelessProgrammableSwitchAccessory extends EnoAccessory_1.EnoAccessory { _gateway; _switchButtons = new Map(); _buttonSubTypes = ['AI', 'A0', 'BI', 'B0']; constructor(platform, accessory, config) { super(platform, accessory, config); // Set accessory information service InformationServiceHelper_1.InformationServiceHelper.setupService(platform, accessory, config); // Create the four buttons as StatelessProgrammableSwitch services for (let i = 0; i < this._buttonSubTypes.length; i++) { const name = config.name + ' ' + this._buttonSubTypes[i]; const service = accessory.getServiceById(this.platform.Service.StatelessProgrammableSwitch, this._buttonSubTypes[i]) || accessory.addService(this.platform.Service.StatelessProgrammableSwitch, name, this._buttonSubTypes[i]); service.displayName = name; service.updateCharacteristic(this.platform.Characteristic.Name, name); const button = new SwitchButton(platform, service, config, i + 1); this._switchButtons.set(this._buttonSubTypes[i], button); } } async setGateway(gateway) { this._gateway = gateway; // Register message receiver in the gateway await this._gateway.registerEnoAccessory(this.config, this.EnoGateway_eepMessageReceived.bind(this)); } EnoGateway_eepMessageReceived(message) { this.platform.log.debug(`${this.accessory.displayName}: ${message.toString()}`); if (message.values.isPressed && message.values.buttons) { for (const mButton of message.values.buttons) { const button = this._switchButtons.get(mButton); if (!button) { this.platform.log.warn(`${mButton}: no such button`); continue; } button.buttonPressed(); } } else { // Always release all buttons, as we don't know which one was released for (const button of this._switchButtons.values()) { button.buttonReleased(); } } } } exports.StatelessProgrammableSwitchAccessory = StatelessProgrammableSwitchAccessory; //# sourceMappingURL=StatelessProgrammableSwitchAccessory.js.map