Skip to content

Javascript client for communicating with Fibaro HomeCenter2 smart home controllers.

License

Notifications You must be signed in to change notification settings

inwaar/fibaro-home-center2-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fibaro HomeCenter2 client

Build Status

Javascript client for communicating with Fibaro HomeCenter2 smart home controllers.

Requirements

  • NodeJS (>=10)
  • Fibaro Home Center 2

Installation

yarn add fibaro-home-center2-client

or

npm install --save fibaro-home-center2-client

Usage

Get rooms:

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getRooms().subscribe((rooms) => {
   console.log(rooms);
});

Get all available devices:

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
   console.log(devices);
});

Poll devices properties' updates:

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.events().subscribe((event) => {
   console.log(event);
});

Control devices:

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
   const light = devices.find(device => device.identifiers.includes('kitchen/lights/main-light'));
   light.turnOff().subscribe(() => {
       console.log('Kitchen light has turned off');
   });
});

Control devices based on events from other devices:

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
    const fan = devices.find(device => device.identifiers.includes('bedroom/climate/fan'));

    client.events().subscribe((event) => {
        if (event.identifiers.includes('bedroom/climate/temperature') && event.property === 'value') {
            if (event.newValue > 22) {
                fan.turnOn().subscribe();
            } else {
                fan.turnOff().subscribe();
            }
        }
    })
});

Dump all devices' identifiers

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
    const map = {};
    for (const device of devices) {
        for (const identifier of device.identifiers) {
            map[identifier] = device.id;
        }
    }

    for (const key of Object.keys(map).sort()) {
        console.log(key + ': ' + map[key]);
    }
});

List all powered devices

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
    devices
        .filter(device => device.properties.power > 0)
        .map(device => console.log(device.identifiers.join(', ') + ': ' + device.properties.power + 'w'));
});

Output example:

hall/lights/lamp: 39.4w
garage/lights/lamp: 79.1w

List all devices' actions and their possible arguments

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
    for (const device of devices) {
        console.log(device.identifiers.join(', '), device.actions);
    }
});

Output example:

...
bedroom/blinds/curtains {
    close: 0,
    open: 0,
    reconfigure: 0,
    reset: 0,
    sceneActivationSet: 0,
    setValue: 1,
    setValue2: 1,
    startLevelDecrease: 0,
    startLevelIncrease: 0,
    stop: 0,
    stopLevelChange: 0
}
bathroom/safety/flood-sensor {
    abortUpdate: 1,
    forceArm: 0,
    meetArmConditions: 0,
    reconfigure: 0,
    retryUpdate: 1,
    setArmed: 1,
    setInterval: 1,
    startUpdate: 1,
    updateFirmware: 1
}
bedroom/lights/led-strip {
    abortUpdate: 1,
    reconfigure: 0,
    reset: 0,
    retryUpdate: 1,
    setB: 1,
    setBrightness: 1,
    setColor: 1,
    setFavoriteProgram: 2,
    setG: 1,
    setR: 1,
    setValue: 1,
    setW: 1,
    startLevelDecrease: 0,
    startLevelIncrease: 0,
    startProgram: 1,
    startUpdate: 1,
    stopLevelChange: 0,
    turnOff: 0,
    turnOn: 0,
    updateFirmware: 1
}
...

API Reference

Classes

Client

Constants

CLIENT_OPTIONS

Client options

Typedefs

DevicePropertyUpdateEvent : Object
EventCriteria : Object
SystemEvent : Object
Room : Object
Device : Object

Client

Kind: global class

new Client(options)

Creates a new client.

Param Type
options CLIENT_OPTIONS | Object

Subscribe on device property update events

Kind: instance method of Client
Properties

Name Type Description
criteria EventCriteria Optional event filtering

client.system() ⇒ Observable.<SystemEvent>

Subscribe on client system events

Kind: instance method of Client

client.query(query, retry) ⇒ Observable.<Object>

Make a GET API request to HC2

Kind: instance method of Client

Param Type Default
query string
retry boolean false

client.getRooms() ⇒

Get rooms

Kind: instance method of Client
Returns: Observable<Room[]>
Example

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getRooms().subscribe((rooms) => {
   console.log(rooms);
});

client.getDevices() ⇒

Returns devices with properties and actions

Kind: instance method of Client
Returns: Observable<Device[]>
Example

const Fibaro = require('fibaro-home-center2-client');

const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.getDevices().subscribe((devices) => {
   console.log(devices);
});

client.getDevices().subscribe((devices) => {
   const light = devices.find(device => device.identifiers.includes('kitchen/lights/main-light'));
   light.turnOff().subscribe(() => {
       console.log('Kitchen light has turned off');
   });
});

client.callAction(deviceId, action, options) ⇒

Low level method to call an action on a given device. Applications are supposed to call actions on devices instead of this function.

Kind: instance method of Client
Returns: Observable

Param Type
deviceId number
action string
options array

Example

const Fibaro = require('fibaro-home-center2-client');

// call an action on an ID known device
const client = new Fibaro.Hc2Client({host: '192.168.1.69', user: 'foo', password: 'bar'});
client.callAction(21, 'turnOn');

// call an action on a device object
client.getDevices().subscribe((devices) => {
   // control light
   const light = devices.find(device => device.identifiers.includes('kitchen/lights/main-light'));
   light.turnOn().subscribe(() => {
       console.log('Kitchen light has turned on');
   });

   // control RGBW devices
   const led = devices.find(device => device.identifiers.includes('living-room/lights/rgb-led-strip'));
   led.setBrightness(50).subscribe(() => {
       console.log('Brightness set to 50');
   });
});

CLIENT_OPTIONS

Client options

Kind: global constant
Read only: true
Properties

Name Type Default Description
host string "192.168.1.69" HC2 controller ip-address
port number 80 HC2 controller port
user string "admin" username
password string user password
connectTimeout number 7000 Reconnect to controller if no success timeout
pollingInterval number 1000 Controller devices properties polling interval
pollingTimeout number 3000 Controller devices properties polling timeout
debug boolean false Trace debug information

DevicePropertyUpdateEvent : Object

Kind: global typedef
Properties

Name Type Description
id number Room id provided by HC
identifier number Generated literal identifier
property string Updated property
newValue string updated value
oldValue string previous value

EventCriteria : Object

Kind: global typedef
Properties

Name Type Description
device Device | null Subscribe only on a specific device events
properties Array.<string> | null Subscribe only on a specific property change events

SystemEvent : Object

Kind: global typedef
Properties

Name Type Description
id number Room id provided by HC
name number Room name provided by HC
identifier number Generated literal identifier

Room : Object

Kind: global typedef
Properties

Name Type Description
id number Room id provided by HC
name number Room name provided by HC
identifier number Generated literal identifier

Device : Object

Kind: global typedef
Properties

Name Type Description
id number Device id provided by HC
name number Device name provided by HC
room Room Device room
identifiers Array.<string> Generated literal identifiers
properties Object Device properties
actions Object Available device actions

License

This project is licensed under the GNU GPLv3 - see the LICENSE file for details

About

Javascript client for communicating with Fibaro HomeCenter2 smart home controllers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published