diff --git a/plejd/ble.bluez.js b/plejd/ble.bluez.js index 2c7f365..abf1afc 100644 --- a/plejd/ble.bluez.js +++ b/plejd/ble.bluez.js @@ -135,8 +135,7 @@ class PlejdService extends EventEmitter { try { await this.adapter.StartDiscovery(); - } - catch (err) { + } catch (err) { console.log('plejd-ble: error: failed to start discovery. Make sure no other add-on is currently scanning.'); return; } @@ -166,8 +165,7 @@ class PlejdService extends EventEmitter { plejd['device'] = this.devices.find(x => x.serialNumber === fixedPlejdPath); logger('discovered ' + plejd['path'] + ' with rssi ' + plejd['rssi']); - } - catch (err) { + } catch (err) { console.log('plejd-ble: failed inspecting ' + plejd['path'] + ' error: ' + err); } } @@ -183,8 +181,7 @@ class PlejdService extends EventEmitter { connectedDevice = plejd; break } - } - catch (err) { + } catch (err) { console.log('plejd-ble: warning: unable to connect, will retry. ' + err); } } @@ -205,8 +202,7 @@ class PlejdService extends EventEmitter { try { const adapterObject = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, path); return [path, adapterObject.getInterface(iface), adapterObject]; - } - catch (err) { + } catch (err) { console.log('plejd-ble: error: failed to get interface \'' + iface + '\': ' + err); } } @@ -222,7 +218,9 @@ class PlejdService extends EventEmitter { if (interfaceKeys.indexOf(BLUEZ_DEVICE_ID) > -1) { if (interfaces[BLUEZ_DEVICE_ID]['UUIDs'].value.indexOf(PLEJD_SERVICE) > -1) { logger('found Plejd service on ' + path); - this.bleDevices.push({ 'path': path }); + this.bleDevices.push({ + 'path': path + }); } else { console.log('uh oh, no Plejd device.'); } @@ -232,8 +230,7 @@ class PlejdService extends EventEmitter { updateSettings(settings) { if (settings.debug) { debug = 'console'; - } - else { + } else { debug = ''; } } @@ -263,8 +260,7 @@ class PlejdService extends EventEmitter { i++; }, 400); - } - else { + } else { this._turnOn(id, brightness); } } @@ -290,6 +286,8 @@ class PlejdService extends EventEmitter { // 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] ? this.plejdDevices[id].dim : 250; + console.log('initial brightness for ' + id + ' is ' + initialBrightness); + const steps = command.transition * 2; const brightnessStep = initialBrightness / steps; let currentBrightness = initialBrightness; @@ -309,8 +307,7 @@ class PlejdService extends EventEmitter { i++; }, 500); - } - else { + } else { this._turnOff(id); } } @@ -337,8 +334,7 @@ class PlejdService extends EventEmitter { const response = this._createChallengeResponse(this.cryptoKey, Buffer.from(challenge)); //logger('responding to authenticate'); await this.characteristics.auth.WriteValue([...response], {}); - } - catch (err) { + } catch (err) { console.log('plejd-ble: error: failed to authenticate: ' + err); } @@ -361,8 +357,7 @@ class PlejdService extends EventEmitter { console.log('plejd-ble: sending ' + data.length + ' byte(s) of data to Plejd'); const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data); await this.characteristics.data.WriteValue([...encryptedData], {}); - } - catch (err) { + } catch (err) { if (err.message === 'In Progress') { setTimeout(() => this.write(data, retry), 1000); return; @@ -411,8 +406,7 @@ class PlejdService extends EventEmitter { try { await this.characteristics.ping.WriteValue([...ping], {}); pong = await this.characteristics.ping.ReadValue({}); - } - catch (err) { + } catch (err) { console.log('error: writing to plejd: ' + err); this.emit('pingFailed', 'write error'); return; @@ -460,9 +454,9 @@ class PlejdService extends EventEmitter { const addr = this._reverseBuffer( Buffer.from( String(dirtyAddr[1]) - .replace(/\-/g, '') - .replace(/\_/g, '') - .replace(/\:/g, ''), 'hex' + .replace(/\-/g, '') + .replace(/\_/g, '') + .replace(/\:/g, ''), 'hex' ) ); @@ -476,17 +470,14 @@ class PlejdService extends EventEmitter { if (chUuid === DATA_UUID) { logger('found DATA characteristic.'); this.characteristics.data = ch; - } - else if (chUuid === LAST_DATA_UUID) { + } else if (chUuid === LAST_DATA_UUID) { logger('found LAST_DATA characteristic.'); this.characteristics.lastData = ch; this.characteristics.lastDataProperties = prop; - } - else if (chUuid === AUTH_UUID) { + } else if (chUuid === AUTH_UUID) { logger('found AUTH characteristic.'); this.characteristics.auth = ch; - } - else if (chUuid === PING_UUID) { + } else if (chUuid === PING_UUID) { logger('found PING characteristic.'); this.characteristics.ping = ch; } @@ -583,23 +574,33 @@ class PlejdService extends EventEmitter { state = parseInt(decoded.toString('hex', 5, 6), 10); dim = parseInt(decoded.toString('hex', 6, 8), 16) >> 8; + this.plejdDevices[device] = { + state: state, + dim: dim + }; + logger('d: ' + device + ' got state+dim update: ' + state + ' - ' + dim); - this.emit('stateChanged', device, { state: state, brightness: dim }); - } - else if (cmd === BLE_CMD_STATE_CHANGE) { + this.emit('stateChanged', device, { + state: state, + brightness: dim + }); + + return; + } 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: state }); - } - else if (cmd === BLE_CMD_SCENE_TRIG) { + 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 + dim: 0 }; } @@ -651,4 +652,4 @@ class PlejdService extends EventEmitter { } } -module.exports = PlejdService; +module.exports = PlejdService; \ No newline at end of file diff --git a/plejd/config.json b/plejd/config.json index 40e1c0b..de1884e 100644 --- a/plejd/config.json +++ b/plejd/config.json @@ -1,6 +1,6 @@ { "name": "Plejd", - "version": "0.4.6", + "version": "0.4.7", "slug": "plejd", "description": "Adds support for the Swedish home automation devices from Plejd.", "url": "https://github.com/icanos/hassio-plejd/", @@ -38,4 +38,4 @@ "connectionTimeout": "int", "writeQueueWaitTime": "int" } -} +} \ No newline at end of file diff --git a/plejd/main.js b/plejd/main.js index 16a6c68..e9e942b 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -4,7 +4,7 @@ const fs = require('fs'); const PlejdService = require('./ble.bluez'); const SceneManager = require('./scene.manager'); -const version = "0.4.6"; +const version = "0.4.7"; async function main() { console.log('starting Plejd add-on v. ' + version); @@ -76,22 +76,24 @@ async function main() { if (typeof command === 'string') { // switch command state = command; - commandObj = { state: state }; + commandObj = { + state: state + }; // 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. - client.updateState(deviceId, { state: state === 'ON' ? 1 : 0 }); - } - else { + client.updateState(deviceId, { + state: state === 'ON' ? 1 : 0 + }); + } else { state = command.state; commandObj = command; } if (state === 'ON') { plejd.turnOn(deviceId, commandObj); - } - else { + } else { plejd.turnOff(deviceId, commandObj); } }); @@ -99,11 +101,9 @@ async function main() { client.on('settingsChanged', (settings) => { if (settings.module === 'mqtt') { client.updateSettings(settings); - } - else if (settings.module === 'ble') { + } else if (settings.module === 'ble') { plejd.updateSettings(settings); - } - else if (settings.module === 'api') { + } else if (settings.module === 'api') { plejdApi.updateSettings(settings); } }); @@ -112,4 +112,4 @@ async function main() { }); } -main(); +main(); \ No newline at end of file