studioimaginaire / phue

A Python library for the Philips Hue system
Other
1.53k stars 266 forks source link

Inconsistent behaviour #52

Open ultima-originem opened 9 years ago

ultima-originem commented 9 years ago

I'm trying to use phue from a Raspberry Pi but can't get my script to work as designed. I've stripped my program to execute just a few lines [after providing credentials, etc.] to illustrate the problem:


print hue.set_light(2, {'on': True, 'bri': 20, 'transitiontime': 50})  # transitiontime is ignored
time.sleep(1)
print hue.set_light(2, {'bri': 200, 'transitiontime': 100})  # completely ignored
time.sleep(1)
print hue.set_light(2, {'on': False, 'transitiontime': 200})  # completely executed

The effect on the lamp is described in the comments. Output on the screen is as follows:

[[{u'success': {u'/lights/2/state/transitiontime': 50}}, {u'success': {u'/lights/2/state/on': True}}, {u'success': {u'/lights/2/state/bri': 20}}]]
[[{u'success': {u'/lights/2/state/transitiontime': 100}}, {u'success': {u'/lights/2/state/bri': 200}}]]
[[{u'success': {u'/lights/2/state/transitiontime': 200}}, {u'success': {u'/lights/2/state/on': False}}]]

A significant part of the code is ignored or at least has no visible effect on the lamp. To be absolutely certain that the bridge doesn't get too many commands in a short time, I've used a 1 second delay between issuing commands.

I've tried many different combinations of commands to single out the problem of which the code shown is the simplest. Is it my flawed understanding of phue or something else? Any help will be appreciated.

DeastinY commented 8 years ago

Hey ot3, I've create a FLASK application for my Pi and I've been running it for a couple of weeks now. No issues whatsover. Didn't play with transitiontime yet, but other than that it works great from a Pi. Is yours connected via cable or wifi ? This might be part of the issue. Maybe try running it from a laptop or desktop and see if the errors persist.

demosdemon commented 8 years ago

@ot3 I'm not entirely sure the bridge was ignoring the last two of your requests. I would like to know what the initial state of your light is before you executed these commands.

The bridge will set brightness as low as possible when transitiontime is provided and the state is currently off and the new state is on. So, when you specify {'on': True, 'bri': 20, 'transitiontime': 50} you are telling the bridge to do three things:

  1. Turn the light on, if the light is already--noop. If the light is off, turn it on leaving last known state.
  2. Set the brightness to 20
  3. Execute task over 5 seconds (default of 40ms)
    1. Because we are transitioning from Off -> On, adjust initial brightness to 1.

Your bridge is now trying to adjust the brightness from 1 to 20 over 5 seconds, a very subtle effect.

The second command comes in while the first command is still executing. The bridge has two choices to make, ignore the request or abandon the previous request. But, you are telling the bridge to adjust from whatever the current brightness is (somewhere between 1 and 20) to 200 over a duration of 10 seconds. The bridge abandon's the previous request and starts to increase the brightness to 200. The speed depends on the initial brightness, but it will still be very slow.

The third command now is telling the bridge to turn off the light and take 20 seconds to do it. The bridge abandons the previous request in progress and transitions the brightness from wherever it is between 1 and 200 back to 1, then off.

You may want to try a more obvious approach to testing the transitiontime, your commands as listed may be too subtle to notice.

You may also want to enable logging and make sure that phue is actually posting your request to the bridge.

ultima-originem commented 8 years ago

Thanks for your suggestion. I haven't gotten back to the code since I posted my question so will have to re-familiarize myself again but your suggestion makes sense.