diff --git a/plejd/README.md b/plejd/README.md index acc3233..4f1f5b3 100644 --- a/plejd/README.md +++ b/plejd/README.md @@ -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 diff --git a/plejd/ble.bluez.js b/plejd/ble.bluez.js index 7fec31e..b7b0641 100644 --- a/plejd/ble.bluez.js +++ b/plejd/ble.bluez.js @@ -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') }); - 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 () => { await this._internalInit(); - }, 2000); + }, this.connectionTimeout * 1000); } async _internalInit() { @@ -155,20 +164,22 @@ class PlejdService extends EventEmitter { for (const plejd of sortedDevices) { try { - console.log('plejd-ble: connecting to ' + plejd['path']); - await plejd['instance'].Connect(); - connectedDevice = plejd; - break + 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; } diff --git a/plejd/config.json b/plejd/config.json index 3f03add..d002274 100644 --- a/plejd/config.json +++ b/plejd/config.json @@ -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" } } \ No newline at end of file diff --git a/plejd/main.js b/plejd/main.js index 469d310..a164fbe 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -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); @@ -26,7 +26,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(() => { diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 2f9dc20..adda828 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -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, diff --git a/plejd/rootfs/usr/bin/plejd.sh b/plejd/rootfs/usr/bin/plejd.sh index 9425931..236e27f 100644 --- a/plejd/rootfs/usr/bin/plejd.sh +++ b/plejd/rootfs/usr/bin/plejd.sh @@ -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\" } "