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

What have I change to support different APIs? #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

b8ka
Copy link

@b8ka b8ka commented Jan 29, 2018

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...

@b8ka b8ka changed the title What have OI change to support different APIs? What have I change to support different APIs? Jan 29, 2018
@jnovack
Copy link
Owner

jnovack commented Jan 29, 2018

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 add Characteristic.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 bind getPowerState() to the get command.
  • Otherwise, we just bind setPowerState() to the set 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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants