homebridge-rpiradio-light
Version:
A Homebridge plugin to control 433mhz radio
58 lines (48 loc) • 1.78 kB
JavaScript
const RateLimiter = require('limiter').RateLimiter;
const rpi433 = require('rpi-433-v2'),
rfEmitter = rpi433.emitter({
pin: 7, //Send through GPIO 0 (or Physical PIN 11)
pulseLength: 350, //Send the code with a 350 pulse length
});
let Service,
Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-rpiradio-light', '433Light', mhz433light);
};
function mhz433light(log, config) {
this.log = log;
this.name = config['name'];
this.off_payload = config['off_payload'];
this.on_payload = config['on_payload'];
this.currentState = false;
this.limiter = new RateLimiter(1, 700); //limit requests to one per 200ms
this.lockservice = new Service.Lightbulb(this.name);
this.lockservice
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
}
mhz433light.prototype.getState = function (callback) {
this.log('current lock state is ' + this.currentState);
callback(null, this.currentState);
};
mhz433light.prototype.setState = function (state, callback) {
this.limiter.removeTokens(1, () => {
this.log('Set state to ', state);
rfEmitter.sendCode((state ? this.on_payload : this.off_payload), (error, stdout) => {
if (error) {
callback('error');
} else {
this.log('STDOUT: ' + stdout);
this.log('Success ', (state ? 'on' : 'off'));
this.currentState = state;
callback(null);
}
});
});
};
mhz433light.prototype.getServices = function () {
return [this.lockservice];
};