Node.js module to interact with Dirigera hub from Ikea smart home system as of year 2023
npm install node-dirigera-promisejs
import DirigeraHub from 'node-dirigera'
// OR
const DirigeraHub = require('node-dirigera')
// First time:
const options = { hubAddress: '192.1.1.2', debug: 5, clientName: 'test-node-dirigera' }
// Next runtime (with accessToken)
const options = { hubAddress: '192.1.1.2', debug: 5, access_token: accessToken, clientName: 'test-node-dirigera' }
const dirigeraHub = new DirigeraHub(options)
`
getAccessToken
How to get an access token
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2'})
// The next command can take up to 1 minute as it will be waiting for you to push the button.
// it will continuously check the hub for a push. Once the button is pressed you will get the access token.
// If you don't make it to the hub an error is thrown(promise reject).
try {
accessToken = await dirigeraHub.getAccessToken()
console.log('All good. Here is your access token:')
console.log(accessToken)
} catch (error) {
console.log(error)
}
// Save accessToken to disk, .env or other config
}
start()
`
logIn - is also called automatic on relevant calls. This can be used at start of the process to confirm a working access token or to start request a new one.
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
try {
await dirigeraHub.logIn()
console.warn('Login success!')
} catch (error) {
console.warn('Login failed. Access token might be invalid')
}
}
start()
`
Return device details
getDevice
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// device = 'bedroom light' // define by given name
// device = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its ID
// device = null/undefined - returns all devices
devices = await dirigeraHub.getDevice(device)
console.log(devices)
// Save accessToken to disk, .env or other config
}
start()
`
setDevice
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// device = 'bedroom blinds' // define by given name
// device = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its ID
const attribute = 'blindsTargetLevel' // Allowed values can be found via getDevice() under capabilities.canReceive
const value = 0 // 0 = open, 100 = close
await dirigeraHub.setDevice(device, attribute, value)
}
start()
`
getRoom
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// room = 'Bedroom' // define by given name
// room = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its room ID
// room = '' or null // will return devices without rooms
const room = 'Bedroom'
const deviceType = 'blinds' // Optional: Limit to specific device type
dirigeraHub.getRoom(room, deviceType)
}
start()
`
setRoom
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// room = 'Bedroom' // define by given name
// room = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its room ID
const room = 'Bedroom'
const value = 0 // 0 = open, 100 = close
const deviceType = 'blinds' // Optional: Limit to specific device type
const result = await dirigeraHub.setRoom(room, attribute, value,deviceType)
// result = { ok: [list of devices handled ok], errors: [list of errors if any] }
}
start()
`
Return device details
getDevice
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// device = 'Night time' // define by given name
// device = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its ID
// device = null/undefined - returns all devices
devices = await dirigeraHub.getScene(device)
console.log(devices)
// Save accessToken to disk, .env or other config
}
start()
`
setDevice
`js
async function start() {
const dirigeraHub = new DirigeraHub({ hubAddress: '192.1.1.2', access_token: 'insertAccessTokenHere'})
// device = 'bedroom blinds' // define by given name
// device = 'b8f3f21e-0fec-4b20-8de4-7fe7f7' // Define by its ID
await dirigeraHub.triggerScene(device)
}
start()
``