r/PythonLearning • • Jun 10 '25

Showcase First python "project"

I recently switched to Linux. Since Elgato software is not available for linux, I wrote this little script to toggle my Keylight. I use this within the streamdeck-ui application to comfortably turn my light on and off or change the brightness.

45 Upvotes

10 comments sorted by

View all comments

4

u/togares Jun 10 '25

I wrote this at 1am and just now realize how unhinged parts of this are

2

u/unvaccinated_zombie Jun 11 '25 edited Jun 11 '25

I have got aspired and wrote a simple simulation with a flask backend and it works.

# app.py
import configparser


from flask import Flask, request

app = Flask(__name__)
config = configparser.ConfigParser()
config.read("lightbulb.ini")

class Bulb:
    def __init__(self, on_off, brightness):
        self.on_off = on_off
        self.brightness = brightness

    def __dict__(self):
        return {
            "lights": [
                {
                    "on": self.on_off,
                    "brightness": self.brightness
                }
            ]
        }

@app.route("/")
def index():
    print(bulb.__dict__())
    return bulb.__dict__()  # you can check out the value at the http://localhost:5000/ too


@app.route("/", methods=["PUT"])
def update_light():
    print("Updating...")
    bulb.on_off = request.json["lights"][0]["on"]
    config["light"]["on"] = str(bulb.on_off)
    bulb.brightness = request.json["lights"][0]["brightness"]
    config["light"]["brightness"] = str(bulb.brightness)
    with open("lightbulb.ini", "w", encoding="utf-8") as f:
        config.write(f)  # this will update the config file with the two values
    return "Update"

if __name__ == "__main__":
    bulb = Bulb(int(config["light"]["on"]), int(config["light"]["brightness"]))
    app.run(debug=True)
; lightbulb.ini
[light]
on = 1
brightness = 60