Skip to content

Commit

Permalink
Merge pull request #42 from BasKiers/feat/auto-toggle-night_mode
Browse files Browse the repository at this point in the history
Feat: auto toggle night mode
  • Loading branch information
jghaanstra authored Jun 30, 2018
2 parents 614ad17 + 9e1d275 commit c77745f
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions drivers/yeelights/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,63 @@ class YeelightDevice extends Homey.Device {
callback(null, value);
}

onCapabilityNightMode(value, opts, callback) {
if (value) {
this.sendCommand(this.getData().id, '{"id": 1, "method": "set_power", "params":["on", "smooth", 500, 5]}');
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_power", "params":["on", "smooth", 500, 1]}');
this.sendCommand(this.getData().id, '{"id":1,"method":"set_bright","params":['+ brightness +', "smooth", 500]}');
}
callback(null, value);
}

onCapabilityDim(value, opts, callback) {
if(value == 0) {
var brightness = 1;
} else {
var brightness = value * 100;
// "hack" to fix dim bar behaviour in the homey UI
if(overWriteDimVal !== undefined) {
this.setCapabilityValue('dim', overWriteDimVal);
}
if(typeof opts.duration !== 'undefined') {
this.sendCommand(this.getData().id, '{"id":1,"method":"set_bright","params":['+ brightness +', "smooth", '+ opts.duration +']}');
}

onCapabilityNightMode(value, opts, callback) {
if (value) {
this.sendCommand(this.getData().id, '{"id": 1, "method": "set_power", "params":["on", "smooth", 500, 5]}');
} else {
this.sendCommand(this.getData().id, '{"id":1,"method":"set_bright","params":['+ brightness +', "smooth", 500]}');
this.sendCommand(this.getData().id, '{"id": 1, "method": "set_power", "params":["on", "smooth", 500, 1]}');
}
callback(null, value);
}
Expand Down

0 comments on commit c77745f

Please sign in to comment.