homebridge-better-http-rgb
homebridge-better-http-rgb copied to clipboard
What have I change to support different APIs?
Hello!
I was able to integrate 3rd party web-server which control the lights in my house, but can only turn off or turn on the lamps. Issue is that API is a bit different and I'm not able to change it.
For example, it returns status for getValues request with next string: {"zone":5,"R":25,"G":35,"B":45,"W":127}. So I have to parse it somehow. All values including brightness have to be entered as 0 to 255.
The only possibilities for set values are:
setR Red channel setG Green channel setB Blue channel setW White channel setA Overal brightness setM position of color wheel getZones gearing the configuration of all lamps and zones getValues
I'm ok to update on my own, but will appreciate guidance where to find and what to update...
First off, I'm happy that someone is expanding on this! This is EXACTLY what I was hoping to achieve with this repo.
You most important function is getServices()
. That's where the library registers all callbacks for the features.
I'll start with the example of "On/Off".
- If the bulb has on/off states, then we tell the
lightbulbService
to addCharacteristic.On
(I'm assuming it does, so that's why it's in BOTH conditions. - If the bulb permits checking of the on/off status (
this.switch.status = true
), then we bindgetPowerState()
to theget
command. - Otherwise, we just bind
setPowerState()
to theset
command.
The get
and set
commands are Homebridge protocol spec, you can write ANY function you want. I chose getPowerState()
and setPowerState()
.
if (this.switch.status) {
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
} else {
lightbulbService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
}
Want to write a new one? Well, you can, but not completely. You have to have a Characteristic that HomeBridge supports (the Characteristic is how it's presented within the client application).
So, you CAN set RGBAW colors, but you have to convert them. Because Homebridge only understands Hue and Saturation, your client application is presented with a Color Wheel. This script gets Hue and Saturation (asynchronously, so have fun with that) Characteristics.
So, you can see that when I receive a Hue/Saturation set
command, each of those functions call setRGB()
and setHue()
and setSaturation()
so I can convert (remember, you receive Hue and Saturation SEPARATELY, and asynchronously). Conversely, if the script receives a get
command, you need to convert the RGB (you received from the device) to HSL so you can update the client's color wheel appropriately, I do that in _rgbToHsl()
and return it to the client.
So, want to convert to CMYK? Just modify the get-
/set-
functions for both -Hue
and -Saturation
to use your own calculations!