From 7d6dbb5fa291200092764c75458594f1974ea58b Mon Sep 17 00:00:00 2001 From: Lucas van der Have Date: Wed, 5 Jun 2019 11:19:22 +0200 Subject: [PATCH] :bug: Fixed issue where init of client would fail without credentials :art: Refactor getting of mac address to `network/devices` call :bug: Fix bug in pairing process for tv which do not have the `notifychange` endpoint --- drivers/philips-jointspace/device.js | 13 ++++++++++++- drivers/philips-jointspace/driver.js | 16 ++++++++-------- lib/JointspaceClient.js | 4 ++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/philips-jointspace/device.js b/drivers/philips-jointspace/device.js index 5044393..e01d8b0 100644 --- a/drivers/philips-jointspace/device.js +++ b/drivers/philips-jointspace/device.js @@ -17,7 +17,12 @@ class PhilipsTV extends Homey.Device { this._settings = this.getSettings(); this.client = new JointspaceClient(this.getCredentials().user); - this.client.setConfig(this.getIPAddress(), this.getAPIVersion(), this.getCredentials().user, this.getCredentials().pass); + + if (this.hasCredentials()) { + this.client.setConfig(this.getIPAddress(), this.getAPIVersion(), this.getCredentials().user, this.getCredentials().pass); + } else { + this.client.setConfig(this.getIPAddress(), this.getAPIVersion()); + } this.log('PhilipsTV Device [' + this.getCredentials().user + '] initialized'); @@ -76,6 +81,12 @@ class PhilipsTV extends Homey.Device { return this._data.credentials; } + hasCredentials() { + return this._data.credentials !== null + && (typeof this._data.credentials.user !== "undefined") + && this._data.credentials.user !== null; + } + updateDevice() { this.client.getInfo().then((response) => { this.setCapabilityValue('onoff', (response.powerstate.powerstate === 'On')) diff --git a/drivers/philips-jointspace/driver.js b/drivers/philips-jointspace/driver.js index 990b7cd..24a7134 100644 --- a/drivers/philips-jointspace/driver.js +++ b/drivers/philips-jointspace/driver.js @@ -131,22 +131,22 @@ class PhilipsJointSpaceDriver extends Homey.Driver { }); socket.on('verify_wol', (data, callback) => { - this.jointspaceClient.getInfo().then((response) => { - if (typeof response['network/devices'] !== "undefined") { - for (let i in response['network/devices']) { - let networkAdapter = response['network/devices'][i]; + this.jointspaceClient.getNetworkDevices().then((networkDevices) => { + if (Array.isArray(networkDevices)) { + for (let i in networkDevices) { + let networkDevice = networkDevices[i]; - if (typeof networkAdapter['wake-on-lan'] !== "undefined" && networkAdapter['wake-on-lan'] === 'Enabled') { - pairingDevice.data.mac = networkAdapter["mac"]; + if (typeof networkDevice['wake-on-lan'] !== "undefined" && networkDevice['wake-on-lan'] === 'Enabled') { + pairingDevice.data.mac = networkDevice["mac"]; callback(null, true); break; } } } else { this.error('Could not get mac address from tv device') + console.log('Could not get mac address from tv', JSON.stringify(networkDevices)); + callback(null, false); } - - callback(null, false); }).catch((error) => { callback(null, false); this.error(error); diff --git a/lib/JointspaceClient.js b/lib/JointspaceClient.js index 40fd08a..2c1955b 100644 --- a/lib/JointspaceClient.js +++ b/lib/JointspaceClient.js @@ -429,6 +429,10 @@ class JointspaceClient { }); } + getNetworkDevices() { + return this.request('network/devices', 'GET'); + } + getPowerState() { return this.request('powerstate', 'GET'); }