Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Playground
/dev.js

/node_modules
/npm-debug.log
/package-lock.json
/benchmark
79 changes: 76 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://cdn.rawgit.com/sqmk/huejay/db9081ee1a22acf77abc93cbd3f2e8f6d20ee16b/media/huejay.svg" alt="Huejay" />
<img src="https://cdn.jsdelivr.net/gh/sqmk/huejay@db9081ee1a22acf77abc93cbd3f2e8f6d20ee16b/media/huejay.svg" alt="Huejay" />
</p>

# Huejay - Philips Hue client for Node.js
Expand Down Expand Up @@ -36,6 +36,7 @@ Philips Hue API version supported: **1.19.0**
- [Bridge Discovery](#bridge-discovery)
- [Errors](#errors)
- [Client Usage](#client-usage)
- [Remote API](#using-the-remote-api)
- [Users](#users)
- [Bridge](#bridge)
- [Portal](#portal)
Expand Down Expand Up @@ -133,6 +134,78 @@ client commands.
The *timeout* option applies to bridge commands. The default value is 15000
milliseconds (or 15 seconds).

### Using the Remote API

Philips provides a hosted API with an identical interface to that of the local bridge API. Using the Remote API allows for bridges and the devices controlling them to be on seperate networks.

To use it, an OAuth token must be generated first.

After [adding a new Remote Hue API app](https://developers.meethue.com/my-apps/), open

`https://api.meethue.com/oauth2/auth?clientid=<client-id>&appid=<app-id>&deviceid=<device-id>&devicename=<device-name>&state=<state>&response_type=code`

in your browser to generate an authentication code.

Parameters:

| Parameter | Meaning |
|-------------|------------------------------------------------------------|
| client-id | Your app's client ID |
| app-id | Your app ID |
| device-id | Anything you want |
| device-name | Your app's name |
| state | Anything you want, will be passed back in the redirect URL |

Once a code has been generated, run

```javascript
const huejay = require('huejay');

(async () => {
const token = new huejay.OAuthToken({
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
});

await token.getByCode('your-auth-code');

console.log(JSON.stringify(token)) // => your OAuth token
})();
```

to generate an OAuth token.

You can then use the OAuth token in future requests like so:

```javascript
const huejay = require('huejay');

(async () => {
const token = new huejay.OAuthToken({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
accessToken: 'your-access-token',
refreshToken: 'your-refresh-token'
});

const client = new huejay.Client({
remote: true,
oauthToken: token,
username: 'your-bridge-username'
});

// Refresh the access token
await token.refresh();

// Turn light `1` on
const light = await client.lights.getById(1);

light.on = true;

await client.lights.save(light);
})();
```

### Users

Huejay provides several commands for managing users on Philips Hue bridges.
Expand Down Expand Up @@ -413,7 +486,7 @@ client.portal.get()
### Software Update

Occasionally, Philips releases new updates for the bridge, lights, and devices.
You can use Huejay to facilitate downloading and installation of updates.
You can use Huejay to facilitate downloading and installation of updates.

#### client.softwareUpdate.get - Get software update details

Expand Down Expand Up @@ -1930,7 +2003,7 @@ Get bridge resource limits and timezones.

Retrieve bridge light limits with the command `client.capabilities.lights`.
This command will eventually return an object describe the limits of the bridge
around the light resource.
around the light resource.

```js
client.capabilities.lights()
Expand Down
2 changes: 1 addition & 1 deletion lib/Action/ChangeGroupAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ChangeGroupAction extends AbstractAction {
}

if (!!withUsername) {
address = `/api/${client.username}${address}`;
address = `${client.username}${address}`;
}

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/Action/ChangeLightState.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ChangeLightState extends AbstractAction {
}

if (!!withUsername) {
address = `/api/${client.username}${address}`;
address = `${client.username}${address}`;
}

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/Action/ChangeSensorState.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ChangeSensorState extends AbstractAction {
}

if (!!withUsername) {
address = `/api/${client.username}${address}`;
address = `${client.username}${address}`;
}

return {
Expand Down
50 changes: 45 additions & 5 deletions lib/Client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

const OAuthToken = require('./OAuthToken');

const DEFAULT_CONFIG = {
host: undefined,
port: 80,
username: undefined,
timeout: 15000,
host: undefined,
port: 80,
remote: false,
oauthToken: new OAuthToken(),
username: undefined,
timeout: 15000,
};

/**
Expand Down Expand Up @@ -59,6 +63,42 @@ class Client {
this.config.port = Number(port);
}

/**
* Get remote
*
* @return {boolean} Remote
*/
get remote() {
return this.config.remote;
}

/**
* Set remote
*
* @param {boolean} remote Remote
*/
set remote(remote) {
this.config.remote = Boolean(remote);
}

/**
* Get OAuthToken
*
* @return {string} OAuthToken
*/
get oauthToken() {
return this.config.oauthToken;
}

/**
* Set OAuthToken
*
* @param {string} oauthToken OAuthToken
*/
set oauthToken(token) {
this.config.oauthToken = token;
}

/**
* Get username
*
Expand Down Expand Up @@ -223,7 +263,7 @@ class Client {

/**
* Get capabilities accessor
*
*
* @return {mixed} Capabilities accessor
*/
get capabilities() {
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Bridge/EnableLinkButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EnableLinkButton {
invoke(client) {
let options = {
method: 'PUT',
url: `api/${client.username}/config`,
url: `${client.username}/config`,
data: {
'linkbutton': true
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Bridge/EnableTouchlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EnableTouchlink {
invoke(client) {
let options = {
method: 'PUT',
url: `api/${client.username}/config`,
url: `${client.username}/config`,
data: {
'touchlink': true
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/Bridge/GetBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class GetBridge {
invoke(client) {
let options = {
url: client.username !== undefined
? `api/${client.username}/config`
: 'api/config'
? `${client.username}/config`
: 'config'
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Bridge/IsAuthenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class IsAuthenticated {
invoke(client) {
let options = {
// Time zone retrieval as it requires user and not resource intensive
url: `api/${client.username}/info/timezones`
url: `${client.username}/info/timezones`
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Bridge/Ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Ping {
*/
invoke(client) {
let options = {
url: 'api/config'
url: 'config'
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Bridge/SaveBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SaveBridge {
saveBridgeAttribute(client, attribute, value) {
let options = {
method: 'PUT',
url: `api/${client.username}/config`,
url: `${client.username}/config`,
data: {}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetGroups {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetLights.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetLights {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetResourceLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetResourceLinks {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetRules {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetScenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetScenes {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetSchedules.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetSchedules {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetSensors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetSensors {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Capability/GetTimeZones.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetTimeZones {
*/
invoke(client) {
let options = {
url: `api/${client.username}/capabilities`,
url: `${client.username}/capabilities`,
raw: true
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/CreateGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CreateGroup {
invoke(client) {
let options = {
method: 'POST',
url: `api/${client.username}/groups`,
url: `${client.username}/groups`,
data: {}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/DeleteGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DeleteGroup {
invoke(client) {
let options = {
method: 'DELETE',
url: `api/${client.username}/groups/${this.groupId}`
url: `${client.username}/groups/${this.groupId}`
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/GetGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GetGroup {
*/
invoke(client) {
let options = {
url: `api/${client.username}/groups/${this.groupId}`
url: `${client.username}/groups/${this.groupId}`
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/GetGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GetGroups {
*/
invoke(client) {
let options = {
url: `api/${client.username}/groups`
url: `${client.username}/groups`
};

return client.getTransport()
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/SaveGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SaveGroup {
invoke(client) {
let options = {
method: 'PUT',
url: `api/${client.username}/groups/${this.group.id}`,
url: `${client.username}/groups/${this.group.id}`,
data: {}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Group/SaveGroupAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SaveGroupAction {
invoke(client) {
let options = {
method: 'PUT',
url: `api/${client.username}/groups/${this.group.id}/action`,
url: `${client.username}/groups/${this.group.id}/action`,
data: {},
multi: true
};
Expand Down
Loading