Supereg / homebridge-http-lightbulb

Powerful http lightbulb for Homebridge: https://github.com/homebridge/homebridge
ISC License
23 stars 7 forks source link

Brightness API expects integer for request body but can only pass string #13

Closed TomAldy closed 3 years ago

TomAldy commented 3 years ago

I am using this package to interact with a Phillips Hue bulb that is on a bridge attached to another Home, I am on the same network so can call the bridge's API to control the bulb I care about on my private Home.

I have got the on/off functionality working, but the bulb is dimmable and so would like to control the brightness, I am sending this body through via the plugin to the Hue Bridge API....

{ "on": true, "bri": 220 }

Obviously 220 reflects the brightness level of the bulb, but the issue comes in when I am trying to set the plugin up via config with this...

"brightness": {
    "setUrl": {
        "url": "http://x.x.x.x/api/sfjsbjfbsf/lights/19/state",
        "method": "PUT",
        "body": {
            "on": true,
            "bri": "%s"
        }
    },
    "unit": "rgb"
},

The issue is that I'm getting an error due to the brightness input being embedded in the body as a string, compared to a number/integer, the Phillips Hue API expects an integer. I am not intending to update this plugin locally usig npm because it does everything I need except this, so I am happy to edit the source code in my local version if need be.

I have taken a look into the homebridge-http-base source code and that appears to force it to be a string rather than a type by choice.

Any help would be appreciated and just want to say, I love this plugin.

Supereg commented 3 years ago

The bri property is a string because you define it as such.

See, your setUrl.body is defined as

{
  "on": true,
  "bri": "%s", // See here, your putting `"` around %s, meaning bri will in any case be a string.
}

Instead of supplying the body as a json object, you may just format it as a string in order to be able to remove the quotes from bri.

"brightness": {
    "setUrl": {
        "url": "http://x.x.x.x/api/sfjsbjfbsf/lights/19/state",
        "method": "PUT",
        "body": " {\"on\": true, \"bri\": %s}", // body is now a string of json; note the `\` which need to be used before `"`, so you can use them inside a string
    },
    "unit": "rgb"
},