Git Product home page Git Product logo

hue-connect's Introduction

Hue-connect

Easy setup for Philips Hue apps

GitHub workflow npm

Project Status

โ›” UNMAINTAINED

This library desperately needs a makeover, but 3rd party apps and integrations became good enough that I no longer see value in maintaining my own tooling.

Why

Because bridge discovery is weird and so is attaining an API token.

Installing

It's published on npm.

$ npm install --save hue-connect

Now you can use it in your project. It works with ES Module syntax or plain ol' require.

// ES Modules
import discover from 'hue-connect'

// CommonJS
const discover = require('hue-connect')

API

The easiest way to use this module is using the hue-register command:

$ hue-register my-app-name > auth.json

Otherwise there's the programmatic approach. There are two functions exported from this module:

  • discover(callback)
  • Bridge

Discovery

The main export from hue-connect is a function. Pass it a callback and it'll call it with every bridge it can find.

import discover from 'hue-connect'

const search = discover(bridge => {
  console.log('Found bridge:', bridge)
})

Once you no longer care about finding new bridges (like once you've attained a token), stop the bridge search using the .cancel() method.

const search = discover(bridge => {})

search.cancel()

Note: Bridge discovery works because this other module is amazing.

Bridge

A bridge is a simple way to get an API token from a hue bridge.

If you know the bridge IP address already, you can skip discovery and create an instance directly.

Bridges are created automatically when using discover().

import { Bridge } from 'hue-connect'

const bridge = new Bridge({
  ip: '192.168.1.42',
})

bridge.connect()

Attaining an API token is pretty straightforward. Call the .connect method and it returns a promise. If the user pressed the bridge button, you get a token.

bridge.connect({
  appName: 'the-name-of-your-app',
  deviceName: 'the-computer-name-or-whatevs',
})

Note: It wants the app and device name because Hue likes that stuff. If you're just experimenting, you can make something up. But don't tell Hue I said that.

Assuming the user has pressed the button, the promise will resolve with your shiny new API token.

bridge.connect({
  appName: 'illumination',
  deviceName: 'bob',
}).then(token => {
  console.log('Awesome sauce!', token)
})

Otherwise the promise will reject horribly.

bridge.connect({ appName, deviceName }).catch(error => {
  console.log('Oh noes:', error.message)
  console.log('Secret error code:', error.code)
})

All the error codes are in this giant table, but you might need to create a hue account to see 'em. The most common is 101 which means the user is lazy and just hasn't pressed the button.

Examples

So you've seen all the docs. Here's an example to tie it all together.

This'll find all the local bridges and ping them once a second until the user presses the bridge button.

const discover = require('hue-connect')

const search = discover(getToken)

async function getToken (bridge) {
  try {
    const token = await bridge.connect({
      appName: 'enlightened',
      deviceName: 'bad-pun-generator',
    })

    console.log('Success!')

    console.log({
      bridge: bridge.ip,
      token: token,
    })

    // Stop searching for new bridges.
    search.cancel()
    process.exit(0)
  } catch (error) {

    // It's some weird error.
    if (error.code !== 101) {
      throw error
    }

    // User hasn't pressed the bridge button.
    // Try again in a second.
    setTimeout(getToken, 1000, bridge)
  }
}

hue-connect's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar psychollama avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hue-connect's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Dependency Dashboard

This issue contains a list of Renovate updates and their statuses.

This repository currently has no open or pending branches.


  • Check this box to trigger a request for Renovate to run again on this repository

Possible edge case with multiple bridges in hue-register command

Hey, I was looking through the code of this project and I came across a possible issue.

This line causes the register callback to call itself until the link button is pressed, but it does not account for multiple bridges. https://github.com/PsychoLlama/hue-connect/blob/master/src/bin/hue-register.js#L35

As far as I can tell, search.cancel just prevents callbacks from being spawned for newly found bridges, it does not break the loop in existing callbacks. This means that even if another callback has already linked to a bridge, the process will keep running until it's linked to all the bridges it discovered.

The same issue affects the code in the readme.

A simple fix would be to call process.exit after search.cancel, but if you wanna get fancy you can probably implement something with cancelable promises or futures, which can also be canceled.

Implement discovery without a library

The library that powers SSDP is pretty old and Hue offers better methods these days. If there's more modern alternatives, I should add those or replace the library with something easier to maintain.

'Modernize' the library

Where 'modernize' is a euphemism for "fix this abominable trash". Ignoring the poor design, I think parts of it might not even work anymore. It needs a makeover.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.