Merge pull request #89 from icanos/feature/transitions

transitions work
This commit is contained in:
Marcus Westin 2020-06-12 11:17:16 +02:00 committed by GitHub
commit d972e27d17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 52 deletions

View file

@ -135,8 +135,7 @@ class PlejdService extends EventEmitter {
try { try {
await this.adapter.StartDiscovery(); 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.'); console.log('plejd-ble: error: failed to start discovery. Make sure no other add-on is currently scanning.');
return; return;
} }
@ -166,8 +165,7 @@ class PlejdService extends EventEmitter {
plejd['device'] = this.devices.find(x => x.serialNumber === fixedPlejdPath); plejd['device'] = this.devices.find(x => x.serialNumber === fixedPlejdPath);
logger('discovered ' + plejd['path'] + ' with rssi ' + plejd['rssi']); logger('discovered ' + plejd['path'] + ' with rssi ' + plejd['rssi']);
} } catch (err) {
catch (err) {
console.log('plejd-ble: failed inspecting ' + plejd['path'] + ' error: ' + err); console.log('plejd-ble: failed inspecting ' + plejd['path'] + ' error: ' + err);
} }
} }
@ -183,8 +181,7 @@ class PlejdService extends EventEmitter {
connectedDevice = plejd; connectedDevice = plejd;
break break
} }
} } catch (err) {
catch (err) {
console.log('plejd-ble: warning: unable to connect, will retry. ' + err); console.log('plejd-ble: warning: unable to connect, will retry. ' + err);
} }
} }
@ -205,8 +202,7 @@ class PlejdService extends EventEmitter {
try { try {
const adapterObject = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, path); const adapterObject = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, path);
return [path, adapterObject.getInterface(iface), adapterObject]; return [path, adapterObject.getInterface(iface), adapterObject];
} } catch (err) {
catch (err) {
console.log('plejd-ble: error: failed to get interface \'' + iface + '\': ' + 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 (interfaceKeys.indexOf(BLUEZ_DEVICE_ID) > -1) {
if (interfaces[BLUEZ_DEVICE_ID]['UUIDs'].value.indexOf(PLEJD_SERVICE) > -1) { if (interfaces[BLUEZ_DEVICE_ID]['UUIDs'].value.indexOf(PLEJD_SERVICE) > -1) {
logger('found Plejd service on ' + path); logger('found Plejd service on ' + path);
this.bleDevices.push({ 'path': path }); this.bleDevices.push({
'path': path
});
} else { } else {
console.log('uh oh, no Plejd device.'); console.log('uh oh, no Plejd device.');
} }
@ -232,8 +230,7 @@ class PlejdService extends EventEmitter {
updateSettings(settings) { updateSettings(settings) {
if (settings.debug) { if (settings.debug) {
debug = 'console'; debug = 'console';
} } else {
else {
debug = ''; debug = '';
} }
} }
@ -263,8 +260,7 @@ class PlejdService extends EventEmitter {
i++; i++;
}, 400); }, 400);
} } else {
else {
this._turnOn(id, brightness); 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) // we have a transition time, split the target brightness (which will be 0)
// into pieces spread of the transition time // into pieces spread of the transition time
const initialBrightness = this.plejdDevices[id] ? this.plejdDevices[id].dim : 250; const initialBrightness = this.plejdDevices[id] ? this.plejdDevices[id].dim : 250;
console.log('initial brightness for ' + id + ' is ' + initialBrightness);
const steps = command.transition * 2; const steps = command.transition * 2;
const brightnessStep = initialBrightness / steps; const brightnessStep = initialBrightness / steps;
let currentBrightness = initialBrightness; let currentBrightness = initialBrightness;
@ -309,8 +307,7 @@ class PlejdService extends EventEmitter {
i++; i++;
}, 500); }, 500);
} } else {
else {
this._turnOff(id); this._turnOff(id);
} }
} }
@ -337,8 +334,7 @@ class PlejdService extends EventEmitter {
const response = this._createChallengeResponse(this.cryptoKey, Buffer.from(challenge)); const response = this._createChallengeResponse(this.cryptoKey, Buffer.from(challenge));
//logger('responding to authenticate'); //logger('responding to authenticate');
await this.characteristics.auth.WriteValue([...response], {}); await this.characteristics.auth.WriteValue([...response], {});
} } catch (err) {
catch (err) {
console.log('plejd-ble: error: failed to authenticate: ' + 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'); console.log('plejd-ble: sending ' + data.length + ' byte(s) of data to Plejd');
const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data); const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data);
await this.characteristics.data.WriteValue([...encryptedData], {}); await this.characteristics.data.WriteValue([...encryptedData], {});
} } catch (err) {
catch (err) {
if (err.message === 'In Progress') { if (err.message === 'In Progress') {
setTimeout(() => this.write(data, retry), 1000); setTimeout(() => this.write(data, retry), 1000);
return; return;
@ -411,8 +406,7 @@ class PlejdService extends EventEmitter {
try { try {
await this.characteristics.ping.WriteValue([...ping], {}); await this.characteristics.ping.WriteValue([...ping], {});
pong = await this.characteristics.ping.ReadValue({}); pong = await this.characteristics.ping.ReadValue({});
} } catch (err) {
catch (err) {
console.log('error: writing to plejd: ' + err); console.log('error: writing to plejd: ' + err);
this.emit('pingFailed', 'write error'); this.emit('pingFailed', 'write error');
return; return;
@ -460,9 +454,9 @@ class PlejdService extends EventEmitter {
const addr = this._reverseBuffer( const addr = this._reverseBuffer(
Buffer.from( Buffer.from(
String(dirtyAddr[1]) String(dirtyAddr[1])
.replace(/\-/g, '') .replace(/\-/g, '')
.replace(/\_/g, '') .replace(/\_/g, '')
.replace(/\:/g, ''), 'hex' .replace(/\:/g, ''), 'hex'
) )
); );
@ -476,17 +470,14 @@ class PlejdService extends EventEmitter {
if (chUuid === DATA_UUID) { if (chUuid === DATA_UUID) {
logger('found DATA characteristic.'); logger('found DATA characteristic.');
this.characteristics.data = ch; this.characteristics.data = ch;
} } else if (chUuid === LAST_DATA_UUID) {
else if (chUuid === LAST_DATA_UUID) {
logger('found LAST_DATA characteristic.'); logger('found LAST_DATA characteristic.');
this.characteristics.lastData = ch; this.characteristics.lastData = ch;
this.characteristics.lastDataProperties = prop; this.characteristics.lastDataProperties = prop;
} } else if (chUuid === AUTH_UUID) {
else if (chUuid === AUTH_UUID) {
logger('found AUTH characteristic.'); logger('found AUTH characteristic.');
this.characteristics.auth = ch; this.characteristics.auth = ch;
} } else if (chUuid === PING_UUID) {
else if (chUuid === PING_UUID) {
logger('found PING characteristic.'); logger('found PING characteristic.');
this.characteristics.ping = ch; this.characteristics.ping = ch;
} }
@ -583,23 +574,33 @@ class PlejdService extends EventEmitter {
state = parseInt(decoded.toString('hex', 5, 6), 10); state = parseInt(decoded.toString('hex', 5, 6), 10);
dim = parseInt(decoded.toString('hex', 6, 8), 16) >> 8; 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); logger('d: ' + device + ' got state+dim update: ' + state + ' - ' + dim);
this.emit('stateChanged', device, { state: state, brightness: dim }); this.emit('stateChanged', device, {
} state: state,
else if (cmd === BLE_CMD_STATE_CHANGE) { brightness: dim
});
return;
} else if (cmd === BLE_CMD_STATE_CHANGE) {
state = parseInt(decoded.toString('hex', 5, 6), 10); state = parseInt(decoded.toString('hex', 5, 6), 10);
logger('d: ' + device + ' got state update: ' + state); logger('d: ' + device + ' got state update: ' + state);
this.emit('stateChanged', device, { state: state }); this.emit('stateChanged', device, {
} state: state
else if (cmd === BLE_CMD_SCENE_TRIG) { });
} else if (cmd === BLE_CMD_SCENE_TRIG) {
const scene = parseInt(decoded.toString('hex', 5, 6), 10); const scene = parseInt(decoded.toString('hex', 5, 6), 10);
this.emit('sceneTriggered', device, scene); this.emit('sceneTriggered', device, scene);
} }
this.plejdDevices[device] = { this.plejdDevices[device] = {
state: state, state: state,
dim: dim dim: 0
}; };
} }
@ -651,4 +652,4 @@ class PlejdService extends EventEmitter {
} }
} }
module.exports = PlejdService; module.exports = PlejdService;

View file

@ -1,6 +1,6 @@
{ {
"name": "Plejd", "name": "Plejd",
"version": "0.4.6", "version": "0.4.7",
"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/",
@ -38,4 +38,4 @@
"connectionTimeout": "int", "connectionTimeout": "int",
"writeQueueWaitTime": "int" "writeQueueWaitTime": "int"
} }
} }

