MylesMor / nanoleafapi

A Python3 wrapper for the Nanoleaf OpenAPI, for use with the Light Panels, Canvas and Shapes (Hexagons, Triangles and Elements).
https://nanoleafapi.readthedocs.io
MIT License
59 stars 15 forks source link

Add extControl enabling function #15

Closed erhan- closed 2 years ago

erhan- commented 2 years ago

Adds a function to enable the UDP extControl API.

erhan- commented 2 years ago

Example code:

from nanoleafapi import Nanoleaf
import socket
from random import randint
from time import sleep

nanoleaf_host = 'yourNanoleafIP'
nanoleaf_udp_port = 60222

nanoleaf_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
nl = Nanoleaf(nanoleaf_host)

#nl.create_auth_token()
nl.enable_extcontrol()

panel_ids = nl.get_ids()
print(panel_ids)
n_panels = len(panel_ids) - 1
n_panels_b = n_panels.to_bytes(2, "big")

while True:

    send_data = b""
    send_data += n_panels_b

    transition = 10

    for panel_id in panel_ids:
        if panel_id != 0:
            panel_id_b = panel_id.to_bytes(2, "big")
            send_data += panel_id_b
            red = randint(40, 255)
            send_data += red.to_bytes(1, "big")
            green = randint(40, 255)
            send_data += green.to_bytes(1, "big")
            blue =randint(40, 255)
            send_data += blue.to_bytes(1, "big")
            white = randint(0, 0)
            send_data += white.to_bytes(1, "big")
            send_data += transition.to_bytes(2, "big")

    print(send_data)

    nanoleaf_socket.sendto(send_data, (nanoleaf_host, nanoleaf_udp_port))
    sleep(1)

nanoleaf_socket.close()
threefjefff commented 2 years ago

Been using this locally with some great results.

MylesMor commented 2 years ago

Hey, thanks for the PR. I don't have any NL devices setup right now and am super busy, but it looks good to me and appreciate the feedback on this as well @threefjefff, so will merge it. Will try and get the package updated in the next week. Thank you for your contribution!

erhan- commented 2 years ago

No problem, thanks for your work actually.

MylesMor commented 2 years ago

It's finally available in the latest version of the package, sorry it took so long! Thanks again!

hadiudoit11 commented 1 year ago

Thanks so much for putting this work out there! I'm trying to build an app that can live on my ec2 instance for others in my home to connect to and control my nanoleaf lights.

i have two nanoleaf devices, one of them works with tcp port using 16021 (default port) then other one can use the same port but obviously they cannot share a tcp port (to my knowledge).

Would UDP be the solution here?

MylesMor commented 1 year ago

Thanks so much for putting this work out there! I'm trying to build an app that can live on my ec2 instance for others in my home to connect to and control my nanoleaf lights.

i have two nanoleaf devices, one of them works with tcp port using 16021 (default port) then other one can use the same port but obviously they cannot share a tcp port (to my knowledge).

Would UDP be the solution here?

Hi! As far as I know, it should be fine controlling two separate devices - all you need is the IP for each one. The TCP port is open on each separate Nanolesf device, so them having the same port shouldn't matter.