Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Jul 18, 2016
0 parents commit 33f85c0
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# homebridge-fake-rgb

An homebridge plugin that create an Fake RGB Bulb HomeKit accessory

# Installation

Follow the instruction in [homebridge](https://www.npmjs.com/package/homebridge) for the homebridge server installation. The plugin is published through [NPM](https://www.npmjs.com/package/homebridge-fake-rgb) and should be installed "globally" by typing:

```bash
npm install -g homebridge-fake-rgb
```

# Configuration

Remember to configure the plugin in config.json in your home directory inside the .homebridge directory.

```json
"accessories": [{
"accessory": "Fake-RGB",
"name": "RGB Bulb"
}]
```

Configuration parameters:

- "accessory": "Fake-RGB",
- "name": "PUT THE NAME OF YOUR TEST BULB HERE",

Look for a sample config in [config.json example](https://github.com/edjopato/homebridge-fake-rgb/blob/master/config-sample.json)
17 changes: 17 additions & 0 deletions config-sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 51826,
"pin": "031-45-154"
},

"description": "Bla Bla Bla",

"accessories": [{
"accessory": "Fake-RGB",
"name": "RGB Bulb"
}],

"platforms": []
}
122 changes: 122 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var Service, Characteristic;

module.exports = function( homebridge ) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory( "homebridge-fake-rgb", "Fake-RGB", RgbAccessory );
};

function RgbAccessory( log, config ) {
this.log = log;
this.config = config;
this.name = config.name;
this.power = 0;
this.brightness = 100;
this.saturation = 0;
this.hue = 0;

this.log( "Initialized '" + this.name + "'" );
}

function rgbFromHsv( h, s, v ) {
var h_ = h / 60;
var c = v * s;
var x = c * ( 1 - Math.abs( ( h_ % 2 ) - 1 ) );

var r = v - c;
var g = v - c;
var b = v - c;

if ( h_ >= 5 ) {
r += c;
b += x;
} else if ( h_ >= 4 ) {
r += x;
b += c;
} else if ( h_ >= 3 ) {
g += x;
b += c;
} else if ( h_ >= 2 ) {
g += c;
b += x;
} else if ( h_ >= 1 ) {
r += x;
g += c;
} else {
r += c;
g += x;
}

return {
r: r,
g: g,
b: b
};
}

RgbAccessory.prototype.setColor = function() {
var color = rgbFromHsv( this.hue, this.saturation / 100, this.brightness / 100 );

if ( !this.power ) {
color.r = 0;
color.g = 0;
color.b = 0;
}

this.log( "set color to", Math.round( color.r * 255 ), Math.round( color.g * 255 ), Math.round( color.b * 255 ) );
};

RgbAccessory.prototype.getServices = function() {
var lightbulbService = new Service.Lightbulb( this.name );
var bulb = this;

lightbulbService
.getCharacteristic( Characteristic.On )
.on( 'get', function( callback ) {
callback( null, bulb.power );
} )
.on( 'set', function( value, callback ) {
bulb.power = value;
bulb.log( "power to " + value );
bulb.setColor();
callback();
} );

lightbulbService
.addCharacteristic( Characteristic.Brightness )
.on( 'get', function( callback ) {
callback( null, bulb.brightness );
} )
.on( 'set', function( value, callback ) {
bulb.brightness = value;
bulb.log( "brightness to " + value );
bulb.setColor();
callback();
} );

lightbulbService
.addCharacteristic( Characteristic.Hue )
.on( 'get', function( callback ) {
callback( null, bulb.hue );
} )
.on( 'set', function( value, callback ) {
bulb.hue = value;
bulb.log( "hue to " + value );
bulb.setColor();
callback();
} );

lightbulbService
.addCharacteristic( Characteristic.Saturation )
.on( 'get', function( callback ) {
callback( null, bulb.saturation );
} )
.on( 'set', function( value, callback ) {
bulb.saturation = value;
bulb.log( "saturation to " + value );
bulb.setColor();
callback();
} );

return [ lightbulbService ];
};
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "homebridge-fake-rgb",
"version": "0.0.1",
"description": "Fake RGB Bulb plugin for homebridge: https://github.com/nfarina/homebridge",
"license": "MIT",
"keywords": [
"homebridge-plugin"
],
"engines": {
"node": ">=0.12.0",
"homebridge": ">=0.2.0"
},
"author": {
"name": "Edgar Toll"
},
"repository": {
"type": "git",
"url": "git://github.com/edjopato/homebridge-fake-rgb.git"
},
"dependencies": {}
}

0 comments on commit 33f85c0

Please sign in to comment.