Skip to content

Commit

Permalink
Merge pull request #89 from icanos/feature/transitions
Browse files Browse the repository at this point in the history
transitions work
  • Loading branch information
icanos authored Jun 12, 2020
2 parents 38b8b8f + 4455d6c commit d972e27
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
77 changes: 39 additions & 38 deletions plejd/ble.bluez.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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.');
}
Expand All @@ -232,8 +230,7 @@ class PlejdService extends EventEmitter {
updateSettings(settings) {
if (settings.debug) {
debug = 'console';
}
else {
} else {
debug = '';
}
}
Expand Down Expand Up @@ -263,8 +260,7 @@ class PlejdService extends EventEmitter {

i++;
}, 400);
}
else {
} else {
this._turnOn(id, brightness);
}
}
Expand All @@ -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;
Expand All @@ -309,8 +307,7 @@ class PlejdService extends EventEmitter {

i++;
}, 500);
}
else {
} else {
this._turnOff(id);
}
}
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'
)
);

Expand All @@ -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;
}
Expand Down Expand Up @@ -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
};
}

Expand Down Expand Up @@ -651,4 +652,4 @@ class PlejdService extends EventEmitter {
}
}

module.exports = PlejdService;
module.exports = PlejdService;
4 changes: 2 additions & 2 deletions plejd/config.json
Original file line number Diff line number Diff line change
@@ -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/",
Expand Down Expand Up @@ -38,4 +38,4 @@
"connectionTimeout": "int",
"writeQueueWaitTime": "int"
}
}
}
24 changes: 12 additions & 12 deletions plejd/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -76,34 +76,34 @@ 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);
}
});

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);
}
});
Expand All @@ -112,4 +112,4 @@ async function main() {
});
}

main();
main();

0 comments on commit d972e27

Please sign in to comment.