hassio-plejd/plejd/main.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-12-04 11:17:06 +01:00
const api = require('./api');
const mqtt = require('./mqtt');
const fs = require('fs');
2019-12-19 09:58:53 +00:00
const PlejdService = require('./ble');
2019-12-04 11:17:06 +01:00
async function main() {
2019-12-21 15:01:15 +00:00
//const rawData = fs.readFileSync('/data/plejd.json');
const rawData = fs.readFileSync('plejd.json');
2019-12-04 11:17:06 +01:00
const config = JSON.parse(rawData);
const plejdApi = new api.PlejdApi(config.site, config.username, config.password);
const client = new mqtt.MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
plejdApi.once('loggedIn', () => {
plejdApi.getCryptoKey((cryptoKey) => {
const devices = plejdApi.getDevices();
2019-12-04 18:52:50 +01:00
client.on('connected', () => {
2019-12-04 11:17:06 +01:00
console.log('plejd-mqtt: connected to mqtt.');
client.discover(devices);
});
client.init();
// init the BLE interface
2019-12-19 09:58:53 +00:00
const plejd = new PlejdService(cryptoKey, true);
plejd.on('authenticated', () => {
2019-12-04 11:17:06 +01:00
console.log('plejd: connected via bluetooth.');
});
// subscribe to changes from Plejd
2019-12-21 15:01:15 +00:00
plejd.on('stateChanged', (deviceId, command) => {
client.updateState(deviceId, command);
2019-12-04 11:17:06 +01:00
});
// subscribe to changes from HA
2019-12-21 15:01:15 +00:00
client.on('stateChanged', (deviceId, command) => {
if (command.state === 'ON') {
plejd.turnOn(deviceId, command);
2019-12-04 11:17:06 +01:00
}
else {
2019-12-21 15:01:15 +00:00
plejd.turnOff(deviceId, command);
2019-12-04 11:17:06 +01:00
}
});
});
});
plejdApi.login();
}
main();