0.3.0 • Published 5 years ago

fibaro-home-center2-client v0.3.0

Weekly downloads
5
License
GPL-3.0
Repository
github
Last release
5 years ago

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

Constants

Typedefs

Client

Kind: global class

new Client(options)

Creates a new client.

ParamType
optionsCLIENT_OPTIONS | Object

client.events() ⇒ Observable.<DevicePropertyUpdateEvent>

Subscribe on device property update events

Kind: instance method of Client
Properties

NameTypeDescription
criteriaEventCriteriaOptional 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

ParamTypeDefault
querystring
retrybooleanfalse

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

ParamType
deviceIdnumber
actionstring
optionsarray

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

NameTypeDefaultDescription
hoststring"192.168.1.69"HC2 controller ip-address
portnumber80HC2 controller port
userstring"admin"username
passwordstringuser password
connectTimeoutnumber7000Reconnect to controller if no success timeout
pollingIntervalnumber1000Controller devices properties polling interval
pollingTimeoutnumber3000Controller devices properties polling timeout
debugbooleanfalseTrace debug information

DevicePropertyUpdateEvent : Object

Kind: global typedef
Properties

NameTypeDescription
idnumberRoom id provided by HC
identifiernumberGenerated literal identifier
propertystringUpdated property
newValuestringupdated value
oldValuestringprevious value

EventCriteria : Object

Kind: global typedef
Properties

NameTypeDescription
deviceDevice | nullSubscribe only on a specific device events
propertiesArray.<string> | nullSubscribe only on a specific property change events

SystemEvent : Object

Kind: global typedef
Properties

NameTypeDescription
idnumberRoom id provided by HC
namenumberRoom name provided by HC
identifiernumberGenerated literal identifier

Room : Object

Kind: global typedef
Properties

NameTypeDescription
idnumberRoom id provided by HC
namenumberRoom name provided by HC
identifiernumberGenerated literal identifier

Device : Object

Kind: global typedef
Properties

NameTypeDescription
idnumberDevice id provided by HC
namenumberDevice name provided by HC
roomRoomDevice room
identifiersArray.<string>Generated literal identifiers
propertiesObjectDevice properties
actionsObjectAvailable device actions

License

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

0.3.0

5 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago