From fc7e96eae9cb6be9ff35a2d340b3970341306d50 Mon Sep 17 00:00:00 2001 From: Maxmudjon Date: Tue, 24 Nov 2020 04:29:01 +0500 Subject: [PATCH] v1.1.9 minor fixes --- README.md | 7 +- app.json | 14 +- drivers/button_switch.aq2/device.js | 10 +- drivers/button_switch.aq3/device.js | 10 +- drivers/button_switch/device.js | 10 +- drivers/cgllc_airmonitor_b1/device.js | 50 ++-- drivers/cgllc_airmonitor_s1/device.js | 67 ++--- drivers/chuangmi_plug_m1/device.js | 74 ++++-- drivers/chuangmi_plug_v1/device.js | 82 +++--- drivers/chuangmi_plug_v3/device.js | 90 ++++--- drivers/ctrl_86plug.aq1/device.js | 12 +- drivers/ctrl_ln1.aq1/device.js | 12 +- drivers/ctrl_ln2.aq1/device.js | 12 +- drivers/ctrl_neutral1/device.js | 12 +- drivers/ctrl_neutral2/device.js | 12 +- drivers/cube/device.js | 12 +- drivers/curtain.aq2/device.js | 16 +- drivers/curtain/device.js | 16 +- drivers/db86_switch/device.js | 10 +- drivers/db86_switch_advanced/device.js | 10 +- drivers/deerma_humidifier_mjjsq/device.js | 144 +++++----- drivers/dmaker_airfresh_t2017/device.js | 199 +++++++------- drivers/gateway/device.js | 18 +- drivers/gateway_radio/device.js | 104 ++++---- drivers/gateway_security/device.js | 65 ++--- drivers/ir_remote/device.js | 32 +-- drivers/light_aqcn02/device.js | 48 ++-- drivers/lock.aq1/device.js | 30 +-- drivers/magnet_sensor.aq2/device.js | 10 +- drivers/magnet_sensor/device.js | 10 +- drivers/mi_air_purifier_ma2/device.js | 145 +++++----- drivers/mi_air_purifier_v6/device.js | 167 ++++++------ drivers/mi_fan_sa1/device.js | 130 ++++----- drivers/mi_fan_sa1/driver.js | 52 ++-- drivers/mi_fan_sa1/pair/xiaomi.html | 76 ++---- drivers/mi_humidifier_ca1/device.js | 131 ++++----- drivers/mi_humidifier_cb1/device.js | 129 ++++----- drivers/mi_humidifier_v1/device.js | 113 ++++---- drivers/mi_motion_sensor/device.js | 10 +- drivers/mi_robot_1s/device.js | 249 +++++++++--------- drivers/mi_rockrobo_vacuum_s5/device.js | 242 ++++++++--------- drivers/mi_rockrobo_vacuum_s5/driver.js | 3 +- .../mi_rockrobo_vacuum_s5/pair/xiaomi.html | 2 +- drivers/mi_rockrobo_vacuum_t6/device.js | 243 ++++++++--------- drivers/mi_rockrobo_vacuum_v1/device.js | 227 ++++++++-------- drivers/motion_sensor.aq2/device.js | 10 +- drivers/natgas_sensor/device.js | 10 +- drivers/philips_light_bulb/device.js | 94 ++++--- drivers/philips_light_candle/device.js | 95 ++++--- drivers/philips_light_candle2/device.js | 94 ++++--- drivers/philips_light_ceiling/device.js | 100 ++++--- drivers/philips_light_downlight/device.js | 94 ++++--- drivers/philips_light_moonlight/device.js | 97 ++++--- drivers/philips_light_sread1/device.js | 118 +++++---- drivers/plug/device.js | 12 +- drivers/qmi_powerstrip_v1/device.js | 83 +++--- drivers/relay_c2acn01/device.js | 100 +++---- drivers/sb86_switch/device.js | 10 +- drivers/smoke_sensor/device.js | 10 +- drivers/temp_hum_sensor/device.js | 10 +- drivers/vibration_sensor/device.js | 10 +- drivers/viomi_vacuum_v7/device.js | 191 +++++++------- drivers/weather_temp_hum_sensor/device.js | 10 +- drivers/wleak_sensor/device.js | 10 +- drivers/yeelight_light_bslamp1/device.js | 119 +++++---- drivers/yeelight_light_ceiling1/device.js | 106 ++++---- drivers/yeelight_light_ceiling10/device.js | 205 +++++++------- drivers/yeelight_light_ceiling3/device.js | 105 ++++---- drivers/yeelight_light_ceiling4/device.js | 194 +++++++------- drivers/yeelight_light_color1/device.js | 152 ++++++----- drivers/yeelight_light_lamp1/device.js | 95 ++++--- drivers/yeelight_light_mono1/device.js | 83 +++--- drivers/yeelight_light_strip1/device.js | 119 +++++---- drivers/zhimi_airfresh_va2/device.js | 130 ++++----- drivers/zhimi_airfresh_va4/device.js | 67 ++--- drivers/zimi_powerstrip_v2/device.js | 82 +++--- 76 files changed, 3005 insertions(+), 2727 deletions(-) diff --git a/README.md b/README.md index ba190f3..8c74ee0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Added support for Mi Gateways child devices. -## Version 1.1.8 - Supported devices: +## Version 1.1.9 - Supported devices: ### Zigbee subdevices Xiaomi Gateway @@ -128,6 +128,11 @@ Added support for Mi Gateways child devices. ## Version logs +### 1.1.9 (24.11.2020) + +1. fix Air Fresh VA4. +2. minor fixes. + ### 1.1.8 (23.10.2020) 1. added Xiaomi Air Fresh VA4. diff --git a/app.json b/app.json index 4cc8cc2..de92cf9 100644 --- a/app.json +++ b/app.json @@ -1,6 +1,6 @@ { "id": "com.maxmudjon.mihomey", - "version": "1.1.8", + "version": "1.1.9", "compatibility": ">=2.0.0", "sdk": 2, "name": { @@ -9286,7 +9286,8 @@ "alarm_main_brush_work_time", "alarm_side_brush_work_time", "alarm_filter_work_time", - "alarm_sensor_dirty_time" + "alarm_sensor_dirty_time", + "button.consumable" ], "energy": { "batteries": ["OTHER"] @@ -9306,6 +9307,15 @@ "title": { "en": "Speed" } + }, + "button.consumable": { + "maintenanceAction": true, + "title": { + "en": "Reset consumable" + }, + "desc": { + "en": "Reset the vacuum cleaner consumables." + } } }, "mobile": { diff --git a/drivers/button_switch.aq2/device.js b/drivers/button_switch.aq2/device.js index 2806b9d..5b80a42 100644 --- a/drivers/button_switch.aq2/device.js +++ b/drivers/button_switch.aq2/device.js @@ -40,10 +40,10 @@ class AqaraButtonSwitch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -52,7 +52,7 @@ class AqaraButtonSwitch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -78,7 +78,7 @@ class AqaraButtonSwitch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -104,7 +104,7 @@ class AqaraButtonSwitch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/button_switch.aq3/device.js b/drivers/button_switch.aq3/device.js index 5b7f82d..5af6b97 100644 --- a/drivers/button_switch.aq3/device.js +++ b/drivers/button_switch.aq3/device.js @@ -48,10 +48,10 @@ class AqaraButtonSwitch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -60,7 +60,7 @@ class AqaraButtonSwitch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -86,7 +86,7 @@ class AqaraButtonSwitch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -120,7 +120,7 @@ class AqaraButtonSwitch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/button_switch/device.js b/drivers/button_switch/device.js index 963097f..9460f66 100644 --- a/drivers/button_switch/device.js +++ b/drivers/button_switch/device.js @@ -40,10 +40,10 @@ class ButtonSwitch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -52,7 +52,7 @@ class ButtonSwitch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi.sensor_" + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -78,7 +78,7 @@ class ButtonSwitch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -104,7 +104,7 @@ class ButtonSwitch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/cgllc_airmonitor_b1/device.js b/drivers/cgllc_airmonitor_b1/device.js index 58601d9..d5dca2e 100644 --- a/drivers/cgllc_airmonitor_b1/device.js +++ b/drivers/cgllc_airmonitor_b1/device.js @@ -10,10 +10,9 @@ class MiAirQualityMonitor2Gen extends Homey.Device { } getAirFreshStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { + .then((device) => { if (!this.getAvailable()) { this.setAvailable(); } @@ -22,22 +21,21 @@ class MiAirQualityMonitor2Gen extends Homey.Device { this.device .call("get_air_data", []) - .then(result => { - - that.updateCapabilityValue("measure_pm25", parseInt(result.result.pm25)); - that.updateCapabilityValue("measure_co2", parseInt(result.result.co2e)); - that.updateCapabilityValue("measure_humidity", parseInt(result.result.humidity)); - that.updateCapabilityValue("measure_temperature", parseInt(result.result.temperature)); - that.updateCapabilityValue("measure_voc", parseInt(result.result.tvoc)) + .then((result) => { + this.updateCapabilityValue("measure_pm25", parseInt(result.result.pm25)); + this.updateCapabilityValue("measure_co2", parseInt(result.result.co2e)); + this.updateCapabilityValue("measure_humidity", parseInt(result.result.humidity)); + this.updateCapabilityValue("measure_temperature", parseInt(result.result.temperature)); + this.updateCapabilityValue("measure_voc", parseInt(result.result.tvoc)); }) - .catch(error => that.log("Sending commmand 'get_air_data' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_air_data' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - this.setUnavailable(Homey.__("reconnecting")); + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getAirFreshStatus(); }, 10000); @@ -45,22 +43,24 @@ class MiAirQualityMonitor2Gen extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_air_data", []) - .then(result => { - that.updateCapabilityValue("measure_pm25", parseInt(result.result.pm25)); - that.updateCapabilityValue("measure_co2", parseInt(result.result.co2e)); - that.updateCapabilityValue("measure_humidity", parseInt(result.result.humidity)); - that.updateCapabilityValue("measure_temperature", parseInt(result.result.temperature)); - that.updateCapabilityValue("measure_voc", parseInt(result.result.tvoc)) + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("measure_pm25", parseInt(result.result.pm25)); + this.updateCapabilityValue("measure_co2", parseInt(result.result.co2e)); + this.updateCapabilityValue("measure_humidity", parseInt(result.result.humidity)); + this.updateCapabilityValue("measure_temperature", parseInt(result.result.temperature)); + this.updateCapabilityValue("measure_voc", parseInt(result.result.tvoc)); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'get_air_data' error: ", error); clearInterval(this.updateInterval); - this.setUnavailable(Homey.__("unreachable")); + this.setUnavailable(error.message); setTimeout(() => { this.getAirFreshStatus(); }, 1000 * interval); @@ -74,7 +74,7 @@ class MiAirQualityMonitor2Gen extends Homey.Device { .then(() => { this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); } @@ -92,7 +92,7 @@ class MiAirQualityMonitor2Gen extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/cgllc_airmonitor_s1/device.js b/drivers/cgllc_airmonitor_s1/device.js index 2b23f1e..5fa550f 100644 --- a/drivers/cgllc_airmonitor_s1/device.js +++ b/drivers/cgllc_airmonitor_s1/device.js @@ -5,15 +5,14 @@ class MiClearGlassAirDetector extends Homey.Device { async onInit() { this.driver = this.getDriver(); this.data = this.getData(); - this.getAirFreshStatus(); + this.getAirMonitorStatus(); this.log("MiJia device init | name: " + this.getName() + " - class: " + this.getClass() + " - data: " + JSON.stringify(this.data)); } - getAirFreshStatus() { - var that = this; + getAirMonitorStatus() { miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { + .then((device) => { if (!this.getAvailable()) { this.setAvailable(); } @@ -22,50 +21,52 @@ class MiClearGlassAirDetector extends Homey.Device { this.device .call("get_prop", ["battery", "battery_state", "co2", "humidity", "pm25", "temperature", "tvoc"]) - .then(result => { - that.updateCapabilityValue("measure_pm25", parseInt(result.pm25)); - that.updateCapabilityValue("measure_co2", parseInt(result.co2)); - that.updateCapabilityValue("measure_humidity", parseFloat(result.humidity)); - that.updateCapabilityValue("measure_temperature", parseFloat(result.temperature)); - that.updateCapabilityValue("measure_voc", parseInt(result.tvoc)); - that.updateCapabilityValue("battery", parseInt(result.battery)); - that.updateCapabilityValue("battery_state", parseInt(result.battery) > 20 ? false : true); + .then((result) => { + this.updateCapabilityValue("measure_pm25", parseInt(result.pm25)); + this.updateCapabilityValue("measure_co2", parseInt(result.co2)); + this.updateCapabilityValue("measure_humidity", parseFloat(result.humidity)); + this.updateCapabilityValue("measure_temperature", parseFloat(result.temperature)); + this.updateCapabilityValue("measure_voc", parseInt(result.tvoc)); + this.updateCapabilityValue("measure_battery", parseInt(result.battery)); + this.updateCapabilityValue("alarm_battery", parseInt(result.battery) > 20 ? false : true); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - this.setUnavailable(Homey.__("reconnecting")); + .catch((error) => { + clearInterval(this.updateInterval); + this.setUnavailable(error.message); setTimeout(() => { - this.getAirFreshStatus(); + this.getAirMonitorStatus(); }, 10000); }); } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["battery", "battery_state", "co2", "humidity", "pm25", "temperature", "tvoc"]) - .then(result => { - that.updateCapabilityValue("measure_pm25", parseInt(result.pm25)); - that.updateCapabilityValue("measure_co2", parseInt(result.co2)); - that.updateCapabilityValue("measure_humidity", parseFloat(result.humidity)); - that.updateCapabilityValue("measure_temperature", parseFloat(result.temperature)); - that.updateCapabilityValue("measure_voc", parseInt(result.tvoc)); - that.updateCapabilityValue("battery", parseInt(result.battery)); - that.updateCapabilityValue("battery_state", parseInt(result.battery) > 20 ? false : true); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("measure_pm25", parseInt(result.pm25)); + this.updateCapabilityValue("measure_co2", parseInt(result.co2)); + this.updateCapabilityValue("measure_humidity", parseFloat(result.humidity)); + this.updateCapabilityValue("measure_temperature", parseFloat(result.temperature)); + this.updateCapabilityValue("measure_voc", parseInt(result.tvoc)); + this.updateCapabilityValue("measure_battery", parseInt(result.battery)); + this.updateCapabilityValue("alarm_battery", parseInt(result.battery) > 20 ? false : true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'get_prop' error: ", error); clearInterval(this.updateInterval); - this.setUnavailable(Homey.__("unreachable")); + this.setUnavailable(error.message); setTimeout(() => { - this.getAirFreshStatus(); + this.getAirMonitorStatus(); }, 1000 * interval); }); }, 1000 * interval); @@ -77,7 +78,7 @@ class MiClearGlassAirDetector extends Homey.Device { .then(() => { this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); } @@ -85,7 +86,7 @@ class MiClearGlassAirDetector extends Homey.Device { onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { - this.getAirFreshStatus(); + this.getAirMonitorStatus(); callback(null, true); } } @@ -95,7 +96,7 @@ class MiClearGlassAirDetector extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/chuangmi_plug_m1/device.js b/drivers/chuangmi_plug_m1/device.js index c83e055..59f3195 100644 --- a/drivers/chuangmi_plug_m1/device.js +++ b/drivers/chuangmi_plug_m1/device.js @@ -22,34 +22,29 @@ class MiSmartPlugWiFi extends Homey.Device { } getXiaomiStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_prop", ["power", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_temperature", result[1]); - that.setCapabilityValue("onoff.led", result[2] === "on" ? true : false); + .then((result) => { + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_temperature", result[1]); + this.updateCapabilityValue("onoff.led", result[2] === "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getXiaomiStatus(); }, 10000); @@ -58,19 +53,40 @@ class MiSmartPlugWiFi extends Homey.Device { updateTimer(interval) { clearInterval(this.updateInterval); - var that = this; this.updateInterval = setInterval(() => { this.device .call("get_prop", ["power", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_temperature", result[1]); - that.setCapabilityValue("onoff.led", result[2] === "on" ? true : false); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_temperature", result[1]); + this.updateCapabilityValue("onoff.led", result[2] === "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); + clearInterval(this.updateInterval); + setTimeout(() => { + this.getXiaomiStatus(); + }, 1000 * interval); + }); }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getXiaomiStatus(); @@ -79,20 +95,20 @@ class MiSmartPlugWiFi extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_power' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_power' error: ", error)); }); } registerLedOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_wifi_led", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_wifi_led' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_wifi_led' error: ", error)); }); } @@ -101,7 +117,7 @@ class MiSmartPlugWiFi extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/chuangmi_plug_v1/device.js b/drivers/chuangmi_plug_v1/device.js index ed975f3..11bc7d1 100644 --- a/drivers/chuangmi_plug_v1/device.js +++ b/drivers/chuangmi_plug_v1/device.js @@ -23,35 +23,30 @@ class MiSmartPlugWiFiWithUSB extends Homey.Device { } getXiaomiStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_prop", ["power", "usb_on", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0]); - that.setCapabilityValue("onoff.usb", result[1]); - that.setCapabilityValue("measure_temperature", result[2]); - that.setCapabilityValue("onoff.led", result[3] === "on" ? true : false); + .then((result) => { + this.updateCapabilityValue("onoff", result[0]); + this.updateCapabilityValue("onoff.usb", result[1]); + this.updateCapabilityValue("measure_temperature", result[2]); + this.updateCapabilityValue("onoff.led", result[3] === "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - let update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getXiaomiStatus(); }, 10000); @@ -59,21 +54,42 @@ class MiSmartPlugWiFiWithUSB extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["power", "usb_on", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0]); - that.setCapabilityValue("onoff.usb", result[1]); - that.setCapabilityValue("measure_temperature", result[2]); - that.setCapabilityValue("onoff.led", result[3] === "on" ? true : false); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0]); + this.updateCapabilityValue("onoff.usb", result[1]); + this.updateCapabilityValue("measure_temperature", result[2]); + this.updateCapabilityValue("onoff.led", result[3] === "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); + clearInterval(this.updateInterval); + setTimeout(() => { + this.getXiaomiStatus(); + }, 1000 * interval); + }); }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getXiaomiStatus(); @@ -82,29 +98,29 @@ class MiSmartPlugWiFiWithUSB extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call(value ? "set_on" : "set_off", []) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_on' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_on' error: ", error)); }); } registerUSBOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call(value ? "set_usb_on" : "set_usb_off", []) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_usb_on' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_usb_on' error: ", error)); }); } registerLedOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_wifi_led", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_wifi_led' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_wifi_led' error: ", error)); }); } @@ -113,7 +129,7 @@ class MiSmartPlugWiFiWithUSB extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/chuangmi_plug_v3/device.js b/drivers/chuangmi_plug_v3/device.js index 61697f7..2eabef5 100644 --- a/drivers/chuangmi_plug_v3/device.js +++ b/drivers/chuangmi_plug_v3/device.js @@ -30,10 +30,9 @@ class MiSmartPlugWiFiWith2USB extends Homey.Device { } getXiaomiStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { + .then((device) => { if (!this.getAvailable()) { this.setAvailable(); } @@ -42,26 +41,20 @@ class MiSmartPlugWiFiWith2USB extends Homey.Device { this.device .call("get_prop", ["power", "usb_on", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] == "on" ? true : false); - that.setCapabilityValue("onoff.usb", result[1]); - that.setCapabilityValue("measure_temperature", result[2]); - that.setCapabilityValue("onoff.led", result[3] == "on" ? true : false); + .then((result) => { + this.updateCapabilityValue("onoff", result[0] == "on" ? true : false); + this.updateCapabilityValue("onoff.usb", result[1]); + this.updateCapabilityValue("measure_temperature", result[2]); + this.updateCapabilityValue("onoff.led", result[3] == "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - let update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getXiaomiStatus(); }, 10000); @@ -69,21 +62,42 @@ class MiSmartPlugWiFiWith2USB extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["power", "usb_on", "temperature", "wifi_led"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] == "on" ? true : false); - that.setCapabilityValue("onoff.usb", result[1]); - that.setCapabilityValue("measure_temperature", result[2]); - that.setCapabilityValue("onoff.led", result[3] == "on" ? true : false); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0] == "on" ? true : false); + this.updateCapabilityValue("onoff.usb", result[1]); + this.updateCapabilityValue("measure_temperature", result[2]); + this.updateCapabilityValue("onoff.led", result[3] == "on" ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); + clearInterval(this.updateInterval); + setTimeout(() => { + this.getXiaomiStatus(); + }, 1000 * interval); + }); }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getXiaomiStatus(); @@ -92,47 +106,47 @@ class MiSmartPlugWiFiWith2USB extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_power' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_power' error: ", error)); }); } registerUSBOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call(value ? "set_usb_on" : "set_usb_off", []) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_usb_on' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_usb_on' error: ", error)); }); } registerLedOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_wifi_led", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_wifi_led' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_wifi_led' error: ", error)); }); } registerUSBOnAction(name, action) { action.registerRunListener(async (args, state) => { this.device - .call("set_usb_on", []) - .then(() => this.log("Sending " + name + " commmand: set_usb_on")) - .catch(error => this.log("Sending commmand 'set_usb_on' error: ", error)); + .call("set_usb_on", []) + .then(() => this.log("Sending " + name + " commmand: set_usb_on")) + .catch((error) => this.log("Sending commmand 'set_usb_on' error: ", error)); }); } registerUSBOffAction(name, action) { action.registerRunListener(async (args, state) => { this.device - .call("set_usb_off", []) - .then(() => this.log("Sending " + name + " commmand: set_usb_off")) - .catch(error => this.log("Sending commmand 'set_usb_off' error: ", error)); + .call("set_usb_off", []) + .then(() => this.log("Sending " + name + " commmand: set_usb_off")) + .catch((error) => this.log("Sending commmand 'set_usb_off' error: ", error)); }); } @@ -141,7 +155,7 @@ class MiSmartPlugWiFiWith2USB extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/ctrl_86plug.aq1/device.js b/drivers/ctrl_86plug.aq1/device.js index a8176e1..dcbc45c 100644 --- a/drivers/ctrl_86plug.aq1/device.js +++ b/drivers/ctrl_86plug.aq1/device.js @@ -57,10 +57,10 @@ class Ctrl86PlugAq1 extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -69,7 +69,7 @@ class Ctrl86PlugAq1 extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -95,7 +95,7 @@ class Ctrl86PlugAq1 extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -104,7 +104,7 @@ class Ctrl86PlugAq1 extends Homey.Device { registerToggle(name, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { status: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -151,7 +151,7 @@ class Ctrl86PlugAq1 extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/ctrl_ln1.aq1/device.js b/drivers/ctrl_ln1.aq1/device.js index 9e800b9..cda1889 100644 --- a/drivers/ctrl_ln1.aq1/device.js +++ b/drivers/ctrl_ln1.aq1/device.js @@ -49,10 +49,10 @@ class SingleSwitchLN extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -61,7 +61,7 @@ class SingleSwitchLN extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -87,7 +87,7 @@ class SingleSwitchLN extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -96,7 +96,7 @@ class SingleSwitchLN extends Homey.Device { registerToggle(name, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { channel_0: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -142,7 +142,7 @@ class SingleSwitchLN extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/ctrl_ln2.aq1/device.js b/drivers/ctrl_ln2.aq1/device.js index 507f2aa..02d6b59 100644 --- a/drivers/ctrl_ln2.aq1/device.js +++ b/drivers/ctrl_ln2.aq1/device.js @@ -60,10 +60,10 @@ class DoubleSwitchLN extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -72,7 +72,7 @@ class DoubleSwitchLN extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -98,7 +98,7 @@ class DoubleSwitchLN extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -107,7 +107,7 @@ class DoubleSwitchLN extends Homey.Device { registerToggle(name, channel, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { [channel]: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -156,7 +156,7 @@ class DoubleSwitchLN extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/ctrl_neutral1/device.js b/drivers/ctrl_neutral1/device.js index 6141ac2..586a312 100644 --- a/drivers/ctrl_neutral1/device.js +++ b/drivers/ctrl_neutral1/device.js @@ -49,10 +49,10 @@ class SingleSwitch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -61,7 +61,7 @@ class SingleSwitch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -87,7 +87,7 @@ class SingleSwitch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -96,7 +96,7 @@ class SingleSwitch extends Homey.Device { registerToggle(name, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { channel_0: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -142,7 +142,7 @@ class SingleSwitch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/ctrl_neutral2/device.js b/drivers/ctrl_neutral2/device.js index d4a6668..f6b2524 100644 --- a/drivers/ctrl_neutral2/device.js +++ b/drivers/ctrl_neutral2/device.js @@ -60,10 +60,10 @@ class DoubleSwitch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -72,7 +72,7 @@ class DoubleSwitch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -98,7 +98,7 @@ class DoubleSwitch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -107,7 +107,7 @@ class DoubleSwitch extends Homey.Device { registerToggle(name, channel, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { [channel]: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -171,7 +171,7 @@ class DoubleSwitch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/cube/device.js b/drivers/cube/device.js index 1f1051d..ea44222 100644 --- a/drivers/cube/device.js +++ b/drivers/cube/device.js @@ -64,17 +64,17 @@ class DoubleButton86Switch extends Homey.Device { if (device["data"]["rotate"]) { let tokens = { - cube_rotated: parseInt(device["data"]["rotate"]) + cube_rotated: parseInt(device["data"]["rotate"]), }; this.triggerFlow(triggers.cubeRotated, "cubeRotated", tokens); } let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -83,7 +83,7 @@ class DoubleButton86Switch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi.sensor_" + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -109,7 +109,7 @@ class DoubleButton86Switch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -139,7 +139,7 @@ class DoubleButton86Switch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/curtain.aq2/device.js b/drivers/curtain.aq2/device.js index bc5b9d0..fa12c66 100644 --- a/drivers/curtain.aq2/device.js +++ b/drivers/curtain.aq2/device.js @@ -61,10 +61,10 @@ class CurtainAQ2 extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -73,7 +73,7 @@ class CurtainAQ2 extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -99,7 +99,7 @@ class CurtainAQ2 extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "]" + " [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "]" + " [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -108,7 +108,7 @@ class CurtainAQ2 extends Homey.Device { registerToggle(name, valueOn = true, valueOff = false, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const newValue = value ? valueOn : valueOff; const data = { curtain_status: newValue }; await Homey.app.mihub.sendWrite(sid, data); @@ -118,7 +118,7 @@ class CurtainAQ2 extends Homey.Device { registerDim(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const level = Math.round(value * 100); const data = { curtain_level: level.toString() }; await Homey.app.mihub.sendWrite(sid, data); @@ -127,7 +127,7 @@ class CurtainAQ2 extends Homey.Device { registerCovering(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const states = { up: "open", idle: "stop", down: "close" }; const data = { curtain_status: states[value] }; await Homey.app.mihub.sendWrite(sid, data); @@ -171,7 +171,7 @@ class CurtainAQ2 extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/curtain/device.js b/drivers/curtain/device.js index 5e3fff5..bbf2dc7 100644 --- a/drivers/curtain/device.js +++ b/drivers/curtain/device.js @@ -61,10 +61,10 @@ class Curtain extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -73,7 +73,7 @@ class Curtain extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -99,7 +99,7 @@ class Curtain extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -108,7 +108,7 @@ class Curtain extends Homey.Device { registerToggle(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const settings = this.getSettings(); if (value) { const data = { curtain_status: settings.reverted ? "open" : "close" }; @@ -122,7 +122,7 @@ class Curtain extends Homey.Device { registerDim(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const level = Math.round(value * 100); const data = { curtain_level: level.toString() }; await Homey.app.mihub.sendWrite(sid, data); @@ -131,7 +131,7 @@ class Curtain extends Homey.Device { registerCovering(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const settings = this.getSettings(); const states = { up: "open", idle: "stop", down: "close" }; @@ -186,7 +186,7 @@ class Curtain extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/db86_switch/device.js b/drivers/db86_switch/device.js index 9157ff3..3f183a8 100644 --- a/drivers/db86_switch/device.js +++ b/drivers/db86_switch/device.js @@ -40,10 +40,10 @@ class DoubleButton86Switch extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -52,7 +52,7 @@ class DoubleButton86Switch extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi.sensor_" + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -78,7 +78,7 @@ class DoubleButton86Switch extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -110,7 +110,7 @@ class DoubleButton86Switch extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/db86_switch_advanced/device.js b/drivers/db86_switch_advanced/device.js index 3964824..7e9a71a 100644 --- a/drivers/db86_switch_advanced/device.js +++ b/drivers/db86_switch_advanced/device.js @@ -57,10 +57,10 @@ class DoubleButton86SwitchAdvanced extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -69,7 +69,7 @@ class DoubleButton86SwitchAdvanced extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -95,7 +95,7 @@ class DoubleButton86SwitchAdvanced extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -130,7 +130,7 @@ class DoubleButton86SwitchAdvanced extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/deerma_humidifier_mjjsq/device.js b/drivers/deerma_humidifier_mjjsq/device.js index 0cba59c..4029f7f 100644 --- a/drivers/deerma_humidifier_mjjsq/device.js +++ b/drivers/deerma_humidifier_mjjsq/device.js @@ -5,14 +5,14 @@ const modes = { 1: "low", 2: "medium", 3: "high", - 4: "humidty" + 4: "humidity", }; const modesID = { low: 1, medium: 2, high: 3, - humidty: 4 + humidity: 4, }; class MiSmartHumidifier extends Homey.Device { @@ -45,40 +45,35 @@ class MiSmartHumidifier extends Homey.Device { } getHumidifierStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_prop", ["Humidifier_Gear", "Humidity_Value", "HumiSet_Value", "Led_State", "OnOff_State", "TemperatureValue", "TipSound_State", "waterstatus", "watertankstatus"]) - .then(result => { - that.setCapabilityValue("humidifier_deerma_mode", modes[result[0]]); - that.setCapabilityValue("measure_humidity", parseInt(result[1])); - that.setCapabilityValue("dim", parseInt(result[2] / 100)); - that.setSettings({ led: result[3] === 1 ? true : false }); - that.setCapabilityValue("onoff", result[4] === 1 ? true : false); - that.setCapabilityValue("measure_temperature", parseInt(result[5])); - that.setSettings({ buzzer: result[6] === 1 ? true : false }); - that.setCapabilityValue("alarm_water", result[7] === 0 ? true : false); - that.setCapabilityValue("alarm_motion.tank", result[8] === 0 ? true : false); + .then((result) => { + this.updateCapabilityValue("humidifier_deerma_mode", modes[result[0]]); + this.updateCapabilityValue("measure_humidity", parseInt(result[1])); + this.updateCapabilityValue("dim", parseInt(result[2]) / 100); + this.setSettings({ led: result[3] === 1 ? true : false }); + this.updateCapabilityValue("onoff", result[4] === 1 ? true : false); + this.updateCapabilityValue("measure_temperature", parseInt(result[5])); + this.setSettings({ buzzer: result[6] === 1 ? true : false }); + this.updateCapabilityValue("alarm_water", result[7] === 0 ? true : false); + this.updateCapabilityValue("alarm_motion.tank", result[8] === 0 ? true : false); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getHumidifierStatus(); }, 10000); @@ -86,32 +81,28 @@ class MiSmartHumidifier extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["Humidifier_Gear", "Humidity_Value", "HumiSet_Value", "Led_State", "OnOff_State", "TemperatureValue", "TipSound_State", "waterstatus", "watertankstatus"]) - .then(result => { - that.setCapabilityValue("humidifier_deerma_mode", modes[result[0]]); - that.setCapabilityValue("measure_humidity", parseInt(result[1])); - that.setCapabilityValue("dim", parseInt(result[2] / 100)); - that.setSettings({ led: result[3] === 1 ? true : false }); - that.setCapabilityValue("onoff", result[4] === 1 ? true : false); - that.setCapabilityValue("measure_temperature", parseInt(result[5])); - that.setSettings({ buzzer: result[6] === 1 ? true : false }); - that.setCapabilityValue("alarm_water", result[7] === 0 ? true : false); - that.setCapabilityValue("alarm_motion.tank", result[8] === 0 ? true : false); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("humidifier_deerma_mode", modes[result[0]]); + this.updateCapabilityValue("measure_humidity", parseInt(result[1])); + this.updateCapabilityValue("dim", parseInt(result[2]) / 100); + this.setSettings({ led: result[3] === 1 ? true : false }); + this.updateCapabilityValue("onoff", result[4] === 1 ? true : false); + this.updateCapabilityValue("measure_temperature", parseInt(result[5])); + this.setSettings({ buzzer: result[6] === 1 ? true : false }); + this.updateCapabilityValue("alarm_water", result[7] === 0 ? true : false); + this.updateCapabilityValue("alarm_motion.tank", result[8] === 0 ? true : false); }) - .catch(error => { - this.log("Sending commmand error: ", error); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } setTimeout(() => { this.getHumidifierStatus(); }, 1000 * interval); @@ -119,8 +110,20 @@ class MiSmartHumidifier extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(name, value) { + if (this.getCapabilityValue(name) != value) { + this.setCapabilityValue(name, value) + .then(() => { + this.log("[" + this.getName() + "] [" + name + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.getName() + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { - if (changedKeys.includes("updateTimer") || changedKeys.includes("gatewayIP") || changedKeys.includes("gatewayToken")) { + if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getHumidifierStatus(); callback(null, true); } @@ -129,10 +132,10 @@ class MiSmartHumidifier extends Homey.Device { this.device .call("SetLedState", [newSettings.led ? 1 : 0]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.led); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'SetLedState' error: ", error); callback(error, false); }); @@ -142,10 +145,10 @@ class MiSmartHumidifier extends Homey.Device { this.device .call("SetTipSound_Status", [newSettings.buzzer ? 1 : 0]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.buzzer); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'SetTipSound_Status' error: ", error); callback(error, false); }); @@ -153,71 +156,68 @@ class MiSmartHumidifier extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("Set_OnOff", [value ? 1 : 0]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'Set_OnOff' error: ", error)); + .catch((error) => this.log("Sending commmand 'Set_OnOff' error: ", error)); }); } registerDryOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_dry", [value ? 1 : 0]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_dry' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_dry' error: ", error)); }); } registerTargetRelativeHumidity(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let humidity = value * 100; if (humidity > 0) { this.device .call("Set_HumiValue", [humidity]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'Set_HumiValue' error: ", error)); + .catch((error) => this.log("Sending commmand 'Set_HumiValue' error: ", error)); } }); } registerHumidifierMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("Set_HumidifierGears", [modesID[value]]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'Set_HumidifierGears' error: ", error)); + .catch((error) => this.log("Sending commmand 'Set_HumidifierGears' error: ", error)); }); } registerHumidifierOnAction(name, action) { - var that = this; action.registerRunListener(async (args, state) => { - that.device + this.device .call("Set_OnOff", [1]) - .then(() => that.log("Set 'Set_OnOff': ", args)) - .catch(error => that.log("Set 'Set_OnOff' error: ", error)); + .then(() => this.log("Set 'Set_OnOff': ", args)) + .catch((error) => this.log("Set 'Set_OnOff' error: ", error)); }); } registerHumidifierOffAction(name, action) { - var that = this; action.registerRunListener(async (args, state) => { - that.device + this.device .call("Set_OnOff", [0]) - .then(() => that.log("Set 'Set_OnOff': ", args)) - .catch(error => that.log("Set 'Set_OnOff' error: ", error)); + .then(() => this.log("Set 'Set_OnOff': ", args)) + .catch((error) => this.log("Set 'Set_OnOff' error: ", error)); }); } registerHumidifierModeAction(name, action) { - var that = this; action.registerRunListener(async (args, state) => { - that.device + this.device .call("Set_HumidifierGears", [modesID[args.modes]]) - .then(() => that.log("Set 'Set_HumidifierGears': ", args.modes)) - .catch(error => that.log("Set 'Set_HumidifierGears' error: ", error)); + .then(() => this.log("Set 'Set_HumidifierGears': ", args.modes)) + .catch((error) => this.log("Set 'Set_HumidifierGears' error: ", error)); }); } @@ -226,7 +226,7 @@ class MiSmartHumidifier extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/dmaker_airfresh_t2017/device.js b/drivers/dmaker_airfresh_t2017/device.js index 6ca3d10..013fb5e 100644 --- a/drivers/dmaker_airfresh_t2017/device.js +++ b/drivers/dmaker_airfresh_t2017/device.js @@ -34,11 +34,12 @@ class MiAirPurifierMJXFJ extends Homey.Device { } getPurifierStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device @@ -58,44 +59,38 @@ class MiAirPurifierMJXFJ extends Homey.Device { "screen_direction", "ptc_level", "ptc_on", - "ptc_status" + "ptc_status", ]) - .then(result => { - that.setCapabilityValue("onoff", result[0]); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_co2", parseInt(result[2])); - that.setCapabilityValue("measure_temperature", parseInt(result[3])); - that.setCapabilityValue("air_purifier_t2017_mode", result[4]); - that.setCapabilityValue("air_heater_mode", result[5]); - that.setCapabilityValue("dim", parseInt(that.normalize[result[6]], 60, 300)); - that.setSettings({ display: result[7] }); - that.setSettings({ sound: result[8] }); - that.setSettings({ childLock: result[9] }); - that.setSettings({ filter_efficient: parseInt(result[10]).toString() + "%" }); - that.setSettings({ filter_intermediate: parseInt(result[11]).toString() + "%" }); - that.setSettings({ screen_direction: result[12] }); + .then((result) => { + this.updateCapabilityValue("onoff", result[0]); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_co2", parseInt(result[2])); + this.updateCapabilityValue("measure_temperature", parseInt(result[3])); + this.updateCapabilityValue("air_purifier_t2017_mode", result[4]); + this.updateCapabilityValue("air_heater_mode", result[5]); + this.updateCapabilityValue("dim", parseInt(this.normalize[result[6]], 60, 300)); + this.setSettings({ display: result[7] }); + this.setSettings({ sound: result[8] }); + this.setSettings({ childLock: result[9] }); + this.setSettings({ filter_efficient: parseInt(result[10]).toString() + "%" }); + this.setSettings({ filter_intermediate: parseInt(result[11]).toString() + "%" }); + this.setSettings({ screen_direction: result[12] }); if (result[13] && result[14]) { - that.setCapabilityValue("air_heater_mode", result[13]); - } else that.setCapabilityValue("air_heater_mode", "off"); - that.setCapabilityValue("onoff.ptc", result[14]); + this.updateCapabilityValue("air_heater_mode", result[13]); + } else this.updateCapabilityValue("air_heater_mode", "off"); + this.updateCapabilityValue("onoff.ptc", result[14]); }) - .catch(error => { - that.log("Sending commmand 'get_prop' error: ", error); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); }); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getPurifierStatus(); }, 10000); @@ -103,7 +98,6 @@ class MiAirPurifierMJXFJ extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device @@ -123,38 +117,35 @@ class MiAirPurifierMJXFJ extends Homey.Device { "screen_direction", "ptc_level", "ptc_on", - "ptc_status" + "ptc_status", ]) - .then(result => { - that.setCapabilityValue("onoff", result[0]); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_co2", parseInt(result[2])); - that.setCapabilityValue("measure_temperature", parseInt(result[3])); - that.setCapabilityValue("air_purifier_t2017_mode", result[4]); - that.setCapabilityValue("air_heater_mode", result[5]); - that.setCapabilityValue("dim", parseInt(that.normalize[result[6]], 60, 300)); - that.setSettings({ display: result[7] }); - that.setSettings({ sound: result[8] }); - that.setSettings({ childLock: result[9] }); - that.setSettings({ filter_efficient: parseInt(result[10]).toString() + "%" }); - that.setSettings({ filter_intermediate: parseInt(result[11]).toString() + "%" }); - that.setSettings({ screen_direction: result[12] }); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0]); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_co2", parseInt(result[2])); + this.updateCapabilityValue("measure_temperature", parseInt(result[3])); + this.updateCapabilityValue("air_purifier_t2017_mode", result[4]); + this.updateCapabilityValue("air_heater_mode", result[5]); + this.updateCapabilityValue("dim", parseInt(this.normalize[result[6]], 60, 300)); + this.setSettings({ display: result[7] }); + this.setSettings({ sound: result[8] }); + this.setSettings({ childLock: result[9] }); + this.setSettings({ filter_efficient: parseInt(result[10]).toString() + "%" }); + this.setSettings({ filter_intermediate: parseInt(result[11]).toString() + "%" }); + this.setSettings({ screen_direction: result[12] }); if (result[13] && result[14]) { - that.setCapabilityValue("air_heater_mode", result[13]); - } else that.setCapabilityValue("air_heater_mode", "off"); - that.setCapabilityValue("onoff.ptc", result[14]); + this.updateCapabilityValue("air_heater_mode", result[13]); + } else this.updateCapabilityValue("air_heater_mode", "off"); + this.updateCapabilityValue("onoff.ptc", result[14]); }) - .catch(error => { - this.log("Sending commmand error: ", error); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } setTimeout(() => { this.getPurifierStatus(); }, 1000 * interval); @@ -162,6 +153,18 @@ class MiAirPurifierMJXFJ extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + normalize(value, min, max) { let normalized = (value - min) / (max - min); return Number(normalized.toFixed(2)); @@ -177,11 +180,11 @@ class MiAirPurifierMJXFJ extends Homey.Device { this.device .call("set_display", [newSettings.display ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + newSettings.display ? "on" : "off"); + this.log("Sending " + this.getName() + " commmand: " + newSettings.display); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_sound' " + newSettings.display ? "on" : "off error: ", error); + .catch((error) => { + this.log("Sending commmand 'set_sound' " + newSettings.display + " error: ", error); callback(error, false); }); } @@ -190,11 +193,11 @@ class MiAirPurifierMJXFJ extends Homey.Device { this.device .call("set_sound", [newSettings.sound ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + newSettings.sound ? "on" : "off"); + this.log("Sending " + this.getName() + " commmand: " + newSettings.sound); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_sound' " + newSettings.sound ? "on" : "off error: ", error); + .catch((error) => { + this.log("Sending commmand 'set_sound' " + newSettings.sound + " error: ", error); callback(error, false); }); } @@ -203,11 +206,11 @@ class MiAirPurifierMJXFJ extends Homey.Device { this.device .call("set_child_lock", [newSettings.childLock ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + newSettings.childLock ? "on" : "off"); + this.log("Sending " + this.getName() + " commmand: " + newSettings.childLock); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_sound' " + newSettings.childLock ? "on" : "off error: ", error); + .catch((error) => { + this.log("Sending commmand 'set_sound' " + newSettings.childLock + " error: ", error); callback(error, false); }); } @@ -219,7 +222,7 @@ class MiAirPurifierMJXFJ extends Homey.Device { this.log("Sending commmand: " + newSettings.screen_direction); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_screen_direction' " + newSettings.screen_direction + " error: ", error); callback(error, false); }); @@ -227,14 +230,14 @@ class MiAirPurifierMJXFJ extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_power' " + value + " error: " + error); callback(error, false); }); @@ -242,14 +245,14 @@ class MiAirPurifierMJXFJ extends Homey.Device { } registerPTCOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_ptc_on", [value]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_ptc_on' " + value + " error: " + error); callback(error, false); }); @@ -257,7 +260,7 @@ class MiAirPurifierMJXFJ extends Homey.Device { } registerFavoriteLevel(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let speed = value * 100; if (speed > 0) { this.device @@ -266,7 +269,7 @@ class MiAirPurifierMJXFJ extends Homey.Device { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_favourite_speed' " + value + " error: " + error); callback(error, false); }); @@ -275,14 +278,14 @@ class MiAirPurifierMJXFJ extends Homey.Device { } registerAirPurifierMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_mode", [value]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_mode' " + value + " error: " + error); callback(error, false); }); @@ -290,14 +293,14 @@ class MiAirPurifierMJXFJ extends Homey.Device { } registerAirPurifierHeatingMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_ptc_level", [value]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_ptc_level' " + value + " error: " + error); callback(error, false); }); @@ -311,21 +314,21 @@ class MiAirPurifierMJXFJ extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["on"]) .then(() => { that.log("Set 'set_power' ON"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -341,21 +344,21 @@ class MiAirPurifierMJXFJ extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["off"]) .then(() => { that.log("Set 'set_power' OFF"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -371,21 +374,21 @@ class MiAirPurifierMJXFJ extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_mode", [args.modes]) .then(() => { that.log("Set 'set_mode': ", args.modes); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_mode' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -401,21 +404,21 @@ class MiAirPurifierMJXFJ extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_favourite_speed", [parseInt(speed * 2.4 + 60)]) .then(() => { that.log("Set 'set_favourite_speed': ", parseInt(speed * 2.4 + 60)); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_favourite_speed' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -429,7 +432,7 @@ class MiAirPurifierMJXFJ extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); } } diff --git a/drivers/gateway/device.js b/drivers/gateway/device.js index 0505d69..b86e58d 100644 --- a/drivers/gateway/device.js +++ b/drivers/gateway/device.js @@ -77,12 +77,12 @@ class Gateway extends Homey.Device { gatewaySid: this.data.sid, deviceModelName: gateways[sid].model, deviceIp: gateways[sid].ip, - deviceProtoVersion: gateways[sid].proto_version + deviceProtoVersion: gateways[sid].proto_version, }); if (gateways[sid]["model"] == "gateway") { this.setSettings({ - deviceModelCodeName: "DGNWG02LM" + deviceModelCodeName: "DGNWG02LM", }); } } @@ -148,7 +148,7 @@ class Gateway extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -157,7 +157,7 @@ class Gateway extends Homey.Device { registerToggle(name, trigger) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { if (value) { var hue = this.getCapabilityValue("light_hue") * 359; var saturation = this.getCapabilityValue("light_saturation") * 100; @@ -176,7 +176,7 @@ class Gateway extends Homey.Device { registerDim(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { var hue = this.getCapabilityValue("light_hue") * 359; var saturation = this.getCapabilityValue("light_saturation") * 100; var dim = value * 100; @@ -186,7 +186,7 @@ class Gateway extends Homey.Device { registerButton(name) { let sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { const data = { join_permission: "yes" }; await Homey.app.mihub.sendWriteCmd(sid, data); }); @@ -194,7 +194,7 @@ class Gateway extends Homey.Device { registerLight_hueAndSaturation() { let sid = this.data.sid; - this.registerMultipleCapabilityListener(["light_hue", "light_saturation"], async valueObj => { + this.registerMultipleCapabilityListener(["light_hue", "light_saturation"], async (valueObj) => { var hue = valueObj.light_hue * 359; var saturation_value = this.getCapabilityValue("light_saturation"); var saturation = saturation_value * 100; @@ -217,7 +217,7 @@ class Gateway extends Homey.Device { } registerLight_saturation(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.log("Light_saturation: ", value); }); } @@ -268,7 +268,7 @@ class Gateway extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/gateway_radio/device.js b/drivers/gateway_radio/device.js index ae201c6..8632ae4 100644 --- a/drivers/gateway_radio/device.js +++ b/drivers/gateway_radio/device.js @@ -46,13 +46,15 @@ class GatewayRadio extends Homey.Device { var that = this; miio .device({ address: this.getSetting("gatewayIP"), token: this.getSetting("gatewayToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_prop_fm", []) - .then(result => { + .then((result) => { that.setCapabilityValue("volume_set", result.current_volume / 100); if (process.env.HOMEY_VERSION.replace(/\W/g, "") >= "200") { if (result.current_program == "527782008") { @@ -72,38 +74,33 @@ class GatewayRadio extends Homey.Device { that.setCapabilityValue("speaker_playing", false); } }) - .catch(error => that.log("Sending commmand 'get_prop_fm' error: ", error)); + .catch((error) => that.log("Sending commmand 'get_prop_fm' error: ", error)); this.device .call("get_channels", { start: 0 }) - .then(result => { - result.chs.forEach(function(item, i, radios) { + .then((result) => { + result.chs.forEach(function (item, i, radios) { that.setSettings({ - [`favorite${i}ID`]: item.id + ", " + item.url + [`favorite${i}ID`]: item.id + ", " + item.url, }); if (i == radios.length - 1) { i = radios.length; for (let j = i; j < 20; j++) { that.setSettings({ - [`favorite${j}ID`]: "" + [`favorite${j}ID`]: "", }); } } }); }) - .catch(error => this.log("Sending commmand 'get_channels' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_channels' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getRadioStatus(); }, 10000); @@ -116,7 +113,7 @@ class GatewayRadio extends Homey.Device { this.updateInterval = setInterval(() => { this.device .call("get_prop_fm", []) - .then(result => { + .then((result) => { that.setCapabilityValue("volume_set", result.current_volume / 100); if (process.env.HOMEY_VERSION.replace(/\W/g, "") >= "200") { if (result.current_program == "527782008") { @@ -136,16 +133,9 @@ class GatewayRadio extends Homey.Device { that.setCapabilityValue("speaker_playing", false); } }) - .catch(error => { - that.log("Sending commmand 'get_prop_fm' error: ", error); - clearInterval(that.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - that.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - that.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - that.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - that.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { that.getRadioStatus(); }, 1000 * interval); @@ -153,33 +143,27 @@ class GatewayRadio extends Homey.Device { this.device .call("get_channels", { start: 0 }) - .then(result => { - result.chs.forEach(function(item, i, radios) { + .then((result) => { + result.chs.forEach(function (item, i, radios) { that.setSettings({ - [`favorite${i}ID`]: item.id + ", " + item.url + [`favorite${i}ID`]: item.id + ", " + item.url, }); if (i == radios.length - 1) { i = radios.length; for (let j = i; j < 20; j++) { that.setSettings({ - [`favorite${j}ID`]: "" + [`favorite${j}ID`]: "", }); } } }); }) - .catch(error => { - that.log("Sending commmand 'get_channels' error: ", error); - clearInterval(that.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - that.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - that.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - that.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - that.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { - that.getRadioStatus(); + this.getRadioStatus(); }, 1000 * interval); }); }, 1000 * interval); @@ -210,7 +194,7 @@ class GatewayRadio extends Homey.Device { this.device .call("remove_channels", { chs: [{ id: id, type: 0, url: url }] }) .then(() => that.log("Removing ID: " + id + " URL: " + url)) - .catch(error => { + .catch((error) => { that.log("Sending commmand 'remove_channels' error: ", error); callback(error, false); }); @@ -228,7 +212,7 @@ class GatewayRadio extends Homey.Device { this.device .call("add_channels", { chs: [{ id: id, type: 0, url: url }] }) .then(() => that.log("Adding " + id + " URL: " + url)) - .catch(error => { + .catch((error) => { that.log("Sending commmand 'add_channels' error: ", error); callback(error, false); }); @@ -240,39 +224,39 @@ class GatewayRadio extends Homey.Device { } registerSpeakerPlayingButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("play_fm", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'play_fm' error: ", error)); + .catch((error) => this.log("Sending commmand 'play_fm' error: ", error)); }); } registerNextButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("play_fm", ["next"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'play_fm' error: ", error)); + .catch((error) => this.log("Sending commmand 'play_fm' error: ", error)); }); } registerPrevButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("play_fm", ["prev"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'play_fm' error: ", error)); + .catch((error) => this.log("Sending commmand 'play_fm' error: ", error)); }); } registerVolumeLevel(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let volume = parseInt(value * 100); this.device .call("volume_ctrl_fm", [volume.toString()]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'volume_ctrl_fm' error: ", error)); + .catch((error) => this.log("Sending commmand 'volume_ctrl_fm' error: ", error)); }); } @@ -298,7 +282,7 @@ class GatewayRadio extends Homey.Device { .then(() => { that.log("Play radio: ", args.favoriteID); }) - .catch(error => { + .catch((error) => { that.log("Play radio 'play_specify_fm' error: ", error); }); @@ -307,7 +291,7 @@ class GatewayRadio extends Homey.Device { .then(() => { that.log("Set volume: ", volume); }) - .catch(error => { + .catch((error) => { that.log("Set volume 'volume_ctrl_fm' error: ", error); }); } @@ -325,7 +309,7 @@ class GatewayRadio extends Homey.Device { that.log("Play radio: ", args.id); that.log("from url: ", args.url); }) - .catch(error => { + .catch((error) => { that.log("Play radio 'play_specify_fm' error: ", error); }); @@ -334,7 +318,7 @@ class GatewayRadio extends Homey.Device { .then(() => { that.log("Set volume: ", volume); }) - .catch(error => { + .catch((error) => { that.log("Set volume 'volume_ctrl_fm' error: ", error); }); }); @@ -345,7 +329,7 @@ class GatewayRadio extends Homey.Device { this.device .call("play_fm", ["toggle"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'play_fm' error: ", error)); + .catch((error) => this.log("Sending commmand 'play_fm' error: ", error)); }); } @@ -354,7 +338,7 @@ class GatewayRadio extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/gateway_security/device.js b/drivers/gateway_security/device.js index 38481d4..bf1c1e9 100644 --- a/drivers/gateway_security/device.js +++ b/drivers/gateway_security/device.js @@ -22,34 +22,30 @@ class GatewaySecurity extends Homey.Device { getSecurityStatus() { miio .device({ address: this.getSetting("gatewayIP"), token: this.getSetting("gatewayToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_arming", []) - .then(result => { + .then((result) => { if (result[0] == "on") { - this.setCapabilityValue("homealarm_state", "armed"); + this.updateCapabilityValue("homealarm_state", "armed"); } else if (result[0] == "off") { - this.setCapabilityValue("homealarm_state", "disarmed"); + this.updateCapabilityValue("homealarm_state", "disarmed"); } }) - .catch(error => this.log("Sending commmand 'get_arming' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_arming' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getSecurityStatus(); }, 10000); @@ -61,23 +57,20 @@ class GatewaySecurity extends Homey.Device { this.updateInterval = setInterval(() => { this.device .call("get_arming", []) - .then(result => { + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } if (result[0] == "on") { - this.setCapabilityValue("homealarm_state", "armed"); + this.updateCapabilityValue("homealarm_state", "armed"); } else if (result[0] == "off") { - this.setCapabilityValue("homealarm_state", "disarmed"); + this.updateCapabilityValue("homealarm_state", "disarmed"); } }) - .catch(error => { + .catch((error) => { this.log("Sending commmand error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } setTimeout(() => { this.getSecurityStatus(); }, 1000 * interval); @@ -85,6 +78,18 @@ class GatewaySecurity extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("gatewayIP") || changedKeys.includes("gatewayToken")) { this.getSecurityStatus(); @@ -93,7 +98,7 @@ class GatewaySecurity extends Homey.Device { } registerHomeAlarmSecurity(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { var state; if (value == "armed") { state = "on"; @@ -106,7 +111,7 @@ class GatewaySecurity extends Homey.Device { this.device .call("set_arming", [state]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand error: ", error)); + .catch((error) => this.log("Sending commmand error: ", error)); }); } @@ -115,7 +120,7 @@ class GatewaySecurity extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/ir_remote/device.js b/drivers/ir_remote/device.js index 421a630..6f5b0d1 100644 --- a/drivers/ir_remote/device.js +++ b/drivers/ir_remote/device.js @@ -43,7 +43,7 @@ class IRRemote extends Homey.Device { } registerStandbyAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { if (value) { if (this.data && this.data.onoff1) { this.sendIrCode(this.data.onoff1, name); @@ -61,37 +61,37 @@ class IRRemote extends Homey.Device { } registerChannelUpAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.sendIrCode(this.data.channel_up, name); }); } registerChannelDownAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.sendIrCode(this.data.channel_down, name); }); } registerVolumeUpAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.sendIrCode(this.data.volume_up, name); }); } registerVolumeDownAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.sendIrCode(this.data.volume_down, name); }); } registerMuteAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.sendIrCode(this.data.volume_mute, name); }); } registerDimAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { if (this.data && this.data.dim1) { this.sendIrCode(this.data.dim + value, name); } else { @@ -101,7 +101,7 @@ class IRRemote extends Homey.Device { } registerThermostatAction(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.log(value); // this.sendIrCode(this.data.volume_mute, name); }); @@ -111,7 +111,7 @@ class IRRemote extends Homey.Device { action.code.registerRunListener(async (args, state) => { let data = { deviceIp: args.device.getSetting("deviceIP"), - deviceToken: args.device.getSetting("deviceToken") + deviceToken: args.device.getSetting("deviceToken"), }; this.sendIrCode(args.code); }); @@ -122,25 +122,25 @@ class IRRemote extends Homey.Device { miio .device({ address: settings.deviceIp, - token: settings.deviceToken + token: settings.deviceToken, }) - .then(async device => { + .then(async (device) => { for (let i = 0; i < settings.replay; i++) { await this.sleep(500); device .call("miIO.ir_play", { freq: 38400, code: code }) - .then(result => { + .then((result) => { if (!cababilityName) { cababilityName = "custom"; } this.log("Sending " + cababilityName + " ir code: " + code); }) - .catch(error => { + .catch((error) => { this.log("Sending ir code error: ", error); }); } }) - .catch(error => { + .catch((error) => { if (error == "Error: Could not connect to device, handshake timeout") { this.log("Device timeout error: ", error); } else { @@ -150,7 +150,7 @@ class IRRemote extends Homey.Device { } sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise((resolve) => setTimeout(resolve, ms)); } onAdded() { @@ -158,7 +158,7 @@ class IRRemote extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/light_aqcn02/device.js b/drivers/light_aqcn02/device.js index cc5d722..498e787 100644 --- a/drivers/light_aqcn02/device.js +++ b/drivers/light_aqcn02/device.js @@ -26,13 +26,15 @@ class AqaraLightBulb extends Homey.Device { miio .device({ address: this.getSetting("deviceIp"), token: this.getSetting("deviceToken") }) - .then(device => { + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_bright", [], { sid }) - .then(result => { - this.setAvailable(); + .then((result) => { if (result[0] == 0) { this.updateCapabilityValue("onoff", false); } else if (result[0] > 1) { @@ -40,20 +42,14 @@ class AqaraLightBulb extends Homey.Device { } this.updateCapabilityValue("dim", parseFloat(result[0] / 100)); }) - .catch(error => this.log("Sending commmand 'get_bright' error: ", error.message)); + .catch((error) => this.log("Sending commmand 'get_bright' error: ", error.message)); const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } - this.updateInterval && clearInterval(this.updateInterval); + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getLightStatus(); }, 10000); @@ -66,7 +62,10 @@ class AqaraLightBulb extends Homey.Device { this.updateInterval = setInterval(() => { this.device .call("get_bright", [], { sid }) - .then(result => { + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } if (result[0] == 0) { this.updateCapabilityValue("onoff", false); } else if (result[0] > 1) { @@ -74,8 +73,9 @@ class AqaraLightBulb extends Homey.Device { } this.updateCapabilityValue("dim", parseFloat(result[0] / 100)); }) - .catch(error => { - this.updateInterval && clearInterval(this.updateInterval); + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); this.log("Sending commmand 'get_bright' error: ", error.message); }); }, 1000 * interval); @@ -87,7 +87,7 @@ class AqaraLightBulb extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); } @@ -102,33 +102,33 @@ class AqaraLightBulb extends Homey.Device { registerToggle(name) { const sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"], { sid }) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_power' error: ", error.message)); + .catch((error) => this.log("Sending commmand 'set_power' error: ", error.message)); }); } registerDim(name) { const sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_bright", [value * 100], { sid }) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_bright' error: ", error.message)); + .catch((error) => this.log("Sending commmand 'set_bright' error: ", error.message)); }); } registerLightTemperature(name) { const sid = this.data.sid; - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let color_temp = this.denormalize(value, 153, 500); this.device .call("set_ct", [color_temp], { sid }) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_ct' error: ", error.message)); + .catch((error) => this.log("Sending commmand 'set_ct' error: ", error.message)); }); } @@ -147,7 +147,7 @@ class AqaraLightBulb extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/lock.aq1/device.js b/drivers/lock.aq1/device.js index 72435fb..e06ebfd 100644 --- a/drivers/lock.aq1/device.js +++ b/drivers/lock.aq1/device.js @@ -120,7 +120,7 @@ class AqaraLock1 extends Homey.Device { "65632", "65633", "65634", - "65635" + "65635", ]; const codesId = [ "131072", @@ -222,7 +222,7 @@ class AqaraLock1 extends Homey.Device { "131168", "131169", "131170", - "131171" + "131171", ]; const cardsId = [ "196608", @@ -324,7 +324,7 @@ class AqaraLock1 extends Homey.Device { "196704", "196705", "196706", - "196707" + "196707", ]; if (device["data"]["voltage"]) { @@ -338,7 +338,7 @@ class AqaraLock1 extends Homey.Device { if (device["data"]["fing_verified"]) { this.updateCapabilityValue("alarm_motion.finger", true); var that = this; - fingersId.forEach(function(item, i, arr) { + fingersId.forEach(function (item, i, arr) { if (device["data"]["fing_verified"] == item) { for (let u = 0; u < 10; u++) { if (settings[`user${u}FingerID`]) { @@ -353,7 +353,7 @@ class AqaraLock1 extends Homey.Device { code_id: 404, card_id: 404, userName: userUN, - wrong_id: 0 + wrong_id: 0, }; that.triggerFlow(triggers.lockUsed, "lockUsed", tokens); } @@ -379,7 +379,7 @@ class AqaraLock1 extends Homey.Device { this.updateCapabilityValue("alarm_motion.code", true); var that = this; - codesId.forEach(function(item, i, arr) { + codesId.forEach(function (item, i, arr) { if (device["data"]["psw_verified"] == item) { for (let u = 0; u < 10; u++) { if (settings[`user${u}CodeID`]) { @@ -394,7 +394,7 @@ class AqaraLock1 extends Homey.Device { code_id: i, card_id: 404, userName: userUN, - wrong_id: 0 + wrong_id: 0, }; that.triggerFlow(triggers.lockUsed, "lockUsed", tokens); } @@ -419,7 +419,7 @@ class AqaraLock1 extends Homey.Device { if (device["data"]["card_verified"]) { this.updateCapabilityValue("alarm_motion.card", true); var that = this; - cardsId.forEach(function(item, i, arr) { + cardsId.forEach(function (item, i, arr) { if (device["data"]["card_verified"] == item) { for (let u = 0; u < 10; u++) { if (settings[`user${u}CardID`]) { @@ -434,7 +434,7 @@ class AqaraLock1 extends Homey.Device { code_id: 404, card_id: i, userName: userUN, - wrong_id: 0 + wrong_id: 0, }; that.triggerFlow(triggers.lockUsed, "lockUsed", tokens); } @@ -464,7 +464,7 @@ class AqaraLock1 extends Homey.Device { code_id: 404, card_id: 404, userName: "Not user", - wrong_id: parseInt(device["data"]["verified_wrong"]) + wrong_id: parseInt(device["data"]["verified_wrong"]), }; that.triggerFlow(triggers.lockUsed, "lockUsed", tokens); @@ -482,10 +482,10 @@ class AqaraLock1 extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -494,7 +494,7 @@ class AqaraLock1 extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi.sensor_" + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -520,7 +520,7 @@ class AqaraLock1 extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -551,7 +551,7 @@ class AqaraLock1 extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/magnet_sensor.aq2/device.js b/drivers/magnet_sensor.aq2/device.js index 3d2ef2e..98e3502 100644 --- a/drivers/magnet_sensor.aq2/device.js +++ b/drivers/magnet_sensor.aq2/device.js @@ -35,10 +35,10 @@ class MagnetSensor extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -47,7 +47,7 @@ class MagnetSensor extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi." + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -73,7 +73,7 @@ class MagnetSensor extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -99,7 +99,7 @@ class MagnetSensor extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/magnet_sensor/device.js b/drivers/magnet_sensor/device.js index 229a4bc..647f2da 100644 --- a/drivers/magnet_sensor/device.js +++ b/drivers/magnet_sensor/device.js @@ -35,10 +35,10 @@ class MagnetSensor extends Homey.Device { let gateways = Homey.app.mihub.gateways; for (let sid in gateways) { - gateways[sid]["childDevices"].forEach(deviceSid => { + gateways[sid]["childDevices"].forEach((deviceSid) => { if (this.data.sid == deviceSid) { this.setSettings({ - deviceFromGatewaySid: sid + deviceFromGatewaySid: sid, }); } }); @@ -47,7 +47,7 @@ class MagnetSensor extends Homey.Device { this.setSettings({ deviceSid: device.sid, deviceModelName: "lumi.sensor_" + device.model, - deviceModelCodeName: device.modelCode + deviceModelCodeName: device.modelCode, }); } @@ -73,7 +73,7 @@ class MagnetSensor extends Homey.Device { .then(() => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"); }) - .catch(error => { + .catch((error) => { this.log("[" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); }); this.triggerFlow(trigger, name, value); @@ -99,7 +99,7 @@ class MagnetSensor extends Homey.Device { onDeleted() { this.unregisterAuthChangeListener(); this.unregisterStateChangeListener(); - this.log("Device deleted deleted"); + this.log("Device deleted"); } } diff --git a/drivers/mi_air_purifier_ma2/device.js b/drivers/mi_air_purifier_ma2/device.js index f169fd9..e7f0638 100644 --- a/drivers/mi_air_purifier_ma2/device.js +++ b/drivers/mi_air_purifier_ma2/device.js @@ -32,10 +32,9 @@ class MiAirPurifierS2 extends Homey.Device { } getPurifierStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { + .then((device) => { if (!this.getAvailable()) { this.setAvailable(); } @@ -44,28 +43,28 @@ class MiAirPurifierS2 extends Homey.Device { this.device .call("get_prop", ["power", "aqi", "average_aqi", "humidity", "temp_dec", "bright", "mode", "favorite_level", "filter1_life", "use_time", "purify_volume", "led", "buzzer", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_humidity", parseInt(result[3])); - that.setCapabilityValue("measure_temperature", parseInt(result[4] / 10)); - that.setCapabilityValue("measure_luminance", parseInt(result[5])); - that.setCapabilityValue("air_purifier_mode", result[6]); - that.setCapabilityValue("dim", parseInt(that.favoriteLevel[result[7]] / 100)); - that.setSettings({ filter1_life: result[8] + "%" }); - that.setSettings({ purify_volume: result[10] + " m3" }); - that.setSettings({ led: result[11] == "on" ? true : false }); - that.setSettings({ buzzer: result[12] == "on" ? true : false }); - that.setSettings({ childLock: result[13] == "on" ? true : false }); + .then((result) => { + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_humidity", parseInt(result[3])); + this.updateCapabilityValue("measure_temperature", parseInt(result[4] / 10)); + this.updateCapabilityValue("measure_luminance", parseInt(result[5])); + this.updateCapabilityValue("air_purifier_mode", result[6]); + this.updateCapabilityValue("dim", parseInt(this.favoriteLevel[result[7]] / 100)); + this.setSettings({ filter1_life: result[8] + "%" }); + this.setSettings({ purify_volume: result[10] + " m3" }); + this.setSettings({ led: result[11] == "on" ? true : false }); + this.setSettings({ buzzer: result[12] == "on" ? true : false }); + this.setSettings({ childLock: result[13] == "on" ? true : false }); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - this.setUnavailable(Homey.__("reconnecting")); + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getPurifierStatus(); }, 10000); @@ -73,29 +72,31 @@ class MiAirPurifierS2 extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["power", "aqi", "average_aqi", "humidity", "temp_dec", "bright", "mode", "favorite_level", "filter1_life", "use_time", "purify_volume", "led", "buzzer", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_humidity", parseInt(result[3])); - that.setCapabilityValue("measure_temperature", parseInt(result[4] / 10)); - that.setCapabilityValue("measure_luminance", parseInt(result[5])); - that.setCapabilityValue("air_purifier_mode", result[6]); - that.setCapabilityValue("dim", parseInt(that.favoriteLevel[result[7]] / 100)); - that.setSettings({ filter1_life: result[8] + "%" }); - that.setSettings({ purify_volume: result[10] + " m3" }); - that.setSettings({ led: result[11] == "on" ? true : false }); - that.setSettings({ buzzer: result[12] == "on" ? true : false }); - that.setSettings({ childLock: result[13] == "on" ? true : false }); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_humidity", parseInt(result[3])); + this.updateCapabilityValue("measure_temperature", parseInt(result[4] / 10)); + this.updateCapabilityValue("measure_luminance", parseInt(result[5])); + this.updateCapabilityValue("air_purifier_mode", result[6]); + this.updateCapabilityValue("dim", parseInt(this.favoriteLevel[result[7]] / 100)); + this.setSettings({ filter1_life: result[8] + "%" }); + this.setSettings({ purify_volume: result[10] + " m3" }); + this.setSettings({ led: result[11] == "on" ? true : false }); + this.setSettings({ buzzer: result[12] == "on" ? true : false }); + this.setSettings({ childLock: result[13] == "on" ? true : false }); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - this.setUnavailable(Homey.__("unreachable")); setTimeout(() => { this.getPurifierStatus(); }, 1000 * interval); @@ -103,8 +104,20 @@ class MiAirPurifierS2 extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(name, value) { + if (this.getCapabilityValue(name) != value) { + this.setCapabilityValue(name, value) + .then(() => { + this.log("[" + this.data.id + "] [" + name + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { - if (changedKeys.includes("updateTimer") || changedKeys.includes("gatewayIP") || changedKeys.includes("gatewayToken")) { + if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getPurifierStatus(); callback(null, true); } @@ -113,10 +126,10 @@ class MiAirPurifierS2 extends Homey.Device { this.device .call("set_led", [newSettings.led ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.led); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_led' error: ", error); callback(error, false); }); @@ -126,10 +139,10 @@ class MiAirPurifierS2 extends Homey.Device { this.device .call("set_buzzer", [newSettings.buzzer ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.buzzer); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_buzzer' error: ", error); callback(error, false); }); @@ -139,10 +152,10 @@ class MiAirPurifierS2 extends Homey.Device { this.device .call("set_child_lock", [newSettings.childLock ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.childLock); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_child_lock' error: ", error); callback(error, false); }); @@ -150,32 +163,32 @@ class MiAirPurifierS2 extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_power' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_power' error: ", error)); }); } registerFavoriteLevel(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let speed = value * 100; if (speed > 0) { this.device .call("set_level_favorite", [this.getFavoriteLevel(speed)]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_level_favorite' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_level_favorite' error: ", error)); } }); } registerAirPurifierMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_mode", [value]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_mode' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_mode' error: ", error)); }); } @@ -186,21 +199,21 @@ class MiAirPurifierS2 extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["on"]) .then(() => { that.log("Set 'set_power' ON"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -216,21 +229,21 @@ class MiAirPurifierS2 extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["off"]) .then(() => { that.log("Set 'set_power' OFF"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -246,21 +259,21 @@ class MiAirPurifierS2 extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_mode", [args.modes]) .then(() => { that.log("Set 'set_mode': ", args.modes); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_mode' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -276,21 +289,21 @@ class MiAirPurifierS2 extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_level_favorite", [that.getFavoriteLevel(args.range)]) .then(() => { that.log("Set 'set_level_favorite': ", that.getFavoriteLevel(args.range)); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_level_favorite' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -314,7 +327,7 @@ class MiAirPurifierS2 extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); diff --git a/drivers/mi_air_purifier_v6/device.js b/drivers/mi_air_purifier_v6/device.js index 04efb3c..a0abe7e 100644 --- a/drivers/mi_air_purifier_v6/device.js +++ b/drivers/mi_air_purifier_v6/device.js @@ -33,45 +33,40 @@ class MiAirPurifierPro extends Homey.Device { } getPurifierStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device .call("get_prop", ["power", "aqi", "average_aqi", "humidity", "temp_dec", "bright", "mode", "favorite_level", "filter1_life", "use_time", "purify_volume", "led", "volume", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_humidity", parseInt(result[3])); - that.setCapabilityValue("measure_temperature", parseInt(result[4] / 10)); - that.setCapabilityValue("measure_luminance", parseInt(result[5])); - that.setCapabilityValue("air_purifier_mode", result[6]); - that.setCapabilityValue("dim", parseInt(that.favoriteLevel[result[7]] / 100)); - that.setSettings({ filter1_life: result[8] + "%" }); - that.setSettings({ purify_volume: result[10] + " m3" }); - that.setSettings({ led: result[11] == "on" ? true : false }); - that.setSettings({ volume: result[12] >= 1 ? true : false }); - that.setSettings({ childLock: result[13] == "on" ? true : false }); + .then((result) => { + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_humidity", parseInt(result[3])); + this.updateCapabilityValue("measure_temperature", parseInt(result[4] / 10)); + this.updateCapabilityValue("measure_luminance", parseInt(result[5])); + this.updateCapabilityValue("air_purifier_mode", result[6]); + this.updateCapabilityValue("dim", parseInt(this.favoriteLevel[result[7]] / 100)); + this.setSettings({ filter1_life: result[8] + "%" }); + this.setSettings({ purify_volume: result[10] + " m3" }); + this.setSettings({ led: result[11] == "on" ? true : false }); + this.setSettings({ volume: result[12] >= 1 ? true : false }); + this.setSettings({ childLock: result[13] == "on" ? true : false }); }) - .catch(error => { - that.log("Sending commmand 'get_prop' error: ", error); + .catch((error) => { + this.log("Sending commmand 'get_prop' error: ", error); }); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getPurifierStatus(); }, 10000); @@ -79,35 +74,31 @@ class MiAirPurifierPro extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device .call("get_prop", ["power", "aqi", "average_aqi", "humidity", "temp_dec", "bright", "mode", "favorite_level", "filter1_life", "use_time", "purify_volume", "led", "volume", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_pm25", parseInt(result[1])); - that.setCapabilityValue("measure_humidity", parseInt(result[3])); - that.setCapabilityValue("measure_temperature", parseInt(result[4] / 10)); - that.setCapabilityValue("measure_luminance", parseInt(result[5])); - that.setCapabilityValue("air_purifier_mode", result[6]); - that.setCapabilityValue("dim", parseInt(that.favoriteLevel[result[7]] / 100)); - that.setSettings({ filter1_life: result[8] + "%" }); - that.setSettings({ purify_volume: result[10] + " m3" }); - that.setSettings({ led: result[11] == "on" ? true : false }); - that.setSettings({ volume: result[12] >= 1 ? true : false }); - that.setSettings({ childLock: result[13] == "on" ? true : false }); + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.updateCapabilityValue("onoff", result[0] === "on" ? true : false); + this.updateCapabilityValue("measure_pm25", parseInt(result[1])); + this.updateCapabilityValue("measure_humidity", parseInt(result[3])); + this.updateCapabilityValue("measure_temperature", parseInt(result[4] / 10)); + this.updateCapabilityValue("measure_luminance", parseInt(result[5])); + this.updateCapabilityValue("air_purifier_mode", result[6]); + this.updateCapabilityValue("dim", parseInt(this.favoriteLevel[result[7]] / 100)); + this.setSettings({ filter1_life: result[8] + "%" }); + this.setSettings({ purify_volume: result[10] + " m3" }); + this.setSettings({ led: result[11] == "on" ? true : false }); + this.setSettings({ volume: result[12] >= 1 ? true : false }); + this.setSettings({ childLock: result[13] == "on" ? true : false }); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } setTimeout(() => { this.getPurifierStatus(); }, 1000 * interval); @@ -115,6 +106,18 @@ class MiAirPurifierPro extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(name, value) { + if (this.getCapabilityValue(name) != value) { + this.setCapabilityValue(name, value) + .then(() => { + this.log("[" + this.data.id + "] [" + name + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getPurifierStatus(); @@ -125,11 +128,11 @@ class MiAirPurifierPro extends Homey.Device { this.device .call("set_led", [newSettings.led ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.led); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_led' error: ", error); + .catch((error) => { + this.log("Sending commmand 'set_led' " + newSettings.led + " error: ", error); callback(error, false); }); } @@ -138,11 +141,11 @@ class MiAirPurifierPro extends Homey.Device { this.device .call("set_volume", [newSettings.volume ? 100 : 0]) .then(() => { - this.log("Sending commmand: " + newSettings.volume ? 100 : 0); + this.log("Sending " + this.getName() + " commmand: " + newSettings.volume); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_led' " + newSettings.volume ? 100 : 0 + " error: ", error); + .catch((error) => { + this.log("Sending commmand 'newSettings.volume' " + newSettings.volume + " error: ", error); callback(error, false); }); } @@ -151,25 +154,25 @@ class MiAirPurifierPro extends Homey.Device { this.device .call("set_child_lock", [newSettings.childLock ? "on" : "off"]) .then(() => { - this.log("Sending commmand: " + newSettings.childLock ? "on" : "off"); + this.log("Sending " + this.getName() + " commmand: " + newSettings.childLock); callback(null, true); }) - .catch(error => { - this.log("Sending commmand 'set_led' " + newSettings.childLock ? "on" : "off error: ", error); + .catch((error) => { + this.log("Sending commmand 'set_child_lock' " + newSettings.childLock + " error: ", error); callback(error, false); }); } } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_power' " + value + " error: " + error); callback(error, false); }); @@ -177,7 +180,7 @@ class MiAirPurifierPro extends Homey.Device { } registerFavoriteLevel(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let speed = value * 100; if (speed > 0) { this.device @@ -186,7 +189,7 @@ class MiAirPurifierPro extends Homey.Device { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_level_favorite' " + value + " error: " + error); callback(error, false); }); @@ -195,14 +198,14 @@ class MiAirPurifierPro extends Homey.Device { } registerAirPurifierMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_mode", [value]) .then(() => { this.log("Sending " + name + " commmand: " + value); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_mode' " + value + " error: " + error); callback(error, false); }); @@ -216,21 +219,21 @@ class MiAirPurifierPro extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["on"]) .then(() => { that.log("Set 'set_power' ON"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -246,21 +249,21 @@ class MiAirPurifierPro extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_power", ["off"]) .then(() => { that.log("Set 'set_power' OFF"); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_power' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -276,21 +279,21 @@ class MiAirPurifierPro extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_mode", [args.modes]) .then(() => { that.log("Set 'set_mode': ", args.modes); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_mode' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -306,21 +309,21 @@ class MiAirPurifierPro extends Homey.Device { miio .device({ address: args.device.getSetting("deviceIP"), - token: args.device.getSetting("deviceToken") + token: args.device.getSetting("deviceToken"), }) - .then(device => { + .then((device) => { device .call("set_level_favorite", [that.getFavoriteLevel(args.range)]) .then(() => { that.log("Set 'set_level_favorite': ", that.getFavoriteLevel(args.range)); device.destroy(); }) - .catch(error => { + .catch((error) => { that.log("Set 'set_level_favorite' error: ", error); device.destroy(); }); }) - .catch(error => { + .catch((error) => { that.log("miio connect error: " + error); }); } catch (error) { @@ -344,7 +347,7 @@ class MiAirPurifierPro extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); } } diff --git a/drivers/mi_fan_sa1/device.js b/drivers/mi_fan_sa1/device.js index 5c6dd7f..fcf4fad 100644 --- a/drivers/mi_fan_sa1/device.js +++ b/drivers/mi_fan_sa1/device.js @@ -1,7 +1,7 @@ const Homey = require("homey"); const miio = require("miio"); -class MiHumidifierV2 extends Homey.Device { +class MiFanSA1 extends Homey.Device { async onInit() { this.initialize = this.initialize.bind(this); this.driver = this.getDriver(); @@ -11,61 +11,42 @@ class MiHumidifierV2 extends Homey.Device { } async initialize() { - this.registerActions(); - this.registerCapabilities(); + // this.registerActions(); + // this.registerCapabilities(); this.getHumidifierStatus(); } registerActions() { const { actions } = this.driver; - this.registerHumidifierOnAction("humidifier_on", actions.humidifierOn); - this.registerHumidifierOffAction("humidifier_off", actions.humidifierOff); - this.registerHumidifierModeAction("humidifier_ca1_mode", actions.humidifierMode); } registerCapabilities() { this.registerOnOffButton("onoff"); - this.registerDryOnOffButton("onoff.dry"); this.registerTargetRelativeHumidity("dim"); - this.registerHumidifierMode("humidifier_ca1_mode"); } getHumidifierStatus() { - var that = this; miio .device({ address: this.getSetting("deviceIP"), token: this.getSetting("deviceToken") }) - .then(device => { - this.setAvailable(); + .then((device) => { + if (!this.getAvailable()) { + this.setAvailable(); + } this.device = device; this.device - .call("get_prop", ["power", "humidity", "temp_dec", "mode", "limit_hum", "depth", "dry", "led_b", "buzzer", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_humidity", parseInt(result[1])); - that.setCapabilityValue("measure_temperature", parseInt(result[2] / 10)); - that.setCapabilityValue("humidifier_ca1_mode", result[3]); - that.setCapabilityValue("dim", parseInt(result[4] / 100)); - that.setCapabilityValue("measure_water", parseInt(result[5])); - that.setCapabilityValue("onoff", result[6] === "on" ? true : false); - that.setSettings({ led: result[5] === 2 ? false : true }); - that.setSettings({ buzzer: result[6] === "on" ? true : false }); - that.setSettings({ childLock: result[6] === "on" ? true : false }); + .call("get_prop", ["led", "angle", "speed", "poweroff_time", "power", "ac_power", "angle_enable", "speed_level", "natural_level", "child_lock", "buzzer", "led_b", "use_time"]) + .then((result) => { + this.log(this.getName(), result); }) - .catch(error => that.log("Sending commmand 'get_prop' error: ", error)); + .catch((error) => this.log("Sending commmand 'get_prop' error: ", error)); - var update = this.getSetting("updateTimer") || 60; + const update = this.getSetting("updateTimer") || 60; this.updateTimer(update); }) - .catch(error => { - this.log(error); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } + .catch((error) => { + this.setUnavailable(error.message); + clearInterval(this.updateInterval); setTimeout(() => { this.getHumidifierStatus(); }, 10000); @@ -73,33 +54,20 @@ class MiHumidifierV2 extends Homey.Device { } updateTimer(interval) { - var that = this; clearInterval(this.updateInterval); this.updateInterval = setInterval(() => { this.device - .call("get_prop", ["power", "humidity", "temp_dec", "mode", "limit_hum", "depth", "dry", "led_b", "buzzer", "child_lock"]) - .then(result => { - that.setCapabilityValue("onoff", result[0] === "on" ? true : false); - that.setCapabilityValue("measure_humidity", parseInt(result[1])); - that.setCapabilityValue("measure_temperature", parseInt(result[2] / 10)); - that.setCapabilityValue("humidifier_ca1_mode", result[3]); - that.setCapabilityValue("dim", parseInt(result[4] / 100)); - that.setCapabilityValue("measure_water", parseInt(result[5])); - that.setCapabilityValue("onoff", result[6] === "on" ? true : false); - that.setSettings({ led: result[5] === 2 ? false : true }); - that.setSettings({ buzzer: result[6] === "on" ? true : false }); - that.setSettings({ childLock: result[6] === "on" ? true : false }); + .call("get_prop", ["led", "angle", "speed", "poweroff_time", "power", "ac_power", "angle_enable", "speed_level", "natural_level", "child_lock", "buzzer", "led_b", "use_time"]) + .then((result) => { + if (!this.getAvailable()) { + this.setAvailable(); + } + this.log(this.getName(), result); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand error: ", error); + this.setUnavailable(error.message); clearInterval(this.updateInterval); - if (error == "Error: Could not connect to device, handshake timeout") { - this.setUnavailable(Homey.__("Could not connect to device, handshake timeout")); - this.log("Error: Could not connect to device, handshake timeout"); - } else if (error == "Error: Could not connect to device, token might be wrong") { - this.setUnavailable(Homey.__("Could not connect to device, token might be wrong")); - this.log("Error: Could not connect to device, token might be wrong"); - } setTimeout(() => { this.getHumidifierStatus(); }, 1000 * interval); @@ -107,8 +75,20 @@ class MiHumidifierV2 extends Homey.Device { }, 1000 * interval); } + updateCapabilityValue(capabilityName, value) { + if (this.getCapabilityValue(capabilityName) != value) { + this.setCapabilityValue(capabilityName, value) + .then(() => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability successfully updated"); + }) + .catch((error) => { + this.log("[" + this.data.id + "] [" + capabilityName + "] [" + value + "] Capability not updated because there are errors: " + error.message); + }); + } + } + onSettings(oldSettings, newSettings, changedKeys, callback) { - if (changedKeys.includes("updateTimer") || changedKeys.includes("gatewayIP") || changedKeys.includes("gatewayToken")) { + if (changedKeys.includes("updateTimer") || changedKeys.includes("deviceIP") || changedKeys.includes("deviceToken")) { this.getHumidifierStatus(); callback(null, true); } @@ -117,10 +97,10 @@ class MiHumidifierV2 extends Homey.Device { this.device .call("set_led_b", [newSettings.led ? 1 : 0]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.led); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_led_b' error: ", error); callback(error, false); }); @@ -130,10 +110,10 @@ class MiHumidifierV2 extends Homey.Device { this.device .call("set_buzzer", [newSettings.buzzer ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.buzzer); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_buzzer' error: ", error); callback(error, false); }); @@ -143,10 +123,10 @@ class MiHumidifierV2 extends Homey.Device { this.device .call("set_child_lock", [newSettings.childLock ? "on" : "off"]) .then(() => { - this.log("Sending " + name + " commmand: " + value); + this.log("Sending " + this.getName() + " commmand: " + newSettings.childLock); callback(null, true); }) - .catch(error => { + .catch((error) => { this.log("Sending commmand 'set_child_lock' error: ", error); callback(error, false); }); @@ -154,41 +134,41 @@ class MiHumidifierV2 extends Homey.Device { } registerOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_power", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_power' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_power' error: ", error)); }); } registerDryOnOffButton(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_dry", [value ? "on" : "off"]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_dry' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_dry' error: ", error)); }); } registerTargetRelativeHumidity(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { let humidity = value * 100; if (humidity > 0) { this.device .call("set_limit_hum", [humidity]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_limit_hum' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_limit_hum' error: ", error)); } }); } registerHumidifierMode(name) { - this.registerCapabilityListener(name, async value => { + this.registerCapabilityListener(name, async (value) => { this.device .call("set_mode", [value]) .then(() => this.log("Sending " + name + " commmand: " + value)) - .catch(error => this.log("Sending commmand 'set_mode' error: ", error)); + .catch((error) => this.log("Sending commmand 'set_mode' error: ", error)); }); } @@ -198,7 +178,7 @@ class MiHumidifierV2 extends Homey.Device { that.device .call("set_power", ["on"]) .then(() => that.log("Set 'set_power': ", args)) - .catch(error => that.log("Set 'set_power' error: ", error)); + .catch((error) => that.log("Set 'set_power' error: ", error)); }); } @@ -208,7 +188,7 @@ class MiHumidifierV2 extends Homey.Device { that.device .call("set_power", ["off"]) .then(() => that.log("Set 'set_power': ", args)) - .catch(error => that.log("Set 'set_power' error: ", error)); + .catch((error) => that.log("Set 'set_power' error: ", error)); }); } @@ -218,7 +198,7 @@ class MiHumidifierV2 extends Homey.Device { that.device .call("set_mode", [args.modes]) .then(() => that.log("Set 'set_mode': ", args.modes)) - .catch(error => that.log("Set 'set_mode' error: ", error)); + .catch((error) => that.log("Set 'set_mode' error: ", error)); }); } @@ -237,7 +217,7 @@ class MiHumidifierV2 extends Homey.Device { } onDeleted() { - this.log("Device deleted deleted"); + this.log("Device deleted"); clearInterval(this.updateInterval); if (typeof this.device !== "undefined") { this.device.destroy(); @@ -245,4 +225,4 @@ class MiHumidifierV2 extends Homey.Device { } } -module.exports = MiHumidifierV2; +module.exports = MiFanSA1; diff --git a/drivers/mi_fan_sa1/driver.js b/drivers/mi_fan_sa1/driver.js index 1b6aea4..f692144 100644 --- a/drivers/mi_fan_sa1/driver.js +++ b/drivers/mi_fan_sa1/driver.js @@ -1,40 +1,30 @@ const Homey = require("homey"); const miio = require("miio"); -const initFlowAction = action => ({ - action: new Homey.FlowCardAction(action).register() -}); - -class MiHumidifierV2 extends Homey.Driver { - onInit() { - this.actions = { - humidifierOn: initFlowAction("humidifier_on"), - humidifierOff: initFlowAction("humidifier_off"), - humidifierMode: initFlowAction("humidifier_ca1_mode") - }; - } +class MiFanSA1 extends Homey.Driver { + onInit() {} onPair(socket) { let pairingDevice = {}; - pairingDevice.name = "Mi Humidifier V2"; + pairingDevice.name = "Mi Fan SA1"; pairingDevice.settings = {}; pairingDevice.data = {}; - socket.on("connect", function(data, callback) { + socket.on("connect", (data, callback) => { this.data = data; miio .device({ address: data.ip, token: data.token }) - .then(device => { + .then((device) => { device .call("miIO.info", []) - .then(value => { + .then((value) => { if (value.model == this.data.model) { - pairingDevice.data.id = "MH:V2:" + value.mac + ":MH:V2"; + pairingDevice.data.id = "MF:S2:" + value.mac + ":MF:S2"; device .call("get_prop", ["power"]) - .then(value => { + .then((value) => { let result = { - state: value[0] + state: value[0], }; pairingDevice.settings.deviceIP = this.data.ip; pairingDevice.settings.deviceToken = this.data.token; @@ -43,33 +33,27 @@ class MiHumidifierV2 extends Homey.Driver { } else if (this.data.timer > 3600) { pairingDevice.settings.updateTimer = 3600; } else { - pairingDevice.settings.updateTimer = parseInt( - this.data.timer - ); + pairingDevice.settings.updateTimer = parseInt(this.data.timer); } callback(null, result); }) - .catch(error => callback(null, error)); + .catch((error) => callback(null, error)); } else { let result = { - notDevice: "It is not Mi Humidifier V2" + notDevice: "It is not Mi Humidifier V2", }; pairingDevice.data.id = null; callback(null, result); } }) - .catch(error => callback(null, error)); + .catch((error) => callback(null, error)); }) - .catch(error => { - if ( - error == "Error: Could not connect to device, handshake timeout" - ) { + .catch((error) => { + if (error == "Error: Could not connect to device, handshake timeout") { callback(null, "timeout"); } - if ( - error == "Error: Could not connect to device, token might be wrong" - ) { + if (error == "Error: Could not connect to device, token might be wrong") { callback(null, "wrongToken"); } else { callback(error, "Error"); @@ -77,10 +61,10 @@ class MiHumidifierV2 extends Homey.Driver { }); }); - socket.on("done", function(data, callback) { + socket.on("done", (data, callback) => { callback(null, pairingDevice); }); } } -module.exports = MiHumidifierV2; +module.exports = MiFanSA1; diff --git a/drivers/mi_fan_sa1/pair/xiaomi.html b/drivers/mi_fan_sa1/pair/xiaomi.html index d881d97..d7b8c52 100644 --- a/drivers/mi_fan_sa1/pair/xiaomi.html +++ b/drivers/mi_fan_sa1/pair/xiaomi.html @@ -2,12 +2,12 @@ -
-