Skip to content

Commit

Permalink
dynamic window resizing and disabled light support
Browse files Browse the repository at this point in the history
  • Loading branch information
SiwatS committed Dec 13, 2023
1 parent 0de0a6b commit 76fdf03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/espmega_lightshow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ def set_tile_state(row: int, column: int, state: bool):
element.config(bg=COLOR_ON_OFFLINE)

def get_tile_state(row: int, column: int):
# If the light is disabled, return disabled
if not light_grid.is_installed(row=row, column=column):
return LIGHT_DISABLED
light = light_grid.get_physical_light(row, column)
state = light.get_light_state()
if state == LightDriver.LIGHT_STATE_ON or state == LightDriver.LIGHT_STATE_ON_UNCONTROLLED:
Expand Down Expand Up @@ -509,7 +512,7 @@ def render_frame(frame: list):

def change_light_config(event):
# Get a list of all configuration options for each light driver
config_options = {"espmega": ESPMegaLightDriver.get_driver_properties()["configuration_parameters"], "homeassistant": HomeAssistantLightDriver.get_driver_properties()["configuration_parameters"]}
config_options = {"disabled":[],"espmega": ESPMegaLightDriver.get_driver_properties()["configuration_parameters"], "homeassistant": HomeAssistantLightDriver.get_driver_properties()["configuration_parameters"]}
config_vars = {}
config_frames = {}
global script_active
Expand All @@ -523,6 +526,7 @@ def change_light_config(event):
light_config_window.title("Light Config")
icon = ImageTk.PhotoImage(icon_image)
light_config_window.wm_iconphoto(True, icon)
light_config_window.resizable(False, False)

def submit_light_config():
global light_grid
Expand All @@ -542,6 +546,10 @@ def submit_light_config():
"Error", f"Please fill in the {config_option} field.")
return

# If the light is disabled, set the light config to None
if driver == "disabled":
physical_light_config = None

# Update the light map
modified_light_map = light_grid.light_map
modified_light_map[row][column] = physical_light_config
Expand All @@ -561,6 +569,9 @@ def submit_light_config():
light_config_window.destroy()

def get_driver_from_json():
# If the light is disabled, return disabled
if light_grid.light_map[row][column] == None:
return "disabled"
return light_grid.light_map[row][column]["driver"]

def load_config_to_entry_fields(driver):
Expand All @@ -585,7 +596,12 @@ def load_config_to_entry_fields(driver):
config_vars[driver][config_var].set(
light_config[config_var])
root.update()


def resize_window():
# Get Number of Config Options
driver = light_driver_var.get()
num_config_options = len(config_options[driver])
light_config_window.geometry(f"250x{90+num_config_options*40}")

def light_driver_dropdown_callback(*args):
# Hide all the config frames
Expand All @@ -596,6 +612,8 @@ def light_driver_dropdown_callback(*args):
config_frames[driver].grid(row=2, column=0, columnspan=2)
# Load Config to Entry Fields
load_config_to_entry_fields(driver)
resize_window()
root.update()

position_label = ttk.Label(
light_config_window, text=f"Configuring Light at {row}, {column}")
Expand All @@ -607,8 +625,9 @@ def light_driver_dropdown_callback(*args):
light_driver_var = tk.StringVar()
light_driver_var.set("espmega")
# Switch to the correct light driver config frame based on the selected light driver
drivers = list(config_options.keys())
light_driver_dropdown = tk.OptionMenu(
light_config_window, light_driver_var, "espmega", "homeassistant",command=light_driver_dropdown_callback)
light_config_window, light_driver_var, *drivers,command=light_driver_dropdown_callback)
light_driver_dropdown.grid(row=1, column=1)


Expand All @@ -620,10 +639,10 @@ def light_driver_dropdown_callback(*args):
config_label_text = config_option.replace("_", " ").title()
config_option_label = ttk.Label(
config_frames[device], text=config_label_text)
config_option_label.pack()
config_option_label.pack(padx=20, anchor="center")
config_option_var = tk.StringVar()
config_option_entry = ttk.Entry(config_frames[device], textvariable=config_option_var)
config_option_entry.pack()
config_option_entry = ttk.Entry(config_frames[device], textvariable=config_option_var, width=35)
config_option_entry.pack(padx=20, anchor="center")
config_vars[device][config_option] = config_option_var

# Each light driver has different configuration options, different configuration options will be shown depending on the selected light driver
Expand All @@ -638,7 +657,8 @@ def light_driver_dropdown_callback(*args):
# Create a button to save the light config and close the window
submit_button = ttk.Button(
light_config_window, text="Save", command=submit_light_config)
submit_button.grid(row=3, column=0, columnspan=2,pady=5)
submit_button.grid(row=3, column=0, columnspan=2,pady=5,padx=85)
resize_window()
light_config_window.mainloop()


Expand Down
5 changes: 5 additions & 0 deletions src/espmega_lightshow/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,15 @@ def read_light_map(self, light_map: list) -> list:
for row_index, row in enumerate(light_map):
for column_index, light in enumerate(row):
# light is a dictionary with fields varying by driver
# Is the light disabled?
if light is None:
self.assign_physical_light(row_index, column_index, None)
continue
# Are we in design mode?
# If we are in design mode, we don't need to connect to the drivers, so we can just assign a dummy driver
if self.design_mode:
self.assign_physical_light(row_index, column_index, DummyLightDriver())
continue
# Let's switch on the driver field
driver_type = light["driver"]
print(light)
Expand Down

0 comments on commit 76fdf03

Please sign in to comment.