Skip to content

Instantly share code, notes, and snippets.

@SergioSV96
Last active December 18, 2023 21:30
Show Gist options
  • Save SergioSV96/4118b7175ec16f953eb23a753af3a399 to your computer and use it in GitHub Desktop.
Save SergioSV96/4118b7175ec16f953eb23a753af3a399 to your computer and use it in GitHub Desktop.
A Hyperion UDP listener for LIFX bulbs! Just "pip install lifxlan --user" and run with "python3 hyperion_lifx.py PORT NAME"
import sys
import socket
import colorsys
import argparse
from lifxlan import LifxLAN
def search_lifx_bulb(name, lifx):
"""
Returns the LIFX device for the selected bulb name
"""
print("Discovering lights...")
# get devices
devices = lifx.get_lights()
print("\nFound {} light(s):\n".format(len(devices)))
for i, device in enumerate(devices):
try:
print(device.get_label())
if device.get_label() == name:
return device
except:
pass
sys.exit("Device not found")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Connect a LIFX bulb with Hyperion.ng')
parser.add_argument('PORT', type=int, help='the hyperion.ng udpraw port selected')
parser.add_argument('NAME', type=str, help="the LIFX bulb's name")
args = parser.parse_args()
# Arguments
port = args.PORT
name = args.NAME
# Connection to LIFX
lifx = LifxLAN()
device = search_lifx_bulb(name, lifx)
"""
SOCKET UDP CLIENT
"""
IP = "127.0.0.1" # LOCAL IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((IP, port))
print("waiting on port:", port)
packet = bytearray(3)
while True:
data = s.recvfrom_into(packet)
hsv = colorsys.rgb_to_hsv(packet[0], packet[1], packet[2])
hsvk = [hsv[0] * 65535, hsv[1] * 65535, hsv[2] * 257, 3500]
device.set_color(hsvk, 0, True)
@RoldyBuffalo
Copy link

RoldyBuffalo commented Jan 9, 2021

Correct me if I'm wrong here, but this just displays a single color across the bulb?

I got it working if so, if It's meant to broadcast more then one color however, it is not working as intended, or I have messed something up. I'm specifically talking about a Lifx Z-strip.

@SergioSV96
Copy link
Author

Correct me if I'm wrong here, but this just displays a single color across the bulb?

I got it working if so, if It's meant to broadcast more then one color however, it is not working as intended, or I have messed something up. I'm specifically talking about a Lifx Z-strip.

Correct, I made this script for a single LIFX bulb, I assume LIFX Z-Strip would work as several bulbs but the script will need to be changed (probably a lot) if you want each individual LED to change in different colors at the same time, let me know if I can help you in any way as I don't own this type of strip but maybe I can point you into the right direction.

@RoldyBuffalo
Copy link

If I'm understanding the code correctly, and this being with minimal knowledge of hyperion; I'd guess you would just have to modify the datagram for the bulb/listener to include more data. Hyperion already supports multiple zone led control, it seems it's just a matter or connecting the dots so to speak.

The Lifx-z Strip can come with multiple or a singular strip. up to 12 zones (or bulbs in this instance) or as little as 3 zones. they come in chunks of 3 zone strips, which you can add onto the end of the line of them.

I'd bet with my knowledge of lifx-z, and your knowledge of hyperion displayed here, I could see us coming up with a solution. However, multiple bulbs is a nice thought, an array of some kind perhaps, but the lifx-z strip in question is only a singular bulb in practice. We'd have to dig into the lifx-z api to figure out how to do multiple zones.

I'll start looking into this Lifx-Max Local control Ambilighting to try to find anything of value for us to snag.

@SergioSV96
Copy link
Author

SergioSV96 commented Jan 19, 2021

If I'm understanding the code correctly, and this being with minimal knowledge of hyperion; I'd guess you would just have to modify the datagram for the bulb/listener to include more data. Hyperion already supports multiple zone led control, it seems it's just a matter or connecting the dots so to speak.

The Lifx-z Strip can come with multiple or a singular strip. up to 12 zones (or bulbs in this instance) or as little as 3 zones. they come in chunks of 3 zone strips, which you can add onto the end of the line of them.

I'd bet with my knowledge of lifx-z, and your knowledge of hyperion displayed here, I could see us coming up with a solution. However, multiple bulbs is a nice thought, an array of some kind perhaps, but the lifx-z strip in question is only a singular bulb in practice. We'd have to dig into the lifx-z api to figure out how to do multiple zones.

I'll start looking into this Lifx-Max Local control Ambilighting to try to find anything of value for us to snag.

Sorry for my late response,

I concur with what you said here, I wish I had one strip to try it myself.

Did you managed to find anything on controlling the Lifx-z strip? I saw that the LifxLan library used in this same gist had been updated already to support the strips as stated in their README:

Supports white, color, multizone (LIFX Z, LIFX Beam)

MultiZoneLight API
Lights with MultiZone capability, such as the LIFX Z, have all the same methods as the Light API

I gave it a quick look and seems pretty straigthforward to implement with this current script, just need to revamp it a little and set some hyperion "zones" for it to send us more "color data" :)

@RoldyBuffalo
Copy link

Hello again, I've been very caught up in other matters at home, but as far as the lifx strips/beams are concerned, I do believe you are correct with the LifxLan requisites for enabling multi-zone support.

And as you also stated, hyperion would need to be able to send the correct number of zones for the strips.

@SergioSV96
Copy link
Author

Hello again, I've been very caught up in other matters at home, but as far as the lifx strips/beams are concerned, I do believe you are correct with the LifxLan requisites for enabling multi-zone support.

And as you also stated, hyperion would need to be able to send the correct number of zones for the strips.

The hyperion part is really easy to do, just add more zones using their website UI, then we need to decode them properly on the script, do you want to do this? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment