added some config and logging

This commit is contained in:
icanos 2020-01-27 20:43:52 +00:00
parent 8413bc65ee
commit c87eee2d2c
6 changed files with 46 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:** **Did you like this? Consider helping me continue the development:**
[Buy me a coffee](https://www.buymeacoffee.com/w1ANTUb) [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 ## Getting started
To get started, make sure that the following requirements are met: 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 mqttUsername | Username of the MQTT broker
mqttPassword | Password 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*. 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! ## 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: 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/ https://www.home-assistant.io/integrations/homekit/
## Changelog ## 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*: *v 0.2.8*:
* FIX: Reset characteristic state on disconnect * 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'; const GATT_CHRC_ID = 'org.bluez.GattCharacteristic1';
class PlejdService extends EventEmitter { class PlejdService extends EventEmitter {
constructor(cryptoKey, keepAlive = false) { constructor(cryptoKey, connectionTimeout, keepAlive = false) {
super(); super();
this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex'); this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex');
@ -49,6 +49,7 @@ class PlejdService extends EventEmitter {
this.bleDevices = []; this.bleDevices = [];
this.plejdDevices = {}; this.plejdDevices = {};
this.connectEventHooked = false; this.connectEventHooked = false;
this.connectionTimeout = connectionTimeout;
// Holds a reference to all characteristics // Holds a reference to all characteristics
this.characteristics = { this.characteristics = {
@ -79,7 +80,9 @@ class PlejdService extends EventEmitter {
auth: null, auth: null,
ping: null ping: null
}; };
clearInterval(this.pingRef); clearInterval(this.pingRef);
console.log('init()');
const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/'); const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/');
this.objectManager = await bluez.getInterface(DBUS_OM_INTERFACE); this.objectManager = await bluez.getInterface(DBUS_OM_INTERFACE);
@ -122,11 +125,17 @@ class PlejdService extends EventEmitter {
'Transport': new dbus.Variant('s', 'le') 'Transport': new dbus.Variant('s', 'le')
}); });
await this.adapter.StartDiscovery(); 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 () => { setTimeout(async () => {
await this._internalInit(); await this._internalInit();
}, 2000); }, this.connectionTimeout * 1000);
} }
async _internalInit() { async _internalInit() {
@ -155,20 +164,22 @@ class PlejdService extends EventEmitter {
for (const plejd of sortedDevices) { for (const plejd of sortedDevices) {
try { try {
console.log('plejd-ble: connecting to ' + plejd['path']); if (plejd['instance']) {
await plejd['instance'].Connect(); console.log('plejd-ble: connecting to ' + plejd['path']);
connectedDevice = plejd; await plejd['instance'].Connect();
break connectedDevice = plejd;
break
}
} }
catch (err) { 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 () => { setTimeout(async () => {
await this.onDeviceConnected(connectedDevice); await this.onDeviceConnected(connectedDevice);
await this.adapter.StopDiscovery(); await this.adapter.StopDiscovery();
}, 2000); }, this.connectionTimeout * 1000);
} }
async _getInterface(managedObjects, iface) { async _getInterface(managedObjects, iface) {
@ -238,7 +249,7 @@ class PlejdService extends EventEmitter {
} }
i++; i++;
}, 500); }, 400);
} }
else { else {
this._turnOn(id, brightness); this._turnOn(id, brightness);
@ -341,7 +352,7 @@ class PlejdService extends EventEmitter {
logger('reconnected and retrying to write'); logger('reconnected and retrying to write');
await this.write(data, false); await this.write(data, false);
} }
}, 2000); }, this.connectionTimeout * 1000);
} }
} }
@ -480,7 +491,7 @@ class PlejdService extends EventEmitter {
} }
if (!this.plejdService) { 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'); this.emit('connectFailed');
return; return;
} }

View file

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

View file

@ -3,7 +3,7 @@ const mqtt = require('./mqtt');
const fs = require('fs'); const fs = require('fs');
const PlejdService = require('./ble.bluez'); const PlejdService = require('./ble.bluez');
const version = "0.3.3"; const version = "0.3.4";
async function main() { async function main() {
console.log('starting Plejd add-on v. ' + version); console.log('starting Plejd add-on v. ' + version);
@ -26,7 +26,7 @@ async function main() {
client.init(); client.init();
// init the BLE interface // init the BLE interface
const plejd = new PlejdService(cryptoKey, true); const plejd = new PlejdService(cryptoKey, config.connectionTimeout, true);
plejd.on('connectFailed', () => { plejd.on('connectFailed', () => {
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.'); console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
setTimeout(() => { setTimeout(() => {

View file

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

View file

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