|
| 1 | +""" |
| 2 | +Slider Trinkey Hue Brightness Python Example |
| 3 | +(Requires Hue and Monitor Brightness CircuitPython example to be running on the Slider Trinkey) |
| 4 | +""" |
| 5 | +import sys |
| 6 | +from phue import Bridge |
| 7 | +import serial |
| 8 | +from serial.tools import list_ports |
| 9 | + |
| 10 | +# Update this to the room, zone or individual lamp you want to control. |
| 11 | +LAMP_OR_GROUP_NAME = "Office" |
| 12 | + |
| 13 | +# Update this to the IP address of your Hue Bridge. |
| 14 | +b = Bridge("0.0.0.0") |
| 15 | + |
| 16 | +slider_trinkey_port = None |
| 17 | +ports = list_ports.comports(include_links=False) |
| 18 | +for p in ports: |
| 19 | + if p.pid is not None: |
| 20 | + print("Port:", p.device, "-", hex(p.pid), end="\t") |
| 21 | + if p.pid == 0x8102: |
| 22 | + slider_trinkey_port = p |
| 23 | + print("Found Slider Trinkey!") |
| 24 | + trinkey = serial.Serial(p.device) |
| 25 | + break |
| 26 | +else: |
| 27 | + print("Did not find Slider Trinkey port :(") |
| 28 | + sys.exit() |
| 29 | + |
| 30 | +# If the app is not registered and the button on the Hue Bridge is not pressed, press the button |
| 31 | +# and call connect() (this only needs to be run a single time) |
| 32 | +b.connect() |
| 33 | +b.get_api() |
| 34 | + |
| 35 | +is_group = False |
| 36 | +light = None |
| 37 | + |
| 38 | +# First, check if it's a group name. |
| 39 | +for group_data in b.get_group().values(): |
| 40 | + if group_data["name"] == LAMP_OR_GROUP_NAME: |
| 41 | + print("Found group with name", LAMP_OR_GROUP_NAME) |
| 42 | + is_group = True |
| 43 | + |
| 44 | +# If it's not a group, find the lamp by name. |
| 45 | +if not is_group: |
| 46 | + light_names = b.get_light_objects("name") |
| 47 | + light = light_names[LAMP_OR_GROUP_NAME] |
| 48 | + print("Found light with name", LAMP_OR_GROUP_NAME) |
| 49 | + |
| 50 | +current_brightness = None |
| 51 | +while True: |
| 52 | + x = trinkey.readline().decode("utf-8") |
| 53 | + if not x.startswith("Slider: "): |
| 54 | + continue |
| 55 | + |
| 56 | + # Convert the Slider Trinkey output value of 0-100 to 0-254. |
| 57 | + brightness_value = int((float(x.split(": ")[1]) / 100) * 254) |
| 58 | + |
| 59 | + if current_brightness is None or brightness_value != current_brightness: |
| 60 | + print("Setting brightness to:", brightness_value) |
| 61 | + if is_group: |
| 62 | + b.set_group(LAMP_OR_GROUP_NAME, {"bri": brightness_value}) |
| 63 | + else: |
| 64 | + light.brightness = brightness_value |
| 65 | + current_brightness = brightness_value |
0 commit comments