2019-12-04 11:17:06 +01:00
|
|
|
const api = require('./api');
|
|
|
|
|
const mqtt = require('./mqtt');
|
|
|
|
|
const fs = require('fs');
|
2020-01-17 15:00:54 +00:00
|
|
|
const PlejdService = require('./ble.bluez');
|
2020-02-29 15:54:08 +00:00
|
|
|
const SceneManager = require('./scene.manager');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2020-06-12 11:15:24 +02:00
|
|
|
const version = "0.4.7";
|
2019-12-23 11:51:04 +00:00
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
async function main() {
|
2019-12-23 11:51:04 +00:00
|
|
|
console.log('starting Plejd add-on v. ' + version);
|
|
|
|
|
|
2020-01-20 19:21:54 +00:00
|
|
|
const rawData = fs.readFileSync('/data/plejd.json');
|
2019-12-04 11:17:06 +01:00
|
|
|
const config = JSON.parse(rawData);
|
|
|
|
|
|
2020-01-27 20:45:13 +00:00
|
|
|
if (!config.connectionTimeout) {
|
|
|
|
|
config.connectionTimeout = 2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
const plejdApi = new api.PlejdApi(config.site, config.username, config.password);
|
|
|
|
|
const client = new mqtt.MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
|
|
|
|
|
|
2020-03-13 11:58:15 +01:00
|
|
|
plejdApi.login().then(() => {
|
|
|
|
|
// load all sites and find the one that we want (from config)
|
|
|
|
|
plejdApi.getSites().then((site) => {
|
|
|
|
|
// load the site and retrieve the crypto key
|
|
|
|
|
plejdApi.getSite(site.site.siteId).then((cryptoKey) => {
|
|
|
|
|
// parse all devices from the API
|
|
|
|
|
const devices = plejdApi.getDevices();
|
|
|
|
|
|
|
|
|
|
client.on('connected', () => {
|
|
|
|
|
console.log('plejd-mqtt: connected to mqtt.');
|
|
|
|
|
client.discover(devices);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.init();
|
|
|
|
|
|
|
|
|
|
// init the BLE interface
|
2020-03-13 12:02:56 +01:00
|
|
|
const sceneManager = new SceneManager(plejdApi.site, devices);
|
2020-03-13 11:58:15 +01:00
|
|
|
const plejd = new PlejdService(cryptoKey, devices, sceneManager, config.connectionTimeout, config.writeQueueWaitTime, true);
|
|
|
|
|
plejd.on('connectFailed', () => {
|
|
|
|
|
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
plejd.init();
|
|
|
|
|
}, 10000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
plejd.init();
|
|
|
|
|
|
|
|
|
|
plejd.on('authenticated', () => {
|
|
|
|
|
console.log('plejd: connected via bluetooth.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// subscribe to changes from Plejd
|
|
|
|
|
plejd.on('stateChanged', (deviceId, command) => {
|
|
|
|
|
client.updateState(deviceId, command);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
plejd.on('sceneTriggered', (deviceId, scene) => {
|
|
|
|
|
client.sceneTriggered(scene);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// subscribe to changes from HA
|
|
|
|
|
client.on('stateChanged', (device, command) => {
|
|
|
|
|
const deviceId = device.id;
|
|
|
|
|
|
|
|
|
|
if (device.typeName === 'Scene') {
|
|
|
|
|
// we're triggering a scene, lets do that and jump out.
|
|
|
|
|
// since scenes aren't "real" devices.
|
|
|
|
|
plejd.triggerScene(device.id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let state = 'OFF';
|
|
|
|
|
let commandObj = {};
|
|
|
|
|
|
|
|
|
|
if (typeof command === 'string') {
|
|
|
|
|
// switch command
|
|
|
|
|
state = command;
|
2020-06-12 11:15:24 +02:00
|
|
|
commandObj = {
|
|
|
|
|
state: state
|
|
|
|
|
};
|
2020-03-13 11:58:15 +01:00
|
|
|
|
|
|
|
|
// since the switch doesn't get any updates on whether it's on or not,
|
|
|
|
|
// we fake this by directly send the updateState back to HA in order for
|
|
|
|
|
// it to change state.
|
2020-06-12 11:15:24 +02:00
|
|
|
client.updateState(deviceId, {
|
|
|
|
|
state: state === 'ON' ? 1 : 0
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2020-03-13 11:58:15 +01:00
|
|
|
state = command.state;
|
|
|
|
|
commandObj = command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state === 'ON') {
|
|
|
|
|
plejd.turnOn(deviceId, commandObj);
|
2020-06-12 11:15:24 +02:00
|
|
|
} else {
|
2020-03-13 11:58:15 +01:00
|
|
|
plejd.turnOff(deviceId, commandObj);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.on('settingsChanged', (settings) => {
|
|
|
|
|
if (settings.module === 'mqtt') {
|
|
|
|
|
client.updateSettings(settings);
|
2020-06-12 11:15:24 +02:00
|
|
|
} else if (settings.module === 'ble') {
|
2020-03-13 11:58:15 +01:00
|
|
|
plejd.updateSettings(settings);
|
2020-06-12 11:15:24 +02:00
|
|
|
} else if (settings.module === 'api') {
|
2020-03-13 11:58:15 +01:00
|
|
|
plejdApi.updateSettings(settings);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-01-15 09:33:54 +00:00
|
|
|
});
|
2019-12-04 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 11:15:24 +02:00
|
|
|
main();
|