Skip to content

Commit

Permalink
GitHub #8 - Add support for light management
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmk committed Oct 26, 2015
1 parent 3ebbefe commit 35cffcb
Show file tree
Hide file tree
Showing 16 changed files with 935 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Use Huejay to interact with Philips Hue in the following ways:
* Manage portal settings
* Manage software updates
* Manage users
* Manage lights

## Installation

Expand Down
38 changes: 38 additions & 0 deletions examples/get-lights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

'use strict';

let huejay = require('../lib/Huejay');
let credentials = require('./.credentials.json');

let client = new huejay.Client(credentials);

console.log('Retrieving lights...');
console.log();

client.getLights()
.then(lights => {
for (let light of lights) {
console.log(`Light [${light.id}]: ${light.name}`);
console.log(` Type: ${light.type}`);
console.log(` Unique ID: ${light.uniqueId}`);
console.log(` Manufacturer: ${light.manufacturer}`);
console.log(` Model Id: ${light.modelId}`);
console.log(` Software Version: ${light.softwareVersion}`);
console.log(' State:');
console.log(` On: ${light.on}`);
console.log(` Reachable: ${light.reachable}`);
console.log(` Brightness: ${light.brightness}`);
console.log(` Color mode: ${light.colorMode}`);
console.log(` Hue: ${light.hue}`);
console.log(` Saturation: ${light.saturation}`);
console.log(` X/Y: ${light.xy[0]}, ${light.xy[1]}`);
console.log(` Color Temp: ${light.colorTemp}`);
console.log(` Alert: ${light.alert}`);
console.log(` Effect: ${light.effect}`);
console.log();
}
})
.catch(error => {
console.log(error.stack);
});
22 changes: 22 additions & 0 deletions examples/get-new-lights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

'use strict';

let huejay = require('../lib/Huejay');
let credentials = require('./.credentials.json');

let client = new huejay.Client(credentials);

console.log('Retrieving new lights...');
console.log();

client.getNewLights()
.then(lights => {
for (let light of lights) {
console.log(light);
console.log();
}
})
.catch(error => {
console.log(error.stack);
});
28 changes: 28 additions & 0 deletions examples/save-light.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

'use strict';

let huejay = require('../lib/Huejay');
let credentials = require('./.credentials.json');

let client = new huejay.Client(credentials);

console.log(`Retrieving light from (${credentials.host})...`);

client.getLights()
.then(lights => {
let light = lights[4];

light.name = `Name test`;
light.on = true;

console.log(`Saving light...`);

return client.saveLight(light);
})
.then(() => {
console.log('Success');
})
.catch(error => {
console.log(error.stack);
});
18 changes: 18 additions & 0 deletions examples/start-light-scan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

'use strict';

let huejay = require('../lib/Huejay');
let credentials = require('./.credentials.json');

let client = new huejay.Client(credentials);

console.log('Starting light scan...');

client.startLightScan()
.then(() => {
console.log('Success');
})
.catch(error => {
console.log(error.stack);
});
76 changes: 38 additions & 38 deletions lib/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,7 @@ class Bridge {
*/
constructor(attributes) {
this.setAttributes(attributes);
this.resetChangedValues();
}

/**
* Set attributes
*
* @param {Object} attributes Attributes
*/
setAttributes(attributes) {
this.attributes = {};

for (let key in attributes) {
let attributeKey = key;
if (key in ATTRIBUTE_MAP) {
this.attributes[ATTRIBUTE_MAP[key]] = attributes[key];
}
}
this.resetChangedAttributes();
}

/**
Expand Down Expand Up @@ -83,7 +67,7 @@ class Bridge {
set name(name) {
this.attributes.name = name;

this.changed.push('name');
this.changedAttributes.push('name');
}

/**
Expand Down Expand Up @@ -148,7 +132,7 @@ class Bridge {
set zigbeeChannel(channel) {
this.attributes.zigbeeChannel = Number(channel);

this.changed.push('zigbeeChannel');
this.changedAttributes.push('zigbeeChannel');
}

/**
Expand Down Expand Up @@ -177,7 +161,7 @@ class Bridge {
set ipAddress(ipAddress) {
this.attributes.ipAddress = String(ipAddress);

this.changed.push('ipAddress');
this.changedAttributes.push('ipAddress');
}

/**
Expand All @@ -197,7 +181,7 @@ class Bridge {
set dhcpEnabled(value) {
this.attributes.dhcp = Boolean(value);

this.changed.push('dhcp');
this.changedAttributes.push('dhcp');
}

/**
Expand All @@ -217,7 +201,7 @@ class Bridge {
set netmask(mask) {
this.attributes.netmask = String(mask);

this.changed.push('netmask');
this.changedAttributes.push('netmask');
}


Expand All @@ -238,7 +222,7 @@ class Bridge {
set gateway(address) {
this.attributes.gateway = String(address);

this.changed.push('gateway');
this.changedAttributes.push('gateway');
}

/**
Expand All @@ -262,7 +246,7 @@ class Bridge {
set proxyAddress(address) {
this.attributes.proxyAddress = String(address);

this.changed.push('proxyAddress');
this.changedAttributes.push('proxyAddress');
}

/**
Expand All @@ -284,7 +268,7 @@ class Bridge {
set proxyPort(port) {
this.attributes.proxyPort = Number(port);

this.changed.push('proxyPort');
this.changedAttributes.push('proxyPort');
}

/**
Expand Down Expand Up @@ -317,7 +301,7 @@ class Bridge {
set timeZone(timeZone) {
this.attributes.timeZone = String(timeZone);

this.changed.push('timeZone');
this.changedAttributes.push('timeZone');
}

/**
Expand Down Expand Up @@ -368,7 +352,7 @@ class Bridge {
set linkButtonEnabled(value) {
this.attributes.linkButton = Boolean(value);

this.changed.push('linkButton');
this.changedAttributes.push('linkButton');
}

/**
Expand All @@ -388,29 +372,45 @@ class Bridge {
set touchLinkEnabled(value) {
this.attributes.touchLink = Boolean(value);

this.changed.push('touchLink');
this.changedAttributes.push('touchLink');
}

/**
* Set attributes
*
* @param {Object} attributes Attributes
*/
setAttributes(attributes) {
this.attributes = {};

for (let key in attributes) {
let attributeKey = key;
if (key in ATTRIBUTE_MAP) {
this.attributes[ATTRIBUTE_MAP[key]] = attributes[key];
}
}
}

/**
* Get changed
* Get changed attributes
*
* @return {array} List of changed values (with original keys)
* @return {array} List of changed attributes
*/
getChangedValues() {
let changedValues = {};
getChangedAttributes() {
let changedAttributes = {};

for (let key of this.changed) {
changedValues[key] = this.attributes[key];
for (let key of this.changedAttributes) {
changedAttributes[key] = this.attributes[key];
}

return changedValues;
return changedAttributes;
}

/**
* Reset changed values
* Reset changed attributes
*/
resetChangedValues() {
this.changed = [];
resetChangedAttributes() {
this.changedAttributes = [];
}

/**
Expand Down
63 changes: 63 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,69 @@ class Client {
return this.invokeCommand(new DeleteUser(username));
}

/**
* Start light scan
*
* @return {Promise} Promise for chaining
*/
startLightScan() {
let StartLightScan = require('./Command/StartLightScan');

return this.invokeCommand(new StartLightScan);
}

/**
* Get new lights
*
* @return {Promise} Promise for chaining
*/
getNewLights() {
let GetNewLights = require('./Command/GetNewLights');

return this.invokeCommand(new GetNewLights);
}

/**
* Get lights
*
* @return {Promise} Promise for chaining
*/
getLights() {
let GetLights = require('./Command/GetLights');

return this.invokeCommand(new GetLights);
}

/**
* Save light
*
* @param {Light} light Light
*
* @return {Promise} Promise for chaining
*/
saveLight(light) {
let SaveLight = require('./Command/SaveLight');
let SaveLightState = require('./Command/SaveLightState');

return Promise.all([
this.invokeCommand(new SaveLight(light)),
this.invokeCommand(new SaveLightState(light))
]);
}

/**
* Delete light
*
* @param {mixed} lightId Light Id or Light object
*
* @return {Promise} Promise for chaining
*/
deleteLight(lightId) {
let DeleteLight = require('./Command/DeleteLight');

return this.invokeCommand(new DeleteLight(light));
}

/**
* Get time zones
*
Expand Down
37 changes: 37 additions & 0 deletions lib/Command/DeleteLight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

/**
* Delete light command
*
* Delete a light by id
*/
class DeleteLight {
/**
* Constructor
*
* @param {string} lightId Light Id or Light object
*/
constructor(lightId) {
this.lightId = String(lightId);
}

/**
* Invoke command
*
* @param {Client} client Client
*
* @return {Promise} Promise for chaining
*/
invoke(client) {
let options = {
method: 'DELETE',
path: `api/${client.username}/lights/${this.lightId}`
};

return client.getTransport()
.sendRequest(options)
.then(() => true);
}
}

module.exports = DeleteLight;
Loading

0 comments on commit 35cffcb

Please sign in to comment.