View file

@ -4,7 +4,7 @@ const fs = require('fs');
const PlejdService = require('./ble.bluez'); const PlejdService = require('./ble.bluez');
const SceneManager = require('./scene.manager'); const SceneManager = require('./scene.manager');
const version = "0.4.6"; const version = "0.4.7";
async function main() { async function main() {
console.log('starting Plejd add-on v. ' + version); console.log('starting Plejd add-on v. ' + version);
@ -76,22 +76,24 @@ async function main() {
if (typeof command === 'string') { if (typeof command === 'string') {
// switch command // switch command
state = command; state = command;
commandObj = { state: state }; commandObj = {
state: state
};
// since the switch doesn't get any updates on whether it's on or not, // 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 // we fake this by directly send the updateState back to HA in order for
// it to change state. // it to change state.
client.updateState(deviceId, { state: state === 'ON' ? 1 : 0 }); client.updateState(deviceId, {
} state: state === 'ON' ? 1 : 0
else { });
} else {
state = command.state; state = command.state;
commandObj = command; commandObj = command;
} }
if (state === 'ON') { if (state === 'ON') {
plejd.turnOn(deviceId, commandObj); plejd.turnOn(deviceId, commandObj);
} } else {
else {
plejd.turnOff(deviceId, commandObj); plejd.turnOff(deviceId, commandObj);
} }
}); });
@ -99,11 +101,9 @@ async function main() {
client.on('settingsChanged', (settings) => { client.on('settingsChanged', (settings) => {
if (settings.module === 'mqtt') { if (settings.module === 'mqtt') {
client.updateSettings(settings); client.updateSettings(settings);
} } else if (settings.module === 'ble') {
else if (settings.module === 'ble') {
plejd.updateSettings(settings); plejd.updateSettings(settings);
} } else if (settings.module === 'api') {
else if (settings.module === 'api') {
plejdApi.updateSettings(settings); plejdApi.updateSettings(settings);
} }
}); });
@ -112,4 +112,4 @@ async function main() {
}); });
} }
main(); main();