Skip to content

Commit

Permalink
Merge pull request #41 from BasKiers/feat/night_mode
Browse files Browse the repository at this point in the history
feat: added night_mode capability and flow card
  • Loading branch information
jghaanstra authored Jun 30, 2018
2 parents 6edd7ac + cc2a259 commit 614ad17
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 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
15 changes: 15 additions & 0 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,6 +42,15 @@ 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]}');
} else {
this.sendCommand(this.getData().id, '{"id": 1, "method": "set_power", "params":["on", "smooth", 500, 1]}');
}
callback(null, value);
}

onCapabilityDim(value, opts, callback) {
if(value == 0) {
var brightness = 1;
Expand Down Expand Up @@ -87,6 +97,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 @@ -257,6 +271,7 @@ class YeelightDevice extends Homey.Device {

/* send commands to devices using their socket connection */
sendCommand(id, command) {
console.log(command);
if (yeelights[id].connected === false && yeelights[id].socket !== null) {
yeelights[id].socket.emit('error', new Error('Connection to device broken'));
} else if (yeelights[id].socket === null) {
Expand Down

0 comments on commit 614ad17

Please sign in to comment.