### License: WTFPL - http://www.wtfpl.net/ import requests import re import sys import pymsgbox import os from phue import Bridge import json from tkinter import * from PIL import ImageTk, Image from tkinter import Tk, Label, Button ## config.ini - create or use file_name = "config.ini" if not os.path.isfile(file_name): #print(f"The file {file_name} is in the working directory.") ip = pymsgbox.prompt('Hue Bridge IP', default='192.168.178.100') key = pymsgbox.prompt('Hue API key', default='AB-1234567890abcdefghi') with open(file_name, "w") as file: file.write(f"var1: {ip}\nvar2: {key}") else: print ("foobar") with open(file_name, "r") as file: content = file.readlines() ip = content[0].split(":")[1].strip() key = content[1].split(":")[1].strip() ### positional arguments, old # url = f"http://{sys.argv[1]}/api/{sys.argv[2]}/lights" ### get light info url = f"http://{ip}/api/{key}/lights" response = requests.get(url) url_switch = f"http://{ip}/api/{key}/lights" b = Bridge(ip) def switch_on(): requests.put(f"{url}/{i}/state", json={"on": True}) #data = [{"$key": 8},{"$key": 7}] def switch_off(): requests.put(f"{url}/{i}/state", json={"on": False}) def process_light_info(light_number, light_details): light_name = light_details.get('name', 'Unknown') light_type = light_details.get('type', 'Unknown') manufacturer_name = light_details.get('manufacturername', 'Unknown') product_name = light_details.get('productname', 'Unknown') light_status = light_details.get('state', {}).get('on', 'Unknown') # Added status #lid = {light_number} info_str = f"{light_number}. {light_name}\n\tType: {light_type}\n\tManufacturer: {manufacturer_name}, \n\tProduct: {product_name}\n\tStatus: {'On' if light_status else 'Off'}" # Updated info_str to include status lights_info.append(info_str) lights_data = response.json() lights_info = [] ### Hue data as dict or list? if isinstance(lights_data, dict): for light_number, light_details in lights_data.items(): process_light_info(light_number, light_details) elif isinstance(lights_data, list): for i, light_details in enumerate(lights_data): process_light_info(i, light_details) ### print, for debugging # print("\n".join(lights_info)) ### pymsgbox, for debugging #pymsgbox.alert(info, 'El Tutos Hue Lister') info = "\n".join(lights_info) def showinfo(lid): pymsgbox.alert(lid) ## testing mouse scroll stuff ############################################ def mouse_wheel(event): global count # respond to Linux or Windows wheel event if event.num == 5 or event.delta == -120: count -= 1 if event.num == 4 or event.delta == 120: count += 1 count = 0 ## testing end - scrolling only when hovering scrollbar, dumb ... #### Building the GUI, tkinter code # window areas: # canvas = everything # label = info area # frame = button area root = Tk() root.geometry("800x1200+100+100") root.title("El Tutos Hue App") ## background image, just as reference #bg = ImageTk.PhotoImage(Image.open("image.png")) bgcolor = "white" canvas = Canvas(root, background=bgcolor) scroll_y = Scrollbar(root, orient="vertical", command=canvas.yview) frame = Frame(canvas, background=bgcolor) foobar = 10 ### script restart as gross status update, meh #def restart(): # os.execv(sys.executable, ['python'] + sys.argv) ### Slider def sliderstate(): global dumm dumm = brightness.get() for i, entry in enumerate(lights_info): # Find the first standalone number in quotes match = re.search(r'\b(\d+)\b', entry) # Check if a match is found if match: lid = match.group(1) else: # Set a default value if no match is found lid = "Default" label = Label(frame, text=entry, width=50, justify=LEFT, anchor=W, background=bgcolor) label.grid(row=i+2, column=0, sticky=W) on_button = Button(frame, text="On", command=lambda lid2=lid: [b.set_light(int(lid2),'on', True)]) ## optional second command: , restart() on_button.grid(row=i+2, column=2, sticky=E) off_button = Button(frame, text="Off", command=lambda lid2=lid: [b.set_light(int(lid2),'on', False)]) off_button.grid(row=i+2, column=9, sticky=E) # Actual sliders slidername = Label(frame, text='Brightness', background=bgcolor, font='Helvetica 14 bold') slidername.grid(row=3, column=19, sticky=E) brightness = Scale(frame, from_=0, to=248, orient=VERTICAL, length=800) brightness.set(0) brightness.configure(background='white', width=90 ) brightness.grid(row=2, column=19, rowspan=30) bri_go = Button(frame, text="Set", command=lambda lid2=lid: [sliderstate(), b.set_light(int(lid2), 'bri', dumm), print(dumm)]) bri_go.grid(row=i+2, column=12, sticky=E) ### testing scroll stuff - wheel binding canvas.bind_all("", mouse_wheel) canvas.create_window(0, 0, anchor='nw', window=frame) canvas.update_idletasks() canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=scroll_y.set) canvas.pack(fill='both', expand=True, side='left') scroll_y.pack(fill='y', side='right') root.mainloop()