@pst-on-npm/homebridge-enocean
Version:
Integrate EnOcean® devices into Homebridge.
74 lines • 3.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LearnSwitchAccessory = void 0;
/**
* Switch accessory to turn the learn (teach-in) mode on and off.
* If turned on it will automatically turn off after 3 minutes.
*/
class LearnSwitchAccessory {
platform;
accessory;
_gateway;
service;
state = {
On: false,
};
static Device = {
name: 'EnOcean Learn',
id: 'enocean-learn-switch-0815',
};
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'pst-on-github')
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, platform.version);
// Get the Switch service if it exists, otherwise create a new Switch service
// you can create multiple services for each accessory
this.service = this.accessory.getService(this.platform.Service.Switch)
|| this.accessory.addService(this.platform.Service.Switch);
// set the service name, this is what is displayed as the default name on the Home app
// in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
this.service.setCharacteristic(this.platform.Characteristic.Name, 'EnOcean Learn');
// each service must implement at-minimum the "required characteristics" for the given service type
// see https://developers.homebridge.io/#/service/Switch
// register handlers for the On/Off Characteristic
this.service.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this)) // SET - bind to the `setOn` method below
.onGet(this.getOn.bind(this)); // GET - bind to the `getOn` method below
}
async setGateway(gateway) {
this._gateway = gateway;
// Register for stopLearning event
this._gateway.addStoppedLearningEventListener(() => {
this.state.On = false;
this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On);
});
}
/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
*/
async setOn(value) {
this.state.On = value;
if (this._gateway !== undefined) {
if (this.state.On) {
this._gateway.startLearning(120);
}
else {
this._gateway.stopLearning();
}
}
}
/**
* Handle the "GET" requests from HomeKit
* These are sent when HomeKit wants to know the current state of the accessory, for example, checking if a Light bulb is on.
*/
async getOn() {
const isOn = this.state.On;
return isOn;
}
}
exports.LearnSwitchAccessory = LearnSwitchAccessory;
//# sourceMappingURL=LearnSwitchAccessory.js.map