TPLinkSmartDevices icon indicating copy to clipboard operation
TPLinkSmartDevices copied to clipboard

Changing HSV not working

Open pekempy opened this issue 6 years ago • 12 comments

Prior to running any command this is what I get running tplight details [ip] "light_state": { "on_off": 1, "mode": "normal", "hue": 261, "saturation": 43, "color_temp": 0, "brightness": 45

We can see the HSV is 261, 43, 45 here.

After running the following code: smartBulb.HSV.Hue = 222; smartBulb.HSV.Saturation = 78; smartBulb.HSV.Value = 91;

(Which is #3369e8 hex code converted into HSV), if I check the details of the bulb again it's still the same colour

"light_state": { "on_off": 1, "mode": "normal", "hue": 261, "saturation": 43, "color_temp": 0, "brightness": 45

Am I using the command wrong to change values? here's the method var smartBulb = new TPLinkSmartDevices.Devices.TPLinkSmartBulb(bulbIP); smartBulb.HSV.Hue = hue; smartBulb.HSV.Saturation = sat; smartBulb.HSV.Value = val; (hue, sat, val are rounded integers passed in after converting hex to hsv)

pekempy avatar May 21 '19 10:05 pekempy

https://github.com/anthturner/TPLinkSmartDevices/blob/b4423be77b1273f83b1f61f2d1fd9a6d5dcba44c/TPLinkSmartDevices/Devices/TPLinkSmartBulb.cs#L33-L54

When changing the HSV on the bulb, all 3 properties are updated simultaneously. Because of that, the setter on the HSV property is what controls execution of the change. So here, you'd want to create a new BulbHSV object with your preferred values, then use the setter:

var preferredHsv = new BulbHSV()
{
    Hue = hue,
    Saturation = sat,
    Value = val
};
smartBulb.HSV = preferredHsv;

anthturner avatar May 21 '19 15:05 anthturner

Now it's my turn to facepalm. I was looking at it for so long and didn't even think to create a BulbHSV obj. Thanks

pekempy avatar May 21 '19 17:05 pekempy

Might have closed this prematurely.. I can see it's changing the HSV to 222/78/91 (as you see below) (roundh/rounds/roundv are rounded hue/sat/val in case that was the issue) image But the HSV shown by the nodejs package (tplink-lightbulb) still shows the purple HSV? image (it's a bulb at a friends house but he has confirmed it's still purple)

pekempy avatar May 21 '19 17:05 pekempy

@TheBauwssss any ideas on the above?

pekempy avatar May 29 '19 12:05 pekempy

I don't own one of these lights, so I can't test or try it for myself. I can see that in the Refresh() method of the TPLinkSmartBulb class the value returned for Brightness (in the BulbHSV class called Value) is not the raw JSON value returned by the Smart Light Bulb. The returned value is the JSON value modified like this:

https://github.com/anthturner/TPLinkSmartDevices/blob/b4423be77b1273f83b1f61f2d1fd9a6d5dcba44c/TPLinkSmartDevices/Devices/TPLinkSmartBulb.cs#L120

When serializing the BulbHSV object back to JSON the modification is undone by the opposite of the operation:

https://github.com/anthturner/TPLinkSmartDevices/blob/b4423be77b1273f83b1f61f2d1fd9a6d5dcba44c/TPLinkSmartDevices/Devices/TPLinkSmartBulb.cs#L50

I think you need to modify your brightness value input in a similar way. If that does not work, off the top of my head I can think of two more possible things to try:

  1. Checkout https://github.com/anthturner/TPLinkSmartDevices/commit/4bac4ceed59e7c09fcfb91ba43c3cabfe6e6def0 (before I made my changes adding support for the HS110) and see if that fixes the issue
  2. Add a breakpoint to the JSON network handler (or just use Telerik Fiddler) and inspect the JSON values the API is sending to the Smart Light Bulb. Do those values confirm to the specification?

It is late and I am tired so my English might be severely compromised, or at least it feels like it is :D Anyhow, please accept my apologies for the grammatically questionable and weird English I might have used here and there. I can't think of a better way to put it into words without convoluting my sentences.

If this does not fix your issue, could you install Fiddler and send me a copy of the JSON payload your Smart Light Bulb returns to the API (through the Refresh() method) and a copy of the JSON object the API send the Smart Light Bulb?

TheBauwssss avatar May 29 '19 22:05 TheBauwssss

I did try using Fiddler but it wasn't finding anything from my server. This is the contents of the message sent to the bulb image (this is equivalent to #ff5c40)

Following the code I can see the HSV is 240, 75, 100 in subResult after executing the code image

pekempy avatar May 30 '19 11:05 pekempy

I spent almost an hour researching and typing my previous reply, please read it carefully and follow the steps/possible solutions:

  • Did you try setting the brightness value mapped from 0 to 1 instead of 0 to 255?

  • Did you try possible solutions 1 and 2 from my previous reply?

  • If you're not seeing that JSON come along in Fiddler then something else is wrong. Please follow my possible solution 2 and send me a complete dump of the JSON message the APi is sending and the JSON response the bulb sends back.

TheBauwssss avatar Jun 05 '19 23:06 TheBauwssss

@TheBauwssss , @pekempy FYI, the below linked library works, but it's in Java. Perhaps something can be learned from it, regarding this issue.

https://github.com/openhab/openhab2-addons/tree/master/bundles/org.openhab.binding.tplinksmarthome/src/main/java/org/openhab/binding/tplinksmarthome/internal

SimonOrdo avatar Jun 17 '19 20:06 SimonOrdo

The issue is the "light_state" string being passed into the argument param here

https://github.com/anthturner/TPLinkSmartDevices/blob/b4423be77b1273f83b1f61f2d1fd9a6d5dcba44c/TPLinkSmartDevices/Devices/TPLinkSmartBulb.cs#L46

It is totally unnecessary and infact stops the state change ever beinge executed. You need to pass just the JObject as the value with no argument.

joedemax avatar Sep 12 '21 22:09 joedemax