homebridge-rpi-pico-doorbell
Version:
Use GPIO input from RPi Pico as HomeKit doorbell
76 lines • 3.58 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DoorbellRPiPicoAccessory = void 0;
const node_persist_1 = __importDefault(require("node-persist"));
const express_1 = __importDefault(require("express"));
const app = (0, express_1.default)();
const port = 3333; // default port to listen
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
class DoorbellRPiPicoAccessory {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.doorbellMuteKey = 'homebridge-rpi-pico-doorbell.mute';
this.log.debug('Homebridge Raspberry Pi Pico Doorbell loaded.');
// init storage
const cacheDir = this.api.user.persistPath();
this.storage = node_persist_1.default.create();
this.storage.initSync({ dir: cacheDir, forgiveParseErrors: true });
// add accessory information
this.informationService = new this.api.hap.Service.AccessoryInformation()
.setCharacteristic(this.api.hap.Characteristic.Manufacturer, 'Homebridge')
.setCharacteristic(this.api.hap.Characteristic.Model, 'RPiPicoDoorbell');
// create new doorbell accessory
this.doorbellService = new this.api.hap.Service.Doorbell(this.config.name);
// add characteristic ProgrammableSwitchEvent
this.doorbellService.getCharacteristic(this.api.hap.Characteristic.ProgrammableSwitchEvent);
// setup mute characteristic
if (this.config.enableOutput) {
this.muteCharacteristic = this.doorbellService.getCharacteristic(this.api.hap.Characteristic.Mute);
this.muteCharacteristic.onGet(this.handleMuteGet.bind(this));
this.muteCharacteristic.onSet(this.handleMuteSet.bind(this));
}
// restore persisted settings
this.doorbellMute = this.storage.getItemSync(this.doorbellMuteKey) || false;
this.storage.setItemSync(this.doorbellMuteKey, this.doorbellMute);
this.doorbellService.updateCharacteristic(this.api.hap.Characteristic.Mute, this.doorbellMute);
app.get('/ring', (req, res) => {
this.log.info(`Doorbell "${this.config.name}" rang.`);
// ring in homekit
this.log.info('Forwarding ring directly to HomeKit.');
this.doorbellService.updateCharacteristic(this.api.hap.Characteristic.ProgrammableSwitchEvent, this.api.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
this.log.debug('received get request!!!');
res.sendStatus(200);
});
// start the express server
app.listen(port, () => {
// tslint:disable-next-line:no-console
this.log.debug(`server started at http://localhost:${port}`);
});
}
getServices() {
return [
this.informationService,
this.doorbellService,
];
}
handleMuteSet(value) {
this.log.debug(`Set mute to ${value}.`);
this.doorbellMute = value;
this.storage.setItemSync(this.doorbellMuteKey, this.doorbellMute);
}
handleMuteGet() {
this.log.debug('Get mute.');
return this.doorbellMute;
}
}
exports.DoorbellRPiPicoAccessory = DoorbellRPiPicoAccessory;
//# sourceMappingURL=accessory.js.map