Skip to content

Commit

Permalink
Merge branch 'master' into fix/connection-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jghaanstra authored Jun 30, 2018
2 parents 990c2ac + c77745f commit 3ab3277
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class XiaomiMiioApp extends Homey.App {
return Promise.resolve(customcommand);
})

new Homey.FlowCardAction('yeelightNightMode')
.register()
.registerRunListener((args, state) => {
return args.device.triggerCapabilityListener('night_mode', args.value === 'true');
})

// MI ROBOT: CONDITION AND ACTION FLOW CARDS
new Homey.FlowCardAction('findVacuum')
.register()
Expand Down
56 changes: 56 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
"dependencies": {
"net": "*"
},
"capabilities": {
"night_mode": {
"type": "boolean",
"title": {
"en": "Night Mode"
},
"getable": true,
"setable": true
}
},
"drivers": [
{
"id": "yeelights",
Expand Down Expand Up @@ -1069,6 +1079,52 @@
}
]
},
{
"id": "yeelightNightMode",
"title": {
"en": "Toggle Night Mode",
"nl": "Schakel Nacht Modus"
},
"hint": {
"en": "Use this card to switch the light between night mode and normal mode",
"nl": "Gebruik deze actie om de lamp tussen nacht modus en gewone modus te schakelen"
},
"args": [
{
"name": "value",
"type": "dropdown",
"placeholder": {
"en": "Finish action",
"nl": "Afrondactie"
},
"values": [
{
"id": "false",
"label": {
"en": "Normal Mode",
"nl": "Normale Modus"
}
},
{
"id": "true",
"label": {
"en": "Night Mode",
"nl": "Nacht Modus"
}
}
]
},
{
"name": "device",
"type": "device",
"placeholder": {
"en": "Select Yeelight",
"nl": "Selecteer Yeelight"
},
"filter": "driver_id=yeelights&capabilities=night_mode"
}
]
},
{
"id": "findVacuum",
"title": {
Expand Down
77 changes: 65 additions & 12 deletions drivers/yeelights/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class YeelightDevice extends Homey.Device {
this.registerCapabilityListener('dim', this.onCapabilityDim.bind(this));
this.registerMultipleCapabilityListener(['light_hue', 'light_saturation'], this.onCapabilityHueSaturation.bind(this), 500);
this.registerCapabilityListener('light_temperature', this.onCapabilityLightTemperature.bind(this));
this.registerCapabilityListener('night_mode', this.onCapabilityNightMode.bind(this));

let id = this.getData().id;
yeelights[id] = {};
Expand Down Expand Up @@ -41,18 +42,65 @@ 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);
}
}

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_power", "params":["on", "smooth", 500, 1]}');
}
callback(null, value);
}

onCapabilityHueSaturation(valueObj, optsObj) {
Expand Down Expand Up @@ -87,6 +135,10 @@ class YeelightDevice extends Homey.Device {
}
this.sendCommand(this.getData().id, '{"id":1,"method":"set_ct_abx","params":['+ color_temp +', "smooth", 500]}');
callback(null, value);

if(this.hasCapability('night_mode')){
this.setCapabilityValue('night_mode', false);
}
}

// HELPER FUNCTIONS
Expand Down Expand Up @@ -266,12 +318,13 @@ class YeelightDevice extends Homey.Device {
} else {
yeelights[id].socket.write(command + '\r\n');

clearTimeout(yeelights[id].timeout);
yeelights[id].timeout = setTimeout(() => {
if (yeelights[id].connected === true && yeelights[id].socket !== null) {
yeelights[id].socket.emit('error', new Error('Error sending command'));
}
}, 6000);
if (yeelights[id].timeout === null) {
yeelights[id].timeout = setTimeout(() => {
if (yeelights[id].connected === true && yeelights[id].socket !== null) {
yeelights[id].socket.emit('error', new Error('Error sending command'));
}
}, 6000);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions drivers/yeelights/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const typeCapabilityMap = {
'color' : [ 'onoff', 'dim', 'light_hue', 'light_saturation', 'light_temperature', 'light_mode' ],
'stripe' : [ 'onoff', 'dim', 'light_hue', 'light_saturation', 'light_temperature', 'light_mode' ],
'bslamp' : [ 'onoff', 'dim', 'light_hue', 'light_saturation', 'light_temperature', 'light_mode' ],
'ceiling' : [ 'onoff', 'dim', 'light_temperature', 'light_mode' ],
'ceiling' : [ 'onoff', 'dim', 'light_temperature', 'light_mode', 'night_mode' ],
'ceiling4' : [ 'onoff', 'dim', 'light_hue', 'light_saturation', 'light_temperature', 'light_mode' ],
'desklamp' : [ 'onoff', 'dim', 'light_temperature', 'light_mode' ]
}
Expand Down Expand Up @@ -42,7 +42,11 @@ class YeelightDriver extends Homey.Driver {
var name = Homey.__('yeelight_led_strip')+ ' (' + result[i].address + ')';
} else if (result[i].model == 'bslamp') {
var name = Homey.__('yeelight_bedside_lamp')+ ' (' + result[i].address + ')';
} else if (result[i].model == 'ceiling' || result[i].model == 'ceiling4') {
} else if (result[i].model.startsWith('ceiling')) {
if(result[i].model !== 'ceiling4') {
// Default the ceiling light to the default ceiling light.
result[i].model = 'ceiling';
}
var name = Homey.__('yeelight_ceiling_light')+ ' (' + result[i].address + ')';
} else if (result[i].model == 'desklamp') {
var name = Homey.__('yeelight_desklamp')+ ' (' + result[i].address + ')';
Expand Down

0 comments on commit 3ab3277

Please sign in to comment.