Node TRÅDFRI API - Node.js Argon (v4.x+) supported
Node API to control IKEA TRÅDFRI (TRADFRI) Lights. Tested on node version 4.4.7
The package is a refactoring for older versions of Node.js (from 4.x) support of node-tradfri by morzzz007
- Every method returns a Promise. See example under
Usage
for a detailed description - Package has been tested on Node.js v4.4.7
- P-throttle is used to make sure that all CoAP client calls are in series (to avoid execution blocking)
- RSVP lib is used to manage Promises in the library
- Thanks morzzz007 for creating node-tradfri. It made it fairly simple to increased the backlevel support
Installation
npm install node-tradfri-argon --save
CoAP
This library uses libcoap with tinydtls to send CoAP requests. Build instructions are described below see this section. and set the coapClientPath
config setting to point to your library.
Usage
var tradfri = ; /* First time you need to register the connection and the result is used in subsequent calls. Make sure that you save the preshared key that is returned. It is unique per `identity` as specifid in when creating the tradfri object as above */ tradfri; tradfri; // or tradfri;
API
Basics
Every exposed method is asynchronous and returns a promise.
Using the typical promises approach:
tradfri;
Public API List
Devices | Groups |
---|---|
getDeviceIds() | getGroupIds() |
getDevice() | getGroup() |
getDevices() | getGroups() |
getAll() | |
turnOnDevice() | turnOnGroup() |
turnOffDevice() | turnOffGroup() |
toggleDevice() | toggleGroup() |
setDeviceState() | setGroupState() |
see this section)
Methods for working indivudial devices/bulbs (for groupsgetDeviceIds()
Returns device id's.
Response:
65536 65537 65538
<deviceId>
)
getDevice(Returns details on one specific device.
Response:
id: 65536 name: 'TRADFRI remote control' type: 'TRADFRI remote control' on: false
getDevices()
Returns an array with every device connected to the hub.
Example:
id: 65536 name: 'TRADFRI remote control' type: 'TRADFRI remote control' on: false id: 65537 name: 'TRADFRI bulb E27 WS opal 980lm' type: 'TRADFRI bulb E27 WS opal 980lm' on: false id: 65538 name: 'TRADFRI bulb E27 WS opal 980lm 2' type: 'TRADFRI bulb E27 WS opal 980lm' on: false
<deviceId>
)
turnOnDevice(Parameters | type | values |
---|---|---|
deviceId |
required | int/string |
<deviceId>
)
turnOffDevice(Parameters | type | values |
---|---|---|
deviceId |
required | int/string |
<deviceId>
, <state>
)
toggleDevice(Parameters | type | values |
---|---|---|
deviceId |
required | int/string |
state |
optional | boolean |
<deviceId>
, <newState>
)
setDeviceState(Examples
Turn device on:
tradfri;
Combine settings, turn on and set brightness:
tradfri;
Usage
Parameters | type | values |
---|---|---|
deviceId |
required | int/string |
newState |
required | object |
In newState you can combine the following values:
Parameters | values | action |
---|---|---|
state |
boolean/string ('on', 'off', 'toogle') | light on/off/toggle |
color |
string (hex color value, ex: 'efd275' or 'cool', 'normal', 'warm') | Sets color |
brightness |
number/string [0-255] | Sets brightness |
mired |
number [40-600] for RGB bulbs, [250-454] for white spectrum bulbs | Sets color temperature |
transitionTime |
number in seconds/10 | Sets time to transition from one state to another |
Methods for working with groups
getGroupIds()
Returns group id's.
Response:
150429
<groupId>
)
getGroup(Returns group id's.
Response:
id: 150429 name: 'Kitchen' devices: 65536 65537 65538 on: false
getGroups()
Returns an array of groups with the devices in it.
Response:
id: 150429 name: 'Kitchen' devices: 65536 65537 65538 on: false id: 150428 name: 'Livingroom' devices: 65531 65532 on: true
getAll()
Returns an array of groups with the devices in it including detailed device information.
Response:
id: 150429 name: 'Kitchen' devices: id: 65536 name: 'TRADFRI remote control' type: 'TRADFRI remote control' on: false id: 65537 name: 'TRADFRI bulb E27 WS opal 980lm' type: 'TRADFRI bulb E27 WS opal 980lm' on: false id: 65538 name: 'TRADFRI bulb E27 WS opal 980lm 2' type: 'TRADFRI bulb E27 WS opal 980lm' on: false on: false ...
<groupId>
)
turnOnGroup(Parameters | type | values |
---|---|---|
groupId |
required | int/string |
<groupId>
)
turnOffGroup(Parameters | type | values |
---|---|---|
groupId |
required | int/string |
<groupId>
, <state>
)
toggleGroup(Parameters | type | values |
---|---|---|
groupId |
required | int/string |
state |
optional | boolean |
<groupId>
, <newState>
)
setGroupState(Examples
Turn group on:
tradfri;
Combine settings, turn on and set brightness:
tradfri;
Usage
Parameters | type | values |
---|---|---|
groupId |
required | int/string |
newState |
required | object |
In newState you can combine the following values:
Parameters | values | action |
---|---|---|
state |
boolean/string ('on', 'off') | Toggle light on/off |
color |
string (hex color value, ex: 'efd275') | Sets color |
brightness |
number/string (0-255) | Sets brightness |
Compiling libcoap
A number of versions are available in the lib folder
but if it is required to recompile the library the instructions are below.
Linux
Install libcoap as descibed below for Debian/Ubuntu/Raspbian: (credits to homebridge-tradfri)
$ apt-get install libtool git build-essential autoconf automake
$ git clone --recursive https://github.com/obgm/libcoap.git
$ cd libcoap
$ git checkout dtls
$ git submodule update --init --recursive
$ ./autogen.sh
$ ./configure --disable-documentation --disable-shared
$ make
OSX
Use brew to install the prerequisites and then clone the git repo and build the library as described below.
$ brew install libtool
$ brew install automake
$ brew install autoconf
$ git clone https://github.com/obgm/libcoap.git
$ cd libcoap
$ git checkout origin/dtls
$ git checkout -b dtls
$ git submodule update --init ext/tinydtls
$ cd ext/tinydtls
$ autoreconf
$ ./configure
$ cd ../../
$ ./autogen.sh
$ ./configure --disable-shared --disable-documentation
$ make
Where is the client?
After compilation you'll find the coap-client binary in ./examples
. Simply refer to the coap-client location when initiating node-tradfri-argon
. E.g.
var tradfri = ;