From 0c8e502b2f40a64797aa653b2b42053228fa8f0c Mon Sep 17 00:00:00 2001 From: icanos Date: Sat, 21 Dec 2019 15:01:15 +0000 Subject: [PATCH 01/12] initial impl of transitions --- plejd/ble.js | 99 +++++++++++++++++++++++++++++++++++++++++++++------ plejd/main.js | 28 +++++---------- plejd/mqtt.js | 85 ++++++++++++++----------------------------- 3 files changed, 124 insertions(+), 88 deletions(-) diff --git a/plejd/ble.js b/plejd/ble.js index 1663822..e184046 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -4,7 +4,7 @@ const xor = require('buffer-xor'); const _ = require('lodash'); const EventEmitter = require('events'); -let debug = ''; +let debug = 'console'; const getLogger = () => { const consoleLogger = msg => console.log('plejd', msg); @@ -34,6 +34,11 @@ const STATE_DISCONNECTED = 'disconnected'; const STATE_UNINITIALIZED = 'uninitialized'; const STATE_INITIALIZED = 'initialized'; +const BLE_CMD_DIM_CHANGE = '00c8'; +const BLE_CMD_DIM2_CHANGE = '0098'; +const BLE_CMD_STATE_CHANGE = '0097'; +const BLE_CMD_SCENE_TRIG = '0021'; + class PlejdService extends EventEmitter { constructor(cryptoKey, keepAlive = false) { super(); @@ -51,6 +56,8 @@ class PlejdService extends EventEmitter { this.writeQueue = []; + this.plejdDevices = {}; + // Holds a reference to all characteristics this.characteristicState = STATE_UNINITIALIZED; this.characteristics = { @@ -64,13 +71,39 @@ class PlejdService extends EventEmitter { this.wireEvents(); } - turnOn(id, brightness) { - logger('turning on ' + id + ' at brightness ' + brightness); + turnOn(id, command) { + logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness)); + const brightness = command.brightness ? command.brightness : 0; + if (command.transition) { + // we have a transition time, split the target brightness + // into pieces spread of the transition time + const steps = command.transition * 2; + const brightnessStep = brightness / steps; + + let i = 0; + const transitionRef = setInterval(() => { + this._turnOn(id, (brightnessStep * i) + 1); + + if (i >= steps) { + clearInterval(transitionRef); + } + + i++; + }, 500); + } + else { + this._turnOn(id, brightness); + } + } + + _turnOn(id, brightness) { var payload; - if (!brightness) { + if (!brightness || brightness === 0) { + logger('no brightness specified, setting to previous known.'); payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009701', 'hex'); } else { + logger('brightness is ' + brightness); brightness = brightness << 8 | brightness; payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009801' + (brightness).toString(16).padStart(4, '0'), 'hex'); } @@ -78,9 +111,35 @@ class PlejdService extends EventEmitter { this.write(payload); } - turnOff(id) { + turnOff(id, command) { logger('turning off ' + id); + if (command.transition) { + // we have a transition time, split the target brightness (which will be 0) + // into pieces spread of the transition time + const initialBrightness = this.plejdDevices[id].dim; + const steps = command.transition * 2; + const brightnessStep = initialBrightness / steps; + let currentBrightness = initialBrightness; + + let i = 0; + const transitionRef = setInterval(() => { + currentBrightness = initialBrightness - (brightnessStep * i); + if (currentBrightness <= 0) { + clearInterval(transitionRef); + } + + this._turnOn(id, currentBrightness); + + if (i >= steps) { + clearInterval(transitionRef); + } + + i++; + }, 500); + } + + // finally, we turn it off var payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009700', 'hex'); this.write(payload); } @@ -143,7 +202,7 @@ class PlejdService extends EventEmitter { ) ); - logger('connecting to ' + this.device.id + ' with addr ' + this.device.address + ' and rssi ' + this.device.rssi); + console.log('connecting to ' + this.device.id + ' with addr ' + this.device.address + ' and rssi ' + this.device.rssi); setTimeout(() => { if (self.state !== STATE_CONNECTED && self.state !== STATE_AUTHENTICATED) { if (self.deviceIdx < Object.keys(self.devices).length) { @@ -435,19 +494,39 @@ class PlejdService extends EventEmitter { let dim = 0; let device = parseInt(decoded[0], 10); - if (decoded.toString('hex', 3, 5) === '00c8' || decoded.toString('hex', 3, 5) === '0098') { + if (decoded.length < 5) { + // ignore the notification since too small + return; + } + + const cmd = decoded.toString('hex', 3, 5); + + if (debug) { + logger('raw event received: ' + decoded.toString('hex')); + } + + if (cmd === BLE_CMD_DIM_CHANGE || cmd === BLE_CMD_DIM2_CHANGE) { state = parseInt(decoded.toString('hex', 5, 6), 10); dim = parseInt(decoded.toString('hex', 6, 8), 16) >> 8; logger('d: ' + device + ' got state+dim update: ' + state + ' - ' + dim); - this.emit('dimChanged', device, state, dim); + this.emit('stateChanged', device, { state: state, brightness: dim }); } - else if (decoded.toString('hex', 3, 5) === '0097') { + else if (cmd === BLE_CMD_STATE_CHANGE) { state = parseInt(decoded.toString('hex', 5, 6), 10); logger('d: ' + device + ' got state update: ' + state); - this.emit('stateChanged', device, state); + this.emit('stateChanged', device, { state: state }); } + else if (cmd === BLE_CMD_SCENE_TRIG) { + const scene = parseInt(decoded.toString('hex', 5, 6), 10); + this.emit('sceneTriggered', device, scene); + } + + this.plejdDevices[device] = { + state: state, + dim: dim + }; } wireEvents() { diff --git a/plejd/main.js b/plejd/main.js index 78057a5..bcc9b78 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -1,11 +1,11 @@ -const plejd = require('./plejd'); const api = require('./api'); const mqtt = require('./mqtt'); const fs = require('fs'); const PlejdService = require('./ble'); async function main() { - const rawData = fs.readFileSync('/data/plejd.json'); + //const rawData = fs.readFileSync('/data/plejd.json'); + const rawData = fs.readFileSync('plejd.json'); const config = JSON.parse(rawData); const plejdApi = new api.PlejdApi(config.site, config.username, config.password); @@ -29,29 +29,17 @@ async function main() { }); // subscribe to changes from Plejd - plejd.on('stateChanged', (deviceId, state) => { - client.updateState(deviceId, state); - }); - plejd.on('dimChanged', (deviceId, state, dim) => { - client.updateState(deviceId, state); - client.updateBrightness(deviceId, dim); + plejd.on('stateChanged', (deviceId, command) => { + client.updateState(deviceId, command); }); // subscribe to changes from HA - client.on('stateChanged', (deviceId, state) => { - if (state) { - plejd.turnOn(deviceId); + client.on('stateChanged', (deviceId, command) => { + if (command.state === 'ON') { + plejd.turnOn(deviceId, command); } else { - plejd.turnOff(deviceId); - } - }); - client.on('brightnessChanged', (deviceId, brightness) => { - if (brightness > 0) { - plejd.turnOn(deviceId, brightness); - } - else { - plejd.turnOff(deviceId); + plejd.turnOff(deviceId, command); } }); }); diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 30b2a3d..5898525 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -27,32 +27,17 @@ const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`; const getPath = ({ id, type }) => `${discoveryPrefix}/${type}/${nodeId}/${id}`; const getConfigPath = plug => `${getPath(plug)}/config`; -const getAvailabilityTopic = plug => `${getPath(plug)}/availability`; const getStateTopic = plug => `${getPath(plug)}/state`; -const getBrightnessCommandTopic = plug => `${getPath(plug)}/setBrightness`; -const getBrightnessTopic = plug => `${getPath(plug)}/brightness`; const getCommandTopic = plug => `${getPath(plug)}/set`; -const getDiscoveryDimmablePayload = device => ({ - name: device.name, - unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, - state_topic: getStateTopic(device), - command_topic: getCommandTopic(device), - brightness_command_topic: getBrightnessCommandTopic(device), - brightness_state_topic: getBrightnessTopic(device), - payload_on: 1, - payload_off: 0, - optimistic: false -}); - const getDiscoveryPayload = device => ({ + schema: 'json', name: device.name, unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`, state_topic: getStateTopic(device), command_topic: getCommandTopic(device), - payload_on: 1, - payload_off: 0, - optimistic: false + optimistic: false, + brightness: `${device.dimmable}` }); // #endregion @@ -99,24 +84,17 @@ class MqttClient extends EventEmitter { }); this.client.on('message', (topic, message) => { - const command = message.toString(); + //const command = message.toString(); + const command = JSON.parse(message.toString()); if (topic === startTopic) { logger('home assistant has started. lets do discovery.'); self.emit('connected'); } - if (_.includes(topic, 'setBrightness')) { - const device = self.devices.find(x => getBrightnessCommandTopic(x) === topic); - logger('got brightness update for ' + device.name + ' with brightness: ' + command); - - self.emit('brightnessChanged', device.id, parseInt(command)); - } - else if (_.includes(topic, 'set') && _.includes(['0', '1'], command)) { + if (_.includes(topic, 'set')) { const device = self.devices.find(x => getCommandTopic(x) === topic); - logger('got state update for ' + device.name + ' with state: ' + command); - - self.emit('stateChanged', device.id, parseInt(command)); + self.emit('stateChanged', device.id, command); } }); } @@ -134,16 +112,8 @@ class MqttClient extends EventEmitter { devices.forEach((device) => { logger(`sending discovery for ${device.name}`); - let payload = null; - - if (device.type === 'switch') { - payload = getDiscoveryPayload(device); - } - else { - payload = device.dimmable ? getDiscoveryDimmablePayload(device) : getDiscoveryPayload(device); - } - - console.log(`discovered ${device.name} with Plejd ID ${device.id}.`); + let payload = getDiscoveryPayload(device); + console.log(`discovered ${device.type}: ${device.name} with Plejd ID ${device.id}.`); self.deviceMap[device.id] = payload.unique_id; @@ -154,7 +124,7 @@ class MqttClient extends EventEmitter { }); } - updateState(deviceId, state) { + updateState(deviceId, data) { const device = this.devices.find(x => x.id === deviceId); if (!device) { @@ -162,27 +132,26 @@ class MqttClient extends EventEmitter { return; } - logger('updating state for ' + device.name + ': ' + state); + logger('updating state for ' + device.name + ': ' + data.state); + let payload = null; + + if (device.dimmable) { + payload = { + state: data.state === 1 ? 'ON' : 'OFF', + brightness: data.brightness + } + } + else { + payload = { + state: data.state === 1 ? 'ON' : 'OFF' + } + } + + logger(JSON.stringify(payload)); this.client.publish( getStateTopic(device), - state.toString() - ); - } - - updateBrightness(deviceId, brightness) { - const device = this.devices.find(x => x.id === deviceId); - - if (!device) { - logger('error: ' + deviceId + ' is not handled by us.'); - return; - } - - logger('updating brightness for ' + device.name + ': ' + brightness); - - this.client.publish( - getBrightnessTopic(device), - brightness.toString() + JSON.stringify(payload) ); } } From da91a839ec7926709ea6d77027132fbdfa518a76 Mon Sep 17 00:00:00 2001 From: icanos Date: Sat, 21 Dec 2019 15:01:29 +0000 Subject: [PATCH 02/12] restored plejd.json path --- plejd/main.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plejd/main.js b/plejd/main.js index bcc9b78..3130fc7 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -4,8 +4,7 @@ const fs = require('fs'); const PlejdService = require('./ble'); async function main() { - //const rawData = fs.readFileSync('/data/plejd.json'); - const rawData = fs.readFileSync('plejd.json'); + const rawData = fs.readFileSync('/data/plejd.json'); const config = JSON.parse(rawData); const plejdApi = new api.PlejdApi(config.site, config.username, config.password); From ba95426708778c69cb66d77c3cf8aa6bda516238 Mon Sep 17 00:00:00 2001 From: icanos Date: Sat, 21 Dec 2019 15:08:03 +0000 Subject: [PATCH 03/12] disabled debug log to save on sd card --- plejd/ble.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plejd/ble.js b/plejd/ble.js index e184046..f542aca 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -4,7 +4,7 @@ const xor = require('buffer-xor'); const _ = require('lodash'); const EventEmitter = require('events'); -let debug = 'console'; +let debug = ''; const getLogger = () => { const consoleLogger = msg => console.log('plejd', msg); From ec624c6773793b3325ff32688350cd8226d94779 Mon Sep 17 00:00:00 2001 From: icanos Date: Sat, 21 Dec 2019 15:52:35 +0000 Subject: [PATCH 04/12] moved turn off code for transition --- plejd/ble.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plejd/ble.js b/plejd/ble.js index f542aca..aa5919e 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -133,13 +133,20 @@ class PlejdService extends EventEmitter { if (i >= steps) { clearInterval(transitionRef); + + // finally, we turn it off + this._turnOff(id); } i++; }, 500); } + else { + this._turnOff(id); + } + } - // finally, we turn it off + _turnOff(id) { var payload = Buffer.from((id).toString(16).padStart(2, '0') + '0110009700', 'hex'); this.write(payload); } From 3aa69d0930a319b81fbaa6d0d5559c1f2dc82d22 Mon Sep 17 00:00:00 2001 From: icanos Date: Sun, 22 Dec 2019 17:48:16 +0000 Subject: [PATCH 05/12] added scene event to mqtt --- plejd/main.js | 4 ++++ plejd/mqtt.js | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/plejd/main.js b/plejd/main.js index 3130fc7..855037e 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -32,6 +32,10 @@ async function main() { client.updateState(deviceId, command); }); + plejd.on('sceneTriggered', (scene) => { + client.sceneTriggered(scene); + }); + // subscribe to changes from HA client.on('stateChanged', (deviceId, command) => { if (command.state === 'ON') { diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 5898525..40e6440 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -29,6 +29,7 @@ const getPath = ({ id, type }) => const getConfigPath = plug => `${getPath(plug)}/config`; const getStateTopic = plug => `${getPath(plug)}/state`; const getCommandTopic = plug => `${getPath(plug)}/set`; +const getSceneEventTopic = () => `plejd/event/scene`; const getDiscoveryPayload = device => ({ schema: 'json', @@ -147,13 +148,18 @@ class MqttClient extends EventEmitter { } } - logger(JSON.stringify(payload)); - this.client.publish( getStateTopic(device), JSON.stringify(payload) ); } + + sceneTriggered(scene) { + this.client.publish( + getSceneEventTopic(), + JSON.stringify({ scene: scene }) + ); + } } module.exports = { MqttClient }; \ No newline at end of file From a9567517efdc3d2aceff6964e46143cbbf93cd40 Mon Sep 17 00:00:00 2001 From: icanos Date: Sun, 22 Dec 2019 18:05:05 +0000 Subject: [PATCH 06/12] fixed turn off with transition bug and upped ver --- plejd/ble.js | 2 +- plejd/config.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plejd/ble.js b/plejd/ble.js index aa5919e..05717ff 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -117,7 +117,7 @@ class PlejdService extends EventEmitter { if (command.transition) { // we have a transition time, split the target brightness (which will be 0) // into pieces spread of the transition time - const initialBrightness = this.plejdDevices[id].dim; + const initialBrightness = this.plejdDevices[id] ? this.plejdDevices[id].dim : 250; const steps = command.transition * 2; const brightnessStep = initialBrightness / steps; let currentBrightness = initialBrightness; diff --git a/plejd/config.json b/plejd/config.json index c7043c2..a2140bf 100644 --- a/plejd/config.json +++ b/plejd/config.json @@ -1,6 +1,6 @@ { "name": "Plejd", - "version": "0.1.0", + "version": "0.2.0", "slug": "plejd", "description": "Adds support for the Swedish home automation devices from Plejd.", "url": "https://github.com/icanos/hassio-plejd/", From c8dae032a9ee792f87d1c7bdcbeade0e785b5fa0 Mon Sep 17 00:00:00 2001 From: icanos Date: Sun, 22 Dec 2019 18:20:20 +0000 Subject: [PATCH 07/12] added support to toggle logging through mqtt --- plejd/api.js | 11 ++++++++++- plejd/ble.js | 24 +++++++++++++++--------- plejd/main.js | 12 ++++++++++++ plejd/mqtt.js | 21 ++++++++++++++++++++- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/plejd/api.js b/plejd/api.js index a2a7b31..49823a5 100644 --- a/plejd/api.js +++ b/plejd/api.js @@ -8,7 +8,7 @@ API_LOGIN_URL = 'login'; API_SITES_URL = 'functions/getSites'; // #region logging -const debug = ''; +let debug = ''; const getLogger = () => { const consoleLogger = msg => console.log('plejd-api', msg); @@ -34,6 +34,15 @@ class PlejdApi extends EventEmitter { this.site = null; } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + login() { logger('login()'); const self = this; diff --git a/plejd/ble.js b/plejd/ble.js index 05717ff..5a258d6 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -71,6 +71,15 @@ class PlejdService extends EventEmitter { this.wireEvents(); } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + turnOn(id, command) { logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness)); const brightness = command.brightness ? command.brightness : 0; @@ -83,7 +92,7 @@ class PlejdService extends EventEmitter { let i = 0; const transitionRef = setInterval(() => { - this._turnOn(id, (brightnessStep * i) + 1); + this._turnOn(id, parseInt((brightnessStep * i) + 1)); if (i >= steps) { clearInterval(transitionRef); @@ -124,19 +133,16 @@ class PlejdService extends EventEmitter { let i = 0; const transitionRef = setInterval(() => { - currentBrightness = initialBrightness - (brightnessStep * i); - if (currentBrightness <= 0) { - clearInterval(transitionRef); - } - - this._turnOn(id, currentBrightness); - - if (i >= steps) { + currentBrightness = parseInt(initialBrightness - (brightnessStep * i)); + if (currentBrightness <= 0 || i >= steps) { clearInterval(transitionRef); // finally, we turn it off this._turnOff(id); + return; } + + this._turnOn(id, currentBrightness); i++; }, 500); diff --git a/plejd/main.js b/plejd/main.js index 855037e..7b9856a 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -45,6 +45,18 @@ async function main() { plejd.turnOff(deviceId, command); } }); + + client.on('settingsChanged', (settings) => { + if (settings.module === 'mqtt') { + client.updateSettings(settings); + } + else if (settings.module === 'ble') { + plejd.updateSettings(settings); + } + else if (settings.module === 'api') { + plejdApi.updateSettings(settings); + } + }); }); }); diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 40e6440..39afbb9 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -5,7 +5,7 @@ const _ = require('lodash'); const startTopic = 'hass/status'; // #region logging -const debug = ''; +let debug = ''; const getLogger = () => { const consoleLogger = msg => console.log('plejd-mqtt', msg); @@ -30,6 +30,7 @@ const getConfigPath = plug => `${getPath(plug)}/config`; const getStateTopic = plug => `${getPath(plug)}/state`; const getCommandTopic = plug => `${getPath(plug)}/set`; const getSceneEventTopic = () => `plejd/event/scene`; +const getSettingsTopic = () => `plejd/settings`; const getDiscoveryPayload = device => ({ schema: 'json', @@ -78,6 +79,12 @@ class MqttClient extends EventEmitter { logger('error: unable to subscribe to control topics'); } }); + + this.client.subscribe(getSettingsTopic(), (err) => { + if (err) { + console.log('error: could not subscribe to settings topic'); + } + }); }); this.client.on('close', () => { @@ -92,6 +99,9 @@ class MqttClient extends EventEmitter { logger('home assistant has started. lets do discovery.'); self.emit('connected'); } + else if (topic === getSettingsTopic()) { + self.emit('settingsChanged', command); + } if (_.includes(topic, 'set')) { const device = self.devices.find(x => getCommandTopic(x) === topic); @@ -100,6 +110,15 @@ class MqttClient extends EventEmitter { }); } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + reconnect() { this.client.reconnect(); } From fd1d146254746a09ce78393824d3aebface22fab Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Sun, 22 Dec 2019 19:25:59 +0100 Subject: [PATCH 08/12] Update README.md --- README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2fbbb96..b278fc1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ The add-on has been tested on the following platforms: Browse to your Home Assistant installation in a web browser and click on `Hass.io` in the navigation bar to the left. * Open the Home Assistant web console and click `Hass.io` in the menu on the left side. * Click on `Add-on Store` in the top navigation bar of that page. -* Paste the URL to this repo https://github.com/icanos/hassio-plejd/ in the `Add new repository by URL` field and hit `Add`. +* Paste the URL to this repo https://github.com/icanos/hassio-plejd.git in the `Add new repository by URL` field and hit `Add`. * Scroll down and you should find a Plejd add-on that can be installed. Open that and install. * Enjoy! @@ -52,10 +52,9 @@ Browse your Hass.io installation using a tool that allows you to manage files, f You need to add the following to your `configuration.yaml` file: ``` mqtt: - broker: [point to your broker IP] + broker: [point to your broker IP eg. 'mqtt://localhost'] username: [username of mqtt broker] password: !secret mqtt_password - client_id: mqtt discovery: true discovery_prefix: homeassistant birth_message: @@ -88,10 +87,15 @@ Check this out for more information on how you can get your Plejd lights control https://www.home-assistant.io/integrations/homekit/ ## Changelog -*0.1.4*: -* FIX: bug preventing add-on from building +*v 0.1.1*: +* FIX: Fixed missing reference on startup, preventing add-on from starting -*0.1.3*: +*v 0.1.0*: +* NEW: Rewrote the BLE integration for more stability +* FIX: discovery wasn't always sent + +*previous*: +* FIX: bug preventing add-on from building * NEW: Added support for Plejd devices with multiple outputs (such as DIM-02) ## License From 717c17b77e5cd9660547ed9da85ff6677cea2a45 Mon Sep 17 00:00:00 2001 From: icanos Date: Mon, 23 Dec 2019 11:43:30 +0000 Subject: [PATCH 09/12] bug fix brightness > 254 toggles back --- plejd/ble.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plejd/ble.js b/plejd/ble.js index 5a258d6..4639cc4 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -92,7 +92,12 @@ class PlejdService extends EventEmitter { let i = 0; const transitionRef = setInterval(() => { - this._turnOn(id, parseInt((brightnessStep * i) + 1)); + let currentBrightness = parseInt((brightnessStep * i) + 1); + if (currentBrightness > 254) { + currentBrightness = 254; + } + + this._turnOn(id, currentBrightness); if (i >= steps) { clearInterval(transitionRef); From 7a3e11f5f79b0a60aa6880c79d892a59341b5a59 Mon Sep 17 00:00:00 2001 From: icanos Date: Mon, 23 Dec 2019 11:51:04 +0000 Subject: [PATCH 10/12] added a log message to display version --- plejd/main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plejd/main.js b/plejd/main.js index 7b9856a..3c81a13 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -3,7 +3,11 @@ const mqtt = require('./mqtt'); const fs = require('fs'); const PlejdService = require('./ble'); +const version = "0.2.0"; + async function main() { + console.log('starting Plejd add-on v. ' + version); + const rawData = fs.readFileSync('/data/plejd.json'); const config = JSON.parse(rawData); From 83dc9924fd890b1b67e40131097b327af045b3db Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 31 Dec 2019 13:46:10 +0100 Subject: [PATCH 11/12] clean up listeners --- plejd/ble.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plejd/ble.js b/plejd/ble.js index 5a258d6..929b570 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -219,7 +219,11 @@ class PlejdService extends EventEmitter { setTimeout(() => { if (self.state !== STATE_CONNECTED && self.state !== STATE_AUTHENTICATED) { if (self.deviceIdx < Object.keys(self.devices).length) { - logger('connection timed out after 10 s. trying next.'); + logger('connection timed out after 10 s. cleaning up and trying next.'); + + self.device.removeAllListeners('servicesDiscover'); + self.device.removeAllListeners('connect'); + self.device.removeAllListeners('disconnect'); self.deviceIdx++; self.connect(); @@ -240,12 +244,16 @@ class PlejdService extends EventEmitter { disconnect() { logger('disconnect()'); - if (this.state !== STATE_CONNECTED) { + if (this.state !== STATE_CONNECTED && this.state !== STATE_AUTHENTICATED) { return; } clearInterval(this.pingRef); + this.device.removeAllListeners('servicesDiscover'); + this.device.removeAllListeners('connect'); + this.device.removeAllListeners('disconnect'); + this.unsubscribeCharacteristics(); this.device.disconnect(); From 5ce48d182064167f4f075f83a64aaeffd2554e1b Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 31 Dec 2019 13:48:22 +0100 Subject: [PATCH 12/12] a bit more logging in api --- plejd/api.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plejd/api.js b/plejd/api.js index 49823a5..9e02c76 100644 --- a/plejd/api.js +++ b/plejd/api.js @@ -64,7 +64,7 @@ class PlejdApi extends EventEmitter { 'password': this.password }) .then((response) => { - logger('got session token response'); + console.log('plejd-api: got session token response'); self.sessionToken = response.data.sessionToken; self.emit('loggedIn'); }) @@ -95,14 +95,14 @@ class PlejdApi extends EventEmitter { instance.post(API_SITES_URL) .then((response) => { - logger('got sites response'); + console.log('plejd-api: got sites response'); self.site = response.data.result.find(x => x.site.title == self.siteName); self.cryptoKey = self.site.plejdMesh.cryptoKey; callback(self.cryptoKey); }) .catch((error) => { - console.log('error: unable to retrieve the crypto key. error: ' + error); + console.log('error: unable to retrieve the crypto key. error: ' + error + ' (code: ' + error.response.status + ')'); return Promise.reject('unable to retrieve the crypto key. error: ' + error); }); }