Merge pull request #55 from icanos/bluez

new config parameter
This commit is contained in:
Marcus Westin 2020-01-27 21:47:11 +01:00 committed by GitHub
commit fc15227292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 19 deletions

View file

@ -12,6 +12,8 @@ I am in no way affiliated with Plejd and am solely doing this as a hobby project
**Did you like this? Consider helping me continue the development:**
[Buy me a coffee](https://www.buymeacoffee.com/w1ANTUb)
[![Gitter](https://badges.gitter.im/hassio-plejd/community.svg)](https://gitter.im/hassio-plejd/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
## Getting started
To get started, make sure that the following requirements are met:
@ -88,6 +90,7 @@ mqttBroker | URL of the MQTT Broker, eg. mqtt://localhost
mqttUsername | Username of the MQTT broker
mqttPassword | Password of the MQTT broker
includeRoomsAsLights | Adds all rooms as lights, making it possible to turn on/off lights by room instead. Setting this to false will ignore all rooms. *Added in v. 5*.
connectionTimeout | Number of seconds to wait when scanning and connecting. Might need to be tweaked on platforms other than RPi 4. Defaults to: 2 seconds.
## I want voice control!
With the Google Home integration in Home Assistant, you can get voice control for your Plejd lights right away, check this out for more information:
@ -98,6 +101,15 @@ Check this out for more information on how you can get your Plejd lights control
https://www.home-assistant.io/integrations/homekit/
## Changelog
*v 0.3.4*:
* NEW: `connectionTimeout` configuration parameter to enable tweaking of wait time on connection, usable for RPi 3B+.
* FIX: Reworked some logging to get better understanding of what happens.
*v 0.3.0*:
* NEW: New BLE manager, DBus instead of noble
* FIX: Adding entities as devices now as well
* FIX: Bug fixes
*v 0.2.8*:
* FIX: Reset characteristic state on disconnect

View file

@ -40,7 +40,7 @@ const GATT_SERVICE_ID = 'org.bluez.GattService1';
const GATT_CHRC_ID = 'org.bluez.GattCharacteristic1';
class PlejdService extends EventEmitter {
constructor(cryptoKey, keepAlive = false) {
constructor(cryptoKey, connectionTimeout, keepAlive = false) {
super();
this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex');
@ -49,6 +49,7 @@ class PlejdService extends EventEmitter {
this.bleDevices = [];
this.plejdDevices = {};
this.connectEventHooked = false;
this.connectionTimeout = connectionTimeout;
// Holds a reference to all characteristics
this.characteristics = {
@ -79,7 +80,9 @@ class PlejdService extends EventEmitter {
auth: null,
ping: null
};
clearInterval(this.pingRef);
console.log('init()');
const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/');
this.objectManager = await bluez.getInterface(DBUS_OM_INTERFACE);
@ -122,11 +125,17 @@ class PlejdService extends EventEmitter {
'Transport': new dbus.Variant('s', 'le')
});
try {
await this.adapter.StartDiscovery();
}
catch (err) {
console.log('plejd-ble: error: failed to start discovery. Make sure no other add-on is currently scanning.');
return;
}
setTimeout(async () => {
await this._internalInit();
}, 2000);
}, this.connectionTimeout * 1000);
}
async _internalInit() {
@ -155,20 +164,22 @@ class PlejdService extends EventEmitter {
for (const plejd of sortedDevices) {
try {
if (plejd['instance']) {
console.log('plejd-ble: connecting to ' + plejd['path']);
await plejd['instance'].Connect();
connectedDevice = plejd;
break
}
}
catch (err) {
console.log('plejd-ble: failed connecting to plejd, error: ' + err);
console.log('plejd-ble: warning: unable to connect, will retry. ' + err);
}
}
setTimeout(async () => {
await this.onDeviceConnected(connectedDevice);
await this.adapter.StopDiscovery();
}, 2000);
}, this.connectionTimeout * 1000);
}
async _getInterface(managedObjects, iface) {
@ -238,7 +249,7 @@ class PlejdService extends EventEmitter {
}
i++;
}, 500);
}, 400);
}
else {
this._turnOn(id, brightness);
@ -341,7 +352,7 @@ class PlejdService extends EventEmitter {
logger('reconnected and retrying to write');
await this.write(data, false);
}
}, 2000);
}, this.connectionTimeout * 1000);
}
}
@ -480,7 +491,7 @@ class PlejdService extends EventEmitter {
}
if (!this.plejdService) {
console.log('plejd-ble: error: unable to connect to the Plejd mesh.');
console.log('plejd-ble: warning: wasn\'t able to connect to Plejd, will retry.');
this.emit('connectFailed');
return;
}

View file

@ -1,6 +1,6 @@
{
"name": "Plejd",
"version": "0.3.3",
"version": "0.3.4",
"slug": "plejd",
"description": "Adds support for the Swedish home automation devices from Plejd.",
"url": "https://github.com/icanos/hassio-plejd/",
@ -22,7 +22,8 @@
"mqttBroker": "mqtt://",
"mqttUsername": "",
"mqttPassword": "",
"includeRoomsAsLights": false
"includeRoomsAsLights": false,
"connectionTimeout": 2
},
"schema": {
"site": "str",
@ -31,6 +32,7 @@
"mqttBroker": "str",
"mqttUsername": "str",
"mqttPassword": "str",
"includeRoomsAsLights": "bool"
"includeRoomsAsLights": "bool",
"connectionTimeout": "int"
}
}

View file

@ -3,7 +3,7 @@ const mqtt = require('./mqtt');
const fs = require('fs');
const PlejdService = require('./ble.bluez');
const version = "0.3.3";
const version = "0.3.4";
async function main() {
console.log('starting Plejd add-on v. ' + version);
@ -11,6 +11,10 @@ async function main() {
const rawData = fs.readFileSync('/data/plejd.json');
const config = JSON.parse(rawData);
if (!config.connectionTimeout) {
config.connectionTimeout = 2;
}
const plejdApi = new api.PlejdApi(config.site, config.username, config.password);
const client = new mqtt.MqttClient(config.mqttBroker, config.mqttUsername, config.mqttPassword);
@ -26,7 +30,7 @@ async function main() {
client.init();
// init the BLE interface
const plejd = new PlejdService(cryptoKey, true);
const plejd = new PlejdService(cryptoKey, config.connectionTimeout, true);
plejd.on('connectFailed', () => {
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
setTimeout(() => {

View file

@ -35,7 +35,7 @@ const getSettingsTopic = () => `plejd/settings`;
const getDiscoveryPayload = device => ({
schema: 'json',
name: device.name,
unique_id: device.serialNumber + '_' + device.id,
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
state_topic: getStateTopic(device),
command_topic: getCommandTopic(device),
optimistic: false,

View file

@ -9,6 +9,7 @@ MQTTBROKER=$(jq --raw-output ".mqttBroker" $CONFIG_PATH)
MQTTUSERNAME=$(jq --raw-output ".mqttUsername" $CONFIG_PATH)
MQTTPASSWORD=$(jq --raw-output ".mqttPassword" $CONFIG_PATH)
INCLUDEROOMSASLIGHTS=$(jq --raw-output ".includeRoomsAsLights" $CONFIG_PATH)
CONNECTIONTIMEOUT=$(jq --raw-output ".connectionTimeout" $CONFIG_PATH)
PLEJD_PATH=/data/plejd.json
PLEJD_CONFIG="{
@ -18,7 +19,8 @@ PLEJD_CONFIG="{
\"mqttBroker\": \"$MQTTBROKER\",
\"mqttUsername\": \"$MQTTUSERNAME\",
\"mqttPassword\": \"$MQTTPASSWORD\",
\"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\"
\"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\",
\"connectionTimeout\": \"$CONNECTIONTIMEOUT\"
}
"