PhilipsHue / PhilipsHueSDK-Java-MultiPlatform-Android

The Software Development Kit for Philips Hue Java Mulfi-Platform and Android (beta)
273 stars 214 forks source link

Updating LightState with 'on' value fails unintuitively #14

Closed J-Swift closed 9 years ago

J-Swift commented 9 years ago

In the application I'm working on, I send state updates in 'full frames'. That is, I set the 'on', 'transitiontime', 'hue', 'saturation', and 'brightness' in one call. I've found that when I try to do this for a light which is off, I need to initially send an 'on' update with transtiontime of 0 in order to then get the other updates to work well. I do that using something like the following:

final PHLightState state = light.getLastKnownLightState();
final Runnable afterOnRunnable = new Runnable() {
    public void run() {
        state.setHue(hue);
        state.setSaturation(saturation);
        state.setBrightness(brightness);
        state.setTransitionTime(transitionTime);
        bridge.updateLightState( light, state );
    }
}

if ( state.isOn() ) {
    afterOnRunnable.run();
    return;
}

state.setTransitionTime( 0 );
state.setOn( true );
bridge.updateLightState( light, state, new PHLightListener() {
    public void onSuccess() {
        afterOnRunnable.run();
    }
    ...
});

I've found that if the light is already on then this fails in the runnable's call to updateLightState, presumably because I'm trying to 're-set' the on value. When I log out the 'onError' callback information it says:

code: 6
message: parameter, reachable, not available

If I change the code to use a new PHLightState within the runnable, and take care not to set an on value, then it runs fine. This seems to go against the note in the documentation that you can update the light state by 1) retrieving the last known light state 2) updating the value you want to change and 3) sending the update to the bridge.

Am I wrong in my understanding of how to go about updating these values, or is this something that could be addressed?

SteveyO commented 9 years ago

Hi there,

Yes, you can update the light state by either creating a new PHLightState object, or by getting the lastKnownLightState from the light object, but there is a subtle difference. When the SDK serializes the PHLightState object all non-null light state attributes are sent to the bridge (in the JSON to update the state). This is probably why you are finding that the 'on' value is getting re-set.

I would always recommend to create a new PHLightState object which is better for 2 reasons. 1 - it will update quicker as you will send less attributes to the bridge (and in turn less ZigBee commands between the bridge and the lights) and 2 - its gives you greater control as you know exactly what you are updating.

Apologies if the comment in the code was misleading, in retrospect it shouldn't have been included. I have removed it and also in the Tips and Tricks section I have added a new section advising people to use 'new PHLightstate' instead of light.getLastKnownLightState.

Steve

J-Swift commented 9 years ago

OK, thanks Steve. Hopefully the new notes save someone a bit of time down the road.

SteveyO commented 9 years ago

No problem. Yeah, hopefully clearer for all now. I will close this issue now!