anthturner / TPLinkSmartDevices

C# Library for Discovering and Operating TP-Link Smart Devices (HS100/105/110, LB100/110/120/130)
Apache License 2.0
40 stars 16 forks source link

Changing HSV not working #4

Open pekempy opened 5 years ago

pekempy commented 5 years ago

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)

anthturner commented 5 years ago

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;
pekempy commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@TheBauwssss any ideas on the above?

TheBauwssss commented 5 years ago

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?

pekempy commented 5 years ago

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

TheBauwssss commented 5 years ago

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

SimonOrdo commented 5 years ago

@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

joedemax commented 2 years ago

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.