jknlsn.

Fun with LifX lights and Python

Cover Image for Fun with LifX lights and Python

I quite like my LIFX lights. I went a long time without seeing much benefit to smart lights, but now I love them.

When I want to kick off more of a retrowave feel for a late-night coding session, I run this script, and my office lights then oscillate through the same colour scheme as the lights on my keyboard.

from time import sleep

from lifxlan import LifxLAN
# from lifxlan import BLUE, CYAN, GREEN, LifxLAN, ORANGE, PINK, PURPLE, RED, YELLOW

def hsv_to_hsbk(h, s, v):
    hsbk = (
        int(h/255 * 65535),
        int(s/255 * 65535),
        int(v/255 * 65535),
        3500
    )

    return hsbk


def main():
    num_lights = 3

    lifx = LifxLAN(num_lights)

    # Get devices
    group = lifx.get_devices_by_group("Office")

    colors = [
        hsv_to_hsbk(14, 222, 242),
        hsv_to_hsbk(233, 218, 217),
        hsv_to_hsbk(180, 255, 233),
        hsv_to_hsbk(205, 255, 255),
        hsv_to_hsbk(255, 220, 201),
    ]
    # colors = [RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, PINK]

    group.set_power("on")

    duration = 15

    transition_time_ms = duration*1000

    while True:
        for color in colors:
            group.set_color(color, transition_time_ms, False)
            sleep(duration)


if __name__ == "__main__":
    main()

You could change the colours to the built-in colours from the LifXLAN library or any you want. Just get the HSV values. I pulled the values straight out of the keymap.c file in the source available from the Oryx site for my keyboard layout.

Pretty cool.

PS: This isn't an ad but get in touch if someone at LIFX wants to hook me up with a referral code to drop here.

References

lifxlan Python 3 module

Examples, including rainbow.py