From ceee661b48157d188a669b8b4938cfb171c3ae60 Mon Sep 17 00:00:00 2001 From: baskiers Date: Sat, 30 Jun 2018 11:54:25 +0200 Subject: [PATCH] feat: added auto-toggle for night_mode capability. Added logic to automatically toggle nightmode when dimming the device to 100/0 percent twice in a row within 5 seconds. This is usefull when controlling the device from your mobile phone (using the new app) so you can toggle night_mode without executing a flow. --- drivers/yeelights/device.js | 49 +++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/drivers/yeelights/device.js b/drivers/yeelights/device.js index 44ed449..785113f 100644 --- a/drivers/yeelights/device.js +++ b/drivers/yeelights/device.js @@ -41,18 +41,57 @@ class YeelightDevice extends Homey.Device { callback(null, value); } - onCapabilityDim(value, opts, callback) { - if(value == 0) { - var brightness = 1; - } else { - var brightness = value * 100; + async onCapabilityDim(value, opts, callback) { + let brightness = value === 0 ? 1 : value * 100; + let overWriteDimVal; + + // Logic which will toggle between night_mode and normal_mode when brightness is set to 0 or 100 two times within 5 seconds + if(this.hasCapability('night_mode') && opts.duration === undefined) { + if (value === 0) { + if (this.dimMinTime + 5000 > Date.now()) { + const initialNightModeValue = this.getCapabilityValue('night_mode'); + await this.triggerCapabilityListener('night_mode', true); + // If we think we really toggled the night mode we will set the brightness of night mode to 100 + if(initialNightModeValue === false) { + value = 1; + overWriteDimVal = 1; + brightness = 100; + } + this.dimMinTime = 0; + }else { + this.dimMinTime = Date.now(); + } + }else if (value === 1){ + if (this.dimMaxTime + 5000 > Date.now()) { + const initialNightModeValue = this.getCapabilityValue('night_mode'); + await this.triggerCapabilityListener('night_mode', false); + // If we think we really toggled the night mode we will set the brightness of normal mode to 1 + if(initialNightModeValue === true) { + value = 0; + overWriteDimVal = 0; + brightness = 1; + } + this.dimMaxTime = 0; + }else { + this.dimMaxTime = Date.now(); + } + }else { + this.dimMinTime = 0; + this.dimMaxTime = 0; + } } + if(typeof opts.duration !== 'undefined') { this.sendCommand(this.getData().id, '{"id":1,"method":"set_bright","params":['+ brightness +', "smooth", '+ opts.duration +']}'); } else { this.sendCommand(this.getData().id, '{"id":1,"method":"set_bright","params":['+ brightness +', "smooth", 500]}'); } callback(null, value); + + // "hack" to fix dim bar behaviour in the homey UI + if(overWriteDimVal !== undefined) { + this.setCapabilityValue('dim', overWriteDimVal); + } } onCapabilityHueSaturation(valueObj, optsObj) {