Hey guys, I just started dipping my toes into the HTTP API and spent the day making a little Python script which cycles through scenes. It works pretty well, but the bulb sometimes responds with status: timed out.
I made this script to cycle through all the colors the light can produce, skipping over the ranges in which the light doesn’t change color. This makes for a smoother, constantly changing color — at least most of the time (i.e. skips when the bulb times out).
Please note that this script saves the log to the root of the drive because I’m lazy. Happy to hear recommendations for logging.
Here’s the source code minus my token (I know what you guys would do ):
import requests
import logging
from time import sleep
def main():
logging.basicConfig(filename="lifxscript.log", level=logging.INFO)
token = "PUT YOUR TOKEN HERE"
headers = {
"Authorization": "Bearer %s" % token,
}
def activate(scene):
response = requests.put('https://api.lifx.com/v1/scenes/scene_id:%s/activate' % scene, headers=headers)
print(response.content)
logging.info(response.content)
while(True):
activate("SCENE UUID GOES HERE")
sleep(0.5)
activate("SCENE UUID GOES HERE")
sleep(3)
activate("SCENE UUID GOES HERE")
sleep(0.5)
activate("SCENE UUID GOES HERE")
sleep(4)
activate("SCENE UUID GOES HERE")
sleep(0.5)
activate("SCENE UUID GOES HERE")
sleep(4)
if __name__ == '__main__':
main()
This code is mostly derived from the examples in the documentation. I plan on eventually porting this over to use LIFX-PHOTONS-CORE to push packets over LAN rather than relying on LIFX webhooks. I’m thinking this may improve the responsiveness of the light and reduce the occurrence of time outs — if you know anything about this, I’d love to get your input.
Please let me know if you have any recommendations! I’m excited to hear what you guys have to say.