LIFX Developer Zone

Cycle effect in python

Hi there, I have recently been trying to create a colour cycle effect for my python program (it sits on a web server and acts as a webhook for requests, primarily from Dialogflow and Google actions.) But the only documentation provided on the cycle effect is in JSON. I initially thought this would not be a problem as I would just use the default JSON module that comes with python.

So I created an action that gets triggered when I ask google to ‘start the colour cycle’ and added in a variable ‘data’ that holds all of the JSON code. Then I used the JSON.dumps(data) command before placing it in the requests.put data file.

Whenever I call this script, the code doesn’t produce an error but I get a response 405 back from LIFX HTTP API, does anyone know how to fix this? Am I completely looking at this the wrong way or can it simply not be done?

Here is the code in question:

# import flask modules
from flask import Flask, make_response, request, jsonify
from flask_assistant import ask, tell, event, build_item
from flask_assistant import Assistant, ask, tell
import requests
import json

#LIFX Token
token = "ccec958395c83f6ee06f50997ec393892a07af8baa63156f8e707c864d87cd3b"

# build the flask app
app = Flask(__name__)
assist = Assistant(app, project_id="test-f7d4c", route='/')

#Headers for the LIFX packet
headers = {"Authorization": "Bearer %s" % token,}


#Default route for the webhook
@app.route('/webhook', methods=['GET', 'POST'])
def index():
    return('this worked')

@assist.action('Change my light to a colour')
def light(colour, light):
    light = light
    light = light[0]

    data = {
        "power": 'on',
        "brightness": 100,
        "color": colour,}
    
    response = requests.put('https://api.lifx.com/v1/lights/label:' + light + '/state', data=data, headers=headers)

    print(response)
    print(colour)
    print(light)
    if response == 200 or response == 207:
        return("The light is on now")
    else:
        return("Error, try again")

@assist.action('Start a scene')
def scene(scene):
    scene = scene
    return('Start a scene')

@assist.action('Start an effect')
def effect(Effects):
    if Effects == 'cycle':
        data = {
            "states": [
                {
                "brightness": 1.0
                },
                {
                "brightness": 0.5
                },
                {
                "brightness": 0.1
                },
                {
                "power": "off"
                }
            ],
             "defaults": {
                 "power": "on", # all states default to on
                 "saturation": 0, # every state is white
                 "duration": 2.0 # all transitions will be applied over 2 seconds
             }
        }
    

        dataPython = json.dumps(data)
        response = requests.put('https://api.lifx.com/v1beta1/lights/all/cycle', data=dataPython, headers=headers)

        print(response)
        print('That was the response!')
        if response == 200 or response == 207:
            return("Effect started")
        else:
            return("Error, try again")
    else:
        return('Failure!')

# call the main function to run the flask app
if __name__ == '__main__':
   app.run(debug=True)

Here is the code that I have but please note that I have both my header and my token (also all my imports and a couple of other actions) in the file I just haven’t added them here because it would be too many lines.

Also if you would like further clarification on my question just ask…

And finally yes, if anyone is looking the at the right thing, I am using the default script that is provided in the documentation (I didn’t want to be causing the error from not doing it right as before I create this function in detail I have to play around with JSON a bit.

Thanks for the help, Toby

So to do code, do three backtick characters on their own line, followed by your code and then three backtick characters on their own line afterwards

def my_code():
  print("hi")

As for the cycle endpoint, that’s an endpoint used to cycle your lights through different colours rather than a continuous effect.

It works by looking at the colours of your lights and determining which colour in your request is the next colour to set them to.

If you want a waveform effect then you want the breathe or pulse effects.

Also, you’re getting a 405 (which means Method not allowed) because you’re doing a requests.put instead of a requests.post

Ah right, I’m an idiot :joy: I wasn’t sure if this would work as a colour cycle effect but none the less I will probably implement it somehow, is there a way to make a custom colour cycle effect in python? Thanks so much, Toby

You have to keep telling the device to change colour for that. So essentially what you’re doing, but every x seconds call the cycle endpoint again.

If your server is on the same network as your device you could use the LAN protocol with LifxLAN or photons so you can call it more often and not get rate limited.

For example, https://gist.github.com/delfick/3c2c9085f290134584f9b4c0c7613e70