Skip to content

Commit

Permalink
feat: update hue api
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Jun 28, 2023
1 parent 0135ef6 commit 734d700
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 146 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class DmxHue {
Util.exit(pkg.version, 0);
} else if (this._args._[0] === 'setup') {
return this._args.list
? this._hue.listBridges()
? this._hue.listBridges(true)
: this.setup(this._args.ip, this._args.force);
}

Expand Down
134 changes: 73 additions & 61 deletions lib/hue.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import Color from 'color';
import hue from 'node-hue-api';
import { v3 as hue } from 'node-hue-api';
import Util from './util.js';

const HueApi = hue.api;
const LightState = hue.lightStates.LightState;
const APP_DESCRIPTION = 'dmx-hue utility';

export default class Hue {
getLights() {
return this.api.lights().then(
(result) => result.lights,
() => Util.exit('No lights found')
);
async getLights() {
try {
const api = await this.api();
return api.lights.getAll();
} catch {
Util.exit('No lights found');
}
}

createLightState(r, g, b, temperature, brightness, options) {
const state = hue.lightState.create().transition(options.transition);
const state = new LightState().transition(options.transition);
const mapRange = (value, low1, high1, low2, high2) =>
low2 + ((high2 - low2) * (value - low1)) / (high1 - low1);

Expand Down Expand Up @@ -44,64 +46,82 @@ export default class Hue {
return state;
}

setLight(id, state) {
return this.api.setLightState(id, state);
async setLight(id, state) {
const api = await this.api();
return api.lights.setLightState(id, state);
}

listBridges(print) {
return hue.nupnpSearch().then(
(bridges) => {
if (print) {
for (const b of bridges) console.log(b.ipaddress);
}
async listBridges(print) {
try {
const bridges = await hue.discovery.nupnpSearch();
if (bridges.length === 0) {
console.log('No bridges found, trying slower UPnP search...');
bridges = await hue.discovery.upnpSearch();
}

return bridges;
},
() => Util.exit('No bridge found')
);
console.log(`Found ${bridges.length} bridge(s)`);

if (print) {
for (const b of bridges) {
console.log(`- ${b.ipaddress} (${b.name})`);
}
}
return bridges;
} catch {
Util.exit('No bridge found');
}
}

setupBridge(ip = null, force = false) {
async setupBridge(ip = null, force = false) {
const bridge = Util.config.get('bridge');
if (bridge && Util.config.get('user') && !force) {
console.log(`Bridge configured at ${bridge}`);
return Promise.resolve();
}

return hue
.nupnpSearch()
.then((bridges) => {
const bridge = ip
? bridges.find((b) => b.ipaddress === ip)
: bridges[0];
if (bridge !== null) {
Util.config.set('bridge', bridge.ipaddress);
console.log(`Hue bridge found at ${bridge.ipaddress}`);
return this.bridge;
}
try {
const bridges = await this.listBridges();
// TODO: if multiple bridges are found, ask the user to select one
const bridge = ip
? bridges.find((b) => b.ipaddress === ip)
: bridges[0];

if (!bridge) {
throw new Error('No bridges found');
}

Util.config.set('bridge', bridge.ipaddress);
console.log(`Hue bridge found at ${bridge.ipaddress}`);

} catch (error) {
if (ip) {
Util.config.set('bridge', ip);
console.log(`Forced Hue bridge at ${ip}`);
} else {
Util.exit('No bridges found');
}
}

throw undefined;
})
.catch(() => {
if (ip) {
Util.config.set('bridge', ip);
console.log(`Forced Hue bridge at ${ip}`);
return this.bridge;
}
try {
const user = await hue.api
.createLocal(this.bridge)
.connect()
.then((api) => api.users.createUser(APP_DESCRIPTION))
.then((user) => user.username);
Util.config.set('user', user);
console.log('Linked bridge successfully');
} catch {
Util.exit('Cannot link, press the button on bridge and try again.');
}
}

Util.exit('No bridge found');
})
.then((bridge) =>
new HueApi().registerUser(bridge, APP_DESCRIPTION).then(
(user) => {
Util.config.set('user', user);
console.log('Linked bridge successfully');
return user;
},
() =>
Util.exit('Cannot link, press the button on bridge and try again.')
)
);
async api() {
if (!this._api) {
this._api = await hue.api
.createLocal(this.bridge)
.connect(this.user);
}
return this._api;
}

get bridge() {
Expand All @@ -117,12 +137,4 @@ export default class Hue {
Util.exit('Bridge not linked, run "dmx-hue setup"')
);
}

get api() {
if (!this._api) {
this._api = new HueApi(this.bridge, this.user);
}

return this._api;
}
}
Loading

0 comments on commit 734d700

Please sign in to comment.