Skip to content

Commit

Permalink
Merge pull request #14 from mattbucci/master
Browse files Browse the repository at this point in the history
update to new hue-js interface, storing username token in config file
  • Loading branch information
bahamas10 authored Sep 22, 2016
2 parents a9b31b3 + a63c1ac commit 43239de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ Usage
options
-h, --help print this message and exit
-H, --host the hostname or ip of the bridge to control
-i, --init initialize the config file at ~/.hue.json
-j, --json force output to be in json
-s, --save save the config file at ~/.hue.json, same as --init
-u, --updates check for available updates
-v, --version print the version number and exit

Expand Down Expand Up @@ -111,19 +109,6 @@ Again, `-j` if you'd like json output.
Running with the command `lights` will give us a list of all the lights
connected to the base station.

Before we continue, let's create a configuration file. In the file we can
set the default host to connect to, so we don't have to keep supplying the
`-H` argument. Run:

$ hue --host 10.0.1.218 --save
config file written to `~/.hue.json`
{
"host": "10.0.1.218"
}

Now we'll no longer have to supply the `-H` argument
with every command.

From here, we can get information about a single light like:

$ hue lights 1
Expand Down Expand Up @@ -268,7 +253,7 @@ The `state` keyword tells `hue` to read from stdin
Config
------

An optional config file can be created at `~/.hue.json` that looks like...
A config file will be created at `~/.hue.json` upon registration that looks like...

``` json
{
Expand Down
40 changes: 27 additions & 13 deletions hue-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ function usage() {
'options',
' -h, --help print this message and exit',
' -H, --host the hostname or ip of the bridge to control',
' -i, --init initialize the config file at ' + configfile,
' -j, --json force output to be in json',
' -s, --save save the config file at ' + configfile + ', same as --init',
' -u, --updates check for available updates',
' -v, --version print the version number and exit'
].join('\n');
Expand All @@ -84,9 +82,7 @@ function usage() {
var options = [
'h(help)',
'H:(host)',
'i(init)',
'j(json)',
's(save)',
'u(updates)',
'v(version)'
].join('');
Expand All @@ -98,12 +94,6 @@ while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'h': console.log(usage()); process.exit(0);
case 'H': config.host = option.optarg; break;
case 'i': case 's':
var s = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, s + '\n');
console.log('config file written to `%s`', configfile);
console.log(s);
process.exit(0);
case 'j': json = true; break;
case 'u': // check for updates
require('latest').checkupdate(package, function(ret, msg) {
Expand Down Expand Up @@ -243,14 +233,30 @@ switch (args[0]) {
});
break;
case 'register': // register this app
// Check for existing config
var existingconfig = statPath(configfile);
if (existingconfig && existingconfig.isFile()) {
console.log('A config file already exists at %s', configfile);
console.log('please remove it before attempting to register a new hub')
process.exit(1);
}
// Attempt to pair with hue hub
client = getclient();
console.log('please go and press the link button on your base station');
client.register(function(err) {
client.register(function(err, resp) {
if (err) {
console.error('failed to pair to Hue Base Station %s', config.host);
throw err;
}

console.log('Hue Base Station paired!')
console.log('username: ' + resp[0].success.username);
config.username = resp[0].success.username;

// writing config file
var s = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, s + '\n');
console.log('config file written to `%s`', configfile);
});
break;
case 'search': // search for base stations
Expand Down Expand Up @@ -282,15 +288,16 @@ function getclient() {
'error: host not set',
'',
'search for hosts with `hue search`',
'then run with `-H <host>` or create a config file with `--init`',
'then run with `-H <host>`',
].join('\n'));
process.exit(1);
}

// create the client
var client = Hue.createClient({
stationIp: config.host,
appName: app
appName: app,
username: config.username
});
return client;
}
Expand Down Expand Up @@ -330,3 +337,10 @@ function hex2rgb(hex) {
return parseInt(h + '' + i, 16)
}
}

function statPath(path) {
try {
return fs.statSync(path);
} catch (ex) {}
return false;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"posix-getopt": "~1.0.0",
"hue.js": "https://github.com/bahamas10/hue.js/tarball/dave-1432672068",
"hue.js": "https://github.com/bahamas10/hue.js/tarball/master",
"latest": "~0.1.1",
"css-color-names": "0.0.0",
"extsprintf": "~1.0.2"
Expand Down

0 comments on commit 43239de

Please sign in to comment.