Funnily enough, for an entirely different project I started taking pcaps off of a router, and have seen what the Android app sends to the lights over the LAN, at least on startup. There’s a lot of redundancy. On startup the app runs discovery and collects state information several times. Presumably that helps mitigate failed responses. I didn’t do a pcap for long, so I didn’t see whether there was some kind of keep-alive interaction happening over time as well.
If you’re always going to turn on all the lights, I do recommending turning on all lights with a broadcast message (using the LifxLAN() object). Those broadcast messages are very reliable. Who knows, maybe they’ll even “wake up” the lights for unicast messages. That shouldn’t be necessary, however…
When seeing weird connection issues with these bulbs, I have found that physically powering them off, then powering them back on works an astonishing amount of the time.
If that doesn’t work, something to try in lifxlan is to increase the number of attempts from the default (one attempt, see DEFAULT_ATTEMPTS in device.py) to multiple attempts. In a place where normally you would write:
try:
power = mydevice.get_power()
except WorkflowException as e:
print(e)
Instead try:
try:
response = mydevice.req_with_resp(GetPower, StatePower, max_attempts=5)
power = response.power_level
except WorkflowException as e:
print(e)
Or, you can manually implement multiple retries yourself:
power = None
num_attempts = 0
while (num_attempts < 5 and power == None)
try:
power = mydevice.get_power()
except WorkflowException:
num_attempts += 1
(Didn’t test that code, good luck.)
It’ll take five times as long to timeout if it’s going to timeout, but at least you’ll be able to see if these issues are just due to the network dropping packets more often than one would like, or whether there is something else at play.
If the above code works, that suggests that the failures are due to the network frequently dropping packets. You can permanently increase the library’s default number of attempts by changing DEFAULT_ATTEMPTS in device.py and then re-building and re-installing the library. Beware, however, that if you ever try to write fancy fast light shows, things can get real ugly if you forget to set max_attempts=1 in any fire-and-forget calls. Not the best design, really…yet another refactor to add to the list 