Skip to content

Commit

Permalink
Added Scenes
Browse files Browse the repository at this point in the history
Added Dimmers
New request method
Added Switch
Disabled security sensors
  • Loading branch information
damianalarcon committed Sep 24, 2016
1 parent 89323c8 commit 85ae815
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 34 deletions.
54 changes: 37 additions & 17 deletions lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function(HAPnode, config)
devices[device.room].push(device);
});

return({'rooms': data.rooms,'devices_by_room': devices, 'devices_full_list': data.devices});
return({'rooms': data.rooms,'devices_by_room': devices, 'devices_full_list': data.devices, 'scenes': data.scenes});
}
else
{
Expand Down Expand Up @@ -154,25 +154,32 @@ module.exports = function(HAPnode, config)
switch (device.category)
{
case 2: // Dimmable Light:
if(config.dimmertest)
{
// Specifically looking the word "fan" in the device name, very shaky assumption
if (device.name.toLowerCase().includes("fan")){
var Fan = require("./types/fan.js")(HAPnode,config,module);
HAPnode.debug('------ Fan Added: %s', device.name + ' ID:' +device.id);
accessories.push(Fan.newDevice(device));
} else {
var DimmableLight = require("./types/dimmer.js")(HAPnode,config,module);
HAPnode.debug('------ Dimmer light Added: %s', device.name + ' ID:' +device.id);
accessories.push(DimmableLight.newDevice(device));
}
// Specifically looking the word "fan" in the device name, very shaky assumption
if (device.name.toLowerCase().includes("fan")){
var Fan = require("./types/fan.js")(HAPnode,config,module);
HAPnode.debug('------ Fan Added: %s', device.name + ' ID:' +device.id);
accessories.push(Fan.newDevice(device));
} else {
var DimmableLight = require("./types/dimmer.js")(HAPnode,config,module);
HAPnode.debug('------ Dimmer light Added: %s', device.name + ' ID:' +device.id);
accessories.push(DimmableLight.newDevice(device));
}
break;

case 3: // Switch
var Switch = require("./types/switch.js")(HAPnode,config,module);
accessories.push(Switch.newDevice(device));
HAPnode.debug('------ Switch Added: %s', device.name + ' ID:' +device.id);
if(device.subcategory > 0)
{
console.log('we have a switch')
var Switch = require("./types/switch.js")(HAPnode,config,module);
accessories.push(Switch.newDevice(device));
HAPnode.debug('------ Switch Added: %s', device.name + ' ID:' +device.id);
}
else
{
var Lightbulb = require("./types/light.js")(HAPnode,config,module);
accessories.push(Lightbulb.newDevice(device));
HAPnode.debug('------ Lightbulb Added: %s', device.name + ' ID:' +device.id);
}
break;

case 4: // Security Sensor
Expand Down Expand Up @@ -200,8 +207,21 @@ module.exports = function(HAPnode, config)
}
break;
}
});

accessories = module.processscenes(accessories, verainfo.scenes);

return accessories;
};

module.processscenes = function(accessories, list)

This comment has been minimized.

Copy link
@drewcovi

drewcovi Nov 8, 2016

Collaborator

Would you support a PR that lets a flag disable this, or exclude some device ids? Although there is technically a flag for active in scenes, UI7 has pretty much abandoned this, and scenes can be set up already in Homekit (but cannot be imported) so this tends to add quite a few crufty devices.

{
list.forEach(function(scene)
{
var Scene = require("./types/scene.js")(HAPnode,config,module);
accessories.push(Scene.newScene(scene));
HAPnode.debug('------ Scene Added: %s', scene.name + ' ID:' +scene.id);
});


return accessories;
};
Expand Down
116 changes: 116 additions & 0 deletions lib/types/light.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module.exports = function(HAPnode, config, functions)
{
var Accessory = HAPnode.Accessory;
var Service = HAPnode.Service;
var Characteristic = HAPnode.Characteristic;
var uuid = HAPnode.uuid;
var debug = HAPnode.debug;

var module = {};

module.newDevice = function(device)
{
var Lightbulb = {
powerOn: (parseInt(device.status) === 1)?true:false,

setPower: function(on)
{
if (on)
{
binaryState = 1;
Lightbulb.powerOn = true;
}
else
{
binaryState = 0;
Lightbulb.powerOn = false;
}

debug("Making request for device %s", device.name);

return HAPnode.request({method:'GET',uri:"http://"+config.veraIP+":3480/data_request?id=lu_action&output_format=xml&DeviceNum=" + device.id + "&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=" + binaryState, resolveWithFullResponse: true}).then(function (res)
{
if (res.statusCode === 200)
{
status = (Lightbulb.powerOn)?'On':'Off';
debug("The %s has been turned %s", device.name, status);
}
else
{
debug("Error while turning the %s on/off %s", device.name);
}
});
},
getStatus: function()
{
debug("Making request for device %s", device.name);
return HAPnode.request(
{
method: 'POST',
uri: 'http://'+config.veraIP+':3480/data_request?id=variableget&DeviceNum='+device.id+'&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status',
resolveWithFullResponse: true
}).then(function (res)
{
if (res.statusCode === 200)
{
data = parseInt(res.body.toString('utf8'));
this.powerOn = (data === 1)?true:false;
status = (this.powerOn)?'On':'Off';

debug("Status for the light %s is %s", device.name, status);
return this.powerOn;
}
else
{
debug("Error while getting the status for %s", device.name);
return this.powerOn;
}
}.bind(this));
},
identify: function()
{
debug("Identify the light %s", device.name);
}
};

var light = new Accessory(device.name, uuid.generate('device:Lightbulb:'+config.cardinality+':'+device.id));

light.username = functions.genMac('device:'+config.cardinality+':'+device.id);
light.pincode = config.pincode;
light.deviceid = device.id;

light
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Oltica")
.setCharacteristic(Characteristic.Model, "Rev-1")
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");

light.on('identify', function(paired, callback) {
Lightbulb.identify();
callback(); // success
});

light
.addService(Service.Lightbulb, device.name)
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
Lightbulb.setPower(value);
callback();
});

light
.getService(Service.Lightbulb)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
var err = null;
Lightbulb.getStatus().then(function(val) {
callback(err, val);
});

});

return light;
};

return module;
};
13 changes: 9 additions & 4 deletions lib/types/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ module.exports = function(HAPnode, config, functions)
getLockstatus: function()
{
debug("Making request on device %s",device.name);
console.log('status');
return HAPnode.request({method:'GET',uri:'http://'+config.veraIP+':3480/data_request?id=variableget&DeviceNum='+device.id+'&serviceId=urn:micasaverde-com:serviceId:DoorLock1&Variable=Status', resolveWithFullResponse: true}).then(function (res)
{
if (res.statusCode === 200)
{
data = parseInt(res.body.toString('utf8'));
this.locked = (data === 1)?true:false;
status = (this.locked)?'Locked':'Unlocked';

console.log('lock status'+this.locked);
debug("Status for the lock %s is %s", device.name, status);
return this.locked;
}
else
{
console.log('error status');
debug("Error while getting the status for %s", device.name);
return this.locked;
}
Expand Down Expand Up @@ -117,15 +119,18 @@ module.exports = function(HAPnode, config, functions)
.getCharacteristic(Characteristic.LockCurrentState)
.on('get', function(callback) {
var err = null;

Lock.getLockstatus().then(function(val){
if(val)
{
callback(err, Characteristic.LockCurrentState.SECURED);
callback(err, true);
}
else
{
callback(err, Characteristic.LockCurrentState.UNSECURED);

lock
.getService(Service.LockMechanism)
.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.UNSECURED);
callback(err, Characteristic.LockCurrentState.SECURED);
}
});

Expand Down
88 changes: 88 additions & 0 deletions lib/types/scene.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module.exports = function(HAPnode, config, functions)
{
var Accessory = HAPnode.Accessory;
var Service = HAPnode.Service;
var Characteristic = HAPnode.Characteristic;
var uuid = HAPnode.uuid;
var debug = HAPnode.debug;

var module = {};

module.newScene = function(scene)
{
var Switch = {
powerOn: (parseInt(scene.active) === 1)?true:false,

setPower: function(on)
{
if (on)
{
Switch.powerOn = true;
}
else
{
Switch.powerOn = false;
}

debug("Making request for scene %s", scene.name);

return HAPnode.request({method:'GET',uri:"http://"+config.veraIP+":3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=" + scene.id, resolveWithFullResponse: true}).then(function (res)
{
if (res.statusCode === 200)
{
status = (Switch.powerOn)?'On':'Off';
debug("The scene %s has been turned %s", scene.name, status);
}
else
{
debug("Error while turning the %s on/off %s", scene.name);
}
});
},
getStatus: function()
{
return Switch.powerOn;
},
identify: function()
{
debug("Identify the light %s", scene.name);
}
};

var switchac = new Accessory(scene.name, uuid.generate('scene:switch:'+config.cardinality+':'+scene.id));

switchac.username = functions.genMac('scene:'+config.cardinality+':'+scene.id);
switchac.pincode = config.pincode;
switchac.deviceid = scene.id;

switchac
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Oltica")
.setCharacteristic(Characteristic.Model, "Rev-1")
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");

switchac.on('identify', function(paired, callback) {
Switch.identify();
callback(); // success
});

switchac
.addService(Service.Switch, scene.name)
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
Switch.setPower(value);
callback();
});

switchac
.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
callback(null, Switch.getStatus());
});

return switchac;
};

return module;
};
24 changes: 11 additions & 13 deletions lib/types/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,33 @@ module.exports = function(HAPnode, config, functions)
}
};

var lightUUID = uuid.generate('device:switch:'+config.cardinality+':'+device.id);
var switchac = new Accessory(device.name, uuid.generate('device:switch:'+config.cardinality+':'+device.id));

var light = new Accessory(device.name, lightUUID);
switchac.username = functions.genMac('device:'+config.cardinality+':'+device.id);
switchac.pincode = config.pincode;
switchac.deviceid = device.id;

light.username = functions.genMac('device:'+config.cardinality+':'+device.id);
light.pincode = config.pincode;
light.deviceid = device.id;

light
switchac
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Oltica")
.setCharacteristic(Characteristic.Model, "Rev-1")
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");

light.on('identify', function(paired, callback) {
switchac.on('identify', function(paired, callback) {
Switch.identify();
callback(); // success
});

light
.addService(Service.Lightbulb, device.name)
switchac
.addService(Service.Switch, device.name)
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
Switch.setPower(value);
callback();
});

light
.getService(Service.Lightbulb)
switchac
.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
var err = null;
Expand All @@ -111,7 +109,7 @@ module.exports = function(HAPnode, config, functions)

});

return light;
return switchac;
};

return module;
Expand Down

0 comments on commit 85ae815

Please sign in to comment.