diff --git a/APPSTORE.md b/APPSTORE.md index c2d5156..920ecd0 100644 --- a/APPSTORE.md +++ b/APPSTORE.md @@ -30,6 +30,7 @@ Xiaomi has recently released an update for its v1 vacuum that enabled it for the ### 2018-09-05 - v2.8.2 * FIX: fixed bug with return to dock action card for Vacuum Cleaner * FIX: fixed where the Vacuum Cleaner always shows as charging even thought the battery is full +* IMPROVEMENT: better error catching in polling mechanism for miio devices * UPDATE: removed support for Philips Eyecare since there is a bug in the miio library for this device (see https://github.com/aholstenson/miio/issues/145) ### 2018-08-22 - v2.8.1 diff --git a/drivers/air-monitor/device.js b/drivers/air-monitor/device.js index 5e68a15..20d1e17 100644 --- a/drivers/air-monitor/device.js +++ b/drivers/air-monitor/device.js @@ -29,6 +29,9 @@ class MiAirMonitorDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 60; @@ -61,8 +64,12 @@ class MiAirMonitorDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/gateway/device.js b/drivers/gateway/device.js index 3148648..bc19e73 100644 --- a/drivers/gateway/device.js +++ b/drivers/gateway/device.js @@ -64,6 +64,9 @@ class GatewayDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; this.miio.light = miiodevice.child('light'); @@ -116,8 +119,12 @@ class GatewayDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/mi-airpurifier/device.js b/drivers/mi-airpurifier/device.js index 726f907..e90779e 100644 --- a/drivers/mi-airpurifier/device.js +++ b/drivers/mi-airpurifier/device.js @@ -29,6 +29,9 @@ class MiAirPurifierDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 60; @@ -69,8 +72,12 @@ class MiAirPurifierDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/mi-humidifier/device.js b/drivers/mi-humidifier/device.js index d203a1a..67469d6 100644 --- a/drivers/mi-humidifier/device.js +++ b/drivers/mi-humidifier/device.js @@ -29,6 +29,9 @@ class MiHumidifierDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 60; @@ -65,8 +68,12 @@ class MiHumidifierDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/mi-power-plug/device.js b/drivers/mi-power-plug/device.js index e522ab0..82399a2 100644 --- a/drivers/mi-power-plug/device.js +++ b/drivers/mi-power-plug/device.js @@ -28,6 +28,9 @@ class PowerPlugDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 30; @@ -52,8 +55,12 @@ class PowerPlugDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/mi-power-strip/device.js b/drivers/mi-power-strip/device.js index 9ddfce3..6141186 100644 --- a/drivers/mi-power-strip/device.js +++ b/drivers/mi-power-strip/device.js @@ -28,6 +28,9 @@ class PowerStripDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 30; @@ -61,8 +64,12 @@ class PowerStripDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData(); diff --git a/drivers/mi-robot/device.js b/drivers/mi-robot/device.js index 9cdf527..39def29 100644 --- a/drivers/mi-robot/device.js +++ b/drivers/mi-robot/device.js @@ -88,6 +88,9 @@ class MiRobotDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; var interval = this.getSetting('polling') || 60; @@ -101,42 +104,55 @@ class MiRobotDevice extends Homey.Device { clearInterval(this.pollingInterval); this.pollingInterval = setInterval(() => { - var battery = this.miio.getState('batteryLevel'); - var fanspeed = this.miio.getState('fanSpeed'); + const getData = async () => { + try { + var battery = this.miio.getState('batteryLevel'); + var fanspeed = this.miio.getState('fanSpeed'); - if (this.miio.property('state') == 'charging' && battery !== 100) { - var onoff = false; - var state = 'charging'; - } else if (this.miio.property('state') == 'docking' || this.miio.property('state') == 'full' || this.miio.property('state') == 'returning' || this.miio.property('state') == 'waiting' || this.miio.property('state') == 'charging') { - var onoff = false; - var state = 'docked'; - } else if (this.miio.property('state') == 'cleaning' || this.miio.property('state') == 'zone-cleaning') { - var onoff = true; - var state = 'cleaning'; - } else if (this.miio.property('state') == 'spot-cleaning') { - var onoff = true; - var state = 'spot_cleaning'; - } else { - var onoff = false; - var state = 'stopped'; - } + if (this.miio.property('state') == 'charging' && battery !== 100) { + var onoff = false; + var state = 'charging'; + } else if (this.miio.property('state') == 'docking' || this.miio.property('state') == 'full' || this.miio.property('state') == 'returning' || this.miio.property('state') == 'waiting' || this.miio.property('state') == 'charging') { + var onoff = false; + var state = 'docked'; + } else if (this.miio.property('state') == 'cleaning' || this.miio.property('state') == 'zone-cleaning') { + var onoff = true; + var state = 'cleaning'; + } else if (this.miio.property('state') == 'spot-cleaning') { + var onoff = true; + var state = 'spot_cleaning'; + } else { + var onoff = false; + var state = 'stopped'; + } - if (this.getCapabilityValue('onoff') != onoff) { - this.setCapabilityValue('onoff', onoff); - } - if (this.getCapabilityValue('vacuumcleaner_state') != state) { - this.setCapabilityValue('vacuumcleaner_state', state); - } - if (this.getCapabilityValue('measure_battery') != battery) { - this.setCapabilityValue('measure_battery', battery); - } - if (this.getStoreValue('fanspeed') != fanspeed) { - this.setStoreValue('fanspeed', fanspeed); - } - if (this.getStoreValue('state') != this.miio.property('state')) { - this.setStoreValue('state', this.miio.property('state')); - Homey.ManagerFlow.getCard('trigger', 'statusVacuum').trigger(this, {status: this.miio.property('state')}, {}) + if (this.getCapabilityValue('onoff') != onoff) { + this.setCapabilityValue('onoff', onoff); + } + if (this.getCapabilityValue('vacuumcleaner_state') != state) { + this.setCapabilityValue('vacuumcleaner_state', state); + } + if (this.getCapabilityValue('measure_battery') != battery) { + this.setCapabilityValue('measure_battery', battery); + } + if (this.getStoreValue('fanspeed') != fanspeed) { + this.setStoreValue('fanspeed', fanspeed); + } + if (this.getStoreValue('state') != this.miio.property('state')) { + this.setStoreValue('state', this.miio.property('state')); + Homey.ManagerFlow.getCard('trigger', 'statusVacuum').trigger(this, {status: this.miio.property('state')}, {}) + } + } + catch (error) { + this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); + } } + getData(); }, 1000 * interval); } } diff --git a/drivers/philips-bulb/device.js b/drivers/philips-bulb/device.js index 43075c4..719b203 100644 --- a/drivers/philips-bulb/device.js +++ b/drivers/philips-bulb/device.js @@ -47,6 +47,9 @@ class PhilipsBulbDevice extends Homey.Device { address: this.getSetting('address'), token: this.getSetting('token') }).then(miiodevice => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.miio = miiodevice; this.miio.on('colorChanged', c => { @@ -83,8 +86,12 @@ class PhilipsBulbDevice extends Homey.Device { this.setAvailable(); } } catch (error) { - this.setUnavailable(Homey.__('unreachable')); this.log(error); + clearInterval(this.pollingInterval); + this.setUnavailable(Homey.__('unreachable')); + setTimeout(() => { + this.createDevice(); + }, 1000 * interval); } } getData();