Add get and set power state for activity

This commit is contained in:
madmod
2015-07-11 22:11:08 -06:00
parent 681cd7d9eb
commit 6cb9008424
3 changed files with 86 additions and 45 deletions
+4
View File
@@ -42,6 +42,10 @@
"port": "8000", "port": "8000",
"username": "username", "username": "username",
"password": "password" "password": "password"
},
{
"accessory": "LogitechHarmony",
"name": "Living Room Harmony Hub"
} }
], ],
+11 -9
View File
@@ -11,19 +11,21 @@
}, },
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"hap-nodejs": "git+https://github.com/khaost/HAP-NodeJS#2a1bc8d99a2009317ab5da93faebea34c89f197c",
"ad2usb": "git+https://github.com/alistairg/node-ad2usb.git#local", "ad2usb": "git+https://github.com/alistairg/node-ad2usb.git#local",
"request": "2.49.x",
"node-persist": "0.0.x",
"xmldoc": "0.1.x",
"node-hue-api": "^1.0.5",
"xml2js": "0.4.x",
"carwingsjs": "0.0.x", "carwingsjs": "0.0.x",
"elkington": "kevinohara80/elkington",
"hap-nodejs": "git+https://github.com/khaost/HAP-NodeJS#2a1bc8d99a2009317ab5da93faebea34c89f197c",
"harmonyhubjs-client": "^1.1.4",
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
"node-hue-api": "^1.0.5",
"node-milight-promise": "0.0.2",
"node-persist": "0.0.x",
"request": "2.49.x",
"sonos": "0.8.x", "sonos": "0.8.x",
"telldus-live": "0.2.x",
"wemo": "0.2.x", "wemo": "0.2.x",
"wink-js": "0.0.5", "wink-js": "0.0.5",
"elkington": "kevinohara80/elkington", "xml2js": "0.4.x",
"node-milight-promise": "0.0.2", "xmldoc": "0.1.x"
"telldus-live" : "0.2.x"
} }
} }
+69 -34
View File
@@ -17,7 +17,7 @@
// //
var types = require("../lib/HAP-NodeJS/accessories/types.js"); var types = require('HAP-NodeJS/accessories/types.js');
var harmonyDiscover = require('harmonyhubjs-discover'); var harmonyDiscover = require('harmonyhubjs-discover');
var harmony = require('harmonyhubjs-client'); var harmony = require('harmonyhubjs-client');
@@ -41,18 +41,17 @@ function LogitechHarmonyPlatform (log, config) {
LogitechHarmonyPlatform.prototype = { LogitechHarmonyPlatform.prototype = {
// Find one harmony remote hub (only support one for now) // Find one Harmony remote hub (only support one for now)
locateHub: function (callback) { locateHub: function (callback) {
var self = this;
var that = this;
// Connect to a Harmony hub // Connect to a Harmony hub
var createClient = function (ipAddress) { var createClient = function (ipAddress) {
that.log("Connecting to Logitech Harmony remote hub..."); self.log("Connecting to Logitech Harmony remote hub...");
harmony(ipAddress) harmony(ipAddress)
.then(function (client) { .then(function (client) {
that.log("Connected to Logitech Harmony remote hub"); self.log("Connected to Logitech Harmony remote hub");
callback(null, client); callback(null, client);
}); });
@@ -73,7 +72,7 @@ LogitechHarmonyPlatform.prototype = {
// TODO: Support update event with some way to add accessories // TODO: Support update event with some way to add accessories
// TODO: Have some kind of timeout with an error message. Right now this searches forever until it finds one hub. // TODO: Have some kind of timeout with an error message. Right now this searches forever until it finds one hub.
discover.on('online', function (hubInfo) { discover.on('online', function (hubInfo) {
that.log("Found Logitech Harmony remote hub: " + hubInfo.ip); self.log("Found Logitech Harmony remote hub: " + hubInfo.ip);
// Stop looking for hubs once we find the first one // Stop looking for hubs once we find the first one
// TODO: Support multiple hubs // TODO: Support multiple hubs
@@ -87,50 +86,53 @@ LogitechHarmonyPlatform.prototype = {
}, },
accessories: function (callback) { accessories: function (callback) {
var that = this; var self = this;
var foundAccessories = []; var foundAccessories = [];
// Get the first hub // Get the first hub
this.locateHub(function (err, hub) { this.locateHub(function (err, hub) {
if (err) throw err; if (err) throw err;
that.log("Fetching Logitech Harmony devices and activites..."); self.log("Fetching Logitech Harmony devices and activites...");
//getDevices(hub); //getDevices(hub);
getActivities(hub); getActivities(hub);
}); });
// Get Harmony Devices // Get Harmony Devices
/*
var getDevices = function(hub) { var getDevices = function(hub) {
that.log("Fetching Logitech Harmony devices..."); self.log("Fetching Logitech Harmony devices...");
hub.getDevices() hub.getDevices()
.then(function (devices) { .then(function (devices) {
that.log("Found devices: ", devices); self.log("Found devices: ", devices);
var sArray = sortByKey(json['result'],"Name"); var sArray = sortByKey(json['result'],"Name");
sArray.map(function(s) { sArray.map(function(s) {
accessory = new DomoticzAccessory(that.log, that.server, that.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); accessory = new LogitechHarmonyAccessory(self.log, self.server, self.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
foundAccessories.push(accessory); foundAccessories.push(accessory);
}); });
callback(foundAccessories); callback(foundAccessories);
}); });
}; };
*/
// Get Harmony Activities // Get Harmony Activities
var getActivities = function(hub) { var getActivities = function(hub) {
that.log("Fetching Logitech Harmony activities..."); self.log("Fetching Logitech Harmony activities...");
hub.getActivities() hub.getActivities()
.then(function (activities) { .then(function (activities) {
that.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n")); self.log("Found activities: \n" + activities.map(function (a) { return "\t" + a.label; }).join("\n"));
var sArray = sortByKey(activities, "label"); var sArray = sortByKey(activities, "label");
sArray.map(function(s) { sArray.map(function(s) {
var accessory = new LogitechHarmonyAccessory(that.log, hub, s, true); var accessory = new LogitechHarmonyAccessory(self.log, hub, s, true);
// TODO: Update the initial power state
foundAccessories.push(accessory); foundAccessories.push(accessory);
}); });
@@ -150,26 +152,56 @@ function LogitechHarmonyAccessory (log, hub, details, isActivity) {
this.id = details.id; this.id = details.id;
this.name = details.label; this.name = details.label;
this.isActivity = isActivity; this.isActivity = isActivity;
this.isActivityActive = false;
}; };
LogitechHarmonyAccessory.prototype = { LogitechHarmonyAccessory.prototype = {
command: function (command, value) { // TODO: Somehow make this event driven so that it tells the user what activity is on
this.log(this.name + " sending command " + command + " with value " + value); getPowerState: function (callback) {
var self = this;
if (this.isActivity) { if (this.isActivity) {
if (command === "On") { hub.getCurrentActivity().then(function (currentActivity) {
this.hub.startActivity(this.id) callback(currentActivity.id === self.id);
}).except(function (err) {
self.log('Unable to get current activity with error', err);
callback(false);
});
} else { } else {
this.hub.turnOff(); // TODO: Support onRead for devices
this.log('TODO: Support onRead for devices');
} }
},
setPowerState: function (state, callback) {
var self = this;
if (this.isActivity) {
this.log('Set activity ' + this.name + ' power state to ' + state);
// Activity id -1 is turn off all devices
var id = state ? this.id : -1;
this.hub.startActivity(id)
.then(function () {
self.log('Finished setting activity ' + self.name + ' power state to ' + state);
callback();
})
.catch(function (err) {
self.log('Failed setting activity ' + self.name + ' power state to ' + state + ' with error ' + err);
callback(err);
});
} else { } else {
// TODO: Support device specific commands // TODO: Support setting device power
this.log('TODO: Support setting device power');
callback();
} }
}, },
getServices: function () { getServices: function () {
var that = this; var self = this;
return [ return [
{ {
@@ -180,12 +212,13 @@ LogitechHarmonyAccessory.prototype = {
onUpdate: null, onUpdate: null,
perms: ["pr"], perms: ["pr"],
format: "string", format: "string",
initialValue: this.name, initialValue: self.name,
supportEvents: false, supportEvents: false,
supportBonjour: false, supportBonjour: false,
manfDescription: "Name of the accessory", manfDescription: "Name of the accessory",
designedMaxLength: 255 designedMaxLength: 255
},{ },
{
cType: types.MANUFACTURER_CTYPE, cType: types.MANUFACTURER_CTYPE,
onUpdate: null, onUpdate: null,
perms: ["pr"], perms: ["pr"],
@@ -195,7 +228,8 @@ LogitechHarmonyAccessory.prototype = {
supportBonjour: false, supportBonjour: false,
manfDescription: "Manufacturer", manfDescription: "Manufacturer",
designedMaxLength: 255 designedMaxLength: 255
},{ },
{
cType: types.MODEL_CTYPE, cType: types.MODEL_CTYPE,
onUpdate: null, onUpdate: null,
perms: ["pr"], perms: ["pr"],
@@ -205,17 +239,20 @@ LogitechHarmonyAccessory.prototype = {
supportBonjour: false, supportBonjour: false,
manfDescription: "Model", manfDescription: "Model",
designedMaxLength: 255 designedMaxLength: 255
},{ },
{
cType: types.SERIAL_NUMBER_CTYPE, cType: types.SERIAL_NUMBER_CTYPE,
onUpdate: null, onUpdate: null,
perms: ["pr"], perms: ["pr"],
format: "string", format: "string",
initialValue: "A1S2NASF88EW", // TODO: Add hub unique id to this for people with multiple hubs so that it is really a guid.
initialValue: self.id,
supportEvents: false, supportEvents: false,
supportBonjour: false, supportBonjour: false,
manfDescription: "SN", manfDescription: "SN",
designedMaxLength: 255 designedMaxLength: 255
},{ },
{
cType: types.IDENTIFY_CTYPE, cType: types.IDENTIFY_CTYPE,
onUpdate: null, onUpdate: null,
perms: ["pw"], perms: ["pw"],
@@ -236,7 +273,7 @@ LogitechHarmonyAccessory.prototype = {
onUpdate: null, onUpdate: null,
perms: ["pr"], perms: ["pr"],
format: "string", format: "string",
initialValue: this.name, initialValue: self.name,
supportEvents: true, supportEvents: true,
supportBonjour: false, supportBonjour: false,
manfDescription: "Name of service", manfDescription: "Name of service",
@@ -245,12 +282,9 @@ LogitechHarmonyAccessory.prototype = {
{ {
cType: types.POWER_STATE_CTYPE, cType: types.POWER_STATE_CTYPE,
onUpdate: function (value) { onUpdate: function (value) {
if (value == 0) { self.setPowerState(value)
that.command("Off")
} else {
that.command("On")
}
}, },
onRead: self.getPowerState,
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "bool", format: "bool",
initialValue: 0, initialValue: 0,
@@ -263,6 +297,7 @@ LogitechHarmonyAccessory.prototype = {
} }
]; ];
} }
}; };
module.exports.accessory = LogitechHarmonyAccessory; module.exports.accessory = LogitechHarmonyAccessory;