Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Electricitymeter / Cumulative device #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
{
"name": "device",
"type": "device",
"filter": "driver_id=virtual_switch"
"filter": "driver_id=virtual_switch|electricitymeter"
}
]
},
Expand Down Expand Up @@ -465,6 +465,58 @@
"id": "done"
}
]
},
{
"id": "electricitymeter",
"name": {
"en": "Electricity meter",
"nl": "Elektriciteitsmeter",
"de": "Stromzähler",
"no": "Strømmåler"
},
"images": {
"large": "drivers/electricitymeter/assets/images/large.png",
"small": "drivers/electricitymeter/assets/images/small.png"
},
"class": "other",
"capabilities": [
"onoff"
],

"energy": {
"cumulative": true
},

"pair": [
{
"id": "name",
"navigation": {
"next": "icon"
}
},
{
"id": "icon",
"navigation": {
"prev": "name"
}
},
{
"id": "class",
"navigation": {
"prev": "icon"
}
},
{
"id": "capabilities",
"navigation": {
"next": "done",
"prev": "class"
}
},
{
"id": "done"
}
]
},
{
"id": "mode",
Expand Down
1 change: 1 addition & 0 deletions drivers/electricitymeter/assets/electricitymeter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drivers/electricitymeter/assets/images/large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added drivers/electricitymeter/assets/images/small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 111 additions & 0 deletions drivers/electricitymeter/device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';

const Homey = require('homey');
const fs = require('fs');

//a list of devices, with their 'id' as key
//it is generally advisable to keep a list of
//paired and active devices in your driver's memory.
var devices = {};

class VirtualDevice extends Homey.Device {
onInit() {
this.log('Virtual Device (' + this.getName() + ') initialized');

// this.log('name: ', this.getName());
// this.log('id: ', this.getData().id);
// this.log('version: ', this.getData().version);
// this.log('icon: ', this.getData().icon);
// this.log('class: ', this.getClass());
// this.log('capabilities:', JSON.stringify(this.getCapabilities()));
// this.getCapabilities().forEach(capability => {
// this.log('capabilityOptions - ' + capability + ':', JSON.stringify(this.getCapabilityOptions(capability)));
// });
// this.log('state: ', this.getState());

let thisDeviceChanged = new Homey.FlowCardTriggerDevice('press');
thisDeviceChanged.register();

let aVirtualDeviceChanged = new Homey.FlowCardTrigger('device_changed');
aVirtualDeviceChanged.register();

// When capability is changed
this.registerMultipleCapabilityListener(this.getCapabilities(), (changedCapabs, optsObj) => {
this.log(this.getName() + ' -> Capability changed: ' + JSON.stringify(changedCapabs));

for (var capability in changedCapabs) {
var value = changedCapabs[capability];
// this.log('capability: ' + capability);
// this.log('value: ' + value);

if ( capability === 'onoff' ) {
if ( this.getCapabilityValue('onoff') === value ) {
return Promise.resolve(); // no change, no triggers
}
}

if (capability === 'dim' && this.hasCapability( 'onoff' )) {
if ( value > 0 ) {
this.setCapabilityValue( 'onoff', true )
} else {
this.setCapabilityValue( 'onoff', false )
}
}

process.nextTick(async () => {
await sleep(100);
thisDeviceChanged.trigger( this, {}, changedCapabs )
.catch( this.error );
});

let tokens = {
'device': this.getName(),
'variable': capability,
'value': '' + value
}
aVirtualDeviceChanged.trigger( tokens ) // Fire and forget
.catch( this.error )
}

return Promise.resolve();
}, 500);
}

// this method is called when the Device is added
onAdded() {
this.log('Adding device');
this.log('Adding device: ' + this.getName() + ' (' + this.getData().id + ')');
}

// this method is called when the Device is deleted
onDeleted() {
this.log('device deleted: ' + this.getName());

if( typeof this.getData().icon !== 'undefined' && this.getData().icon !== null
&& this.getData().icon.startsWith("../../../userdata")) {
removeIcon(this.getData().icon)
}
}
}

function removeIcon(iconpath) {
console.log("removeIcon( " + iconpath + " )");
return new Promise((resolve, reject) => {
try {
if (fs.existsSync(iconpath)) {
fs.unlinkSync(iconpath);
return resolve(true);
} else {
return resolve(true);
}
} catch (error) {
return reject(error);
}
})
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

module.exports = VirtualDevice;
Loading