From 84bdbc2476a1ce096b762c2e1d0eb0d000fd84c0 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 9 Dec 2023 15:46:59 +0100 Subject: [PATCH] Add groups --- main.py | 122 ++++++++++++++++++++++---------- src/tinge/HueBridge/__init__.py | 4 +- src/tinge/__init__.py | 5 +- 3 files changed, 89 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index 526deb6..1bb82d1 100755 --- a/main.py +++ b/main.py @@ -6,13 +6,13 @@ kivy.require('2.2.1') from kivy.app import App from kivy.uix.boxlayout import BoxLayout -from kivy.uix.stacklayout import StackLayout from kivy.uix.button import Button from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy.uix.screenmanager import Screen, ScreenManager +from kivy.uix.stacklayout import StackLayout -from src.tinge import HueBridge, HueUtils, Tinge +from src.tinge import HueBridge, HueGroup, HueUtils, Tinge class HueApp(App): @@ -25,6 +25,40 @@ class HueApp(App): return sm +class GroupScreen(Screen): + + def __init__(self, sm, tinge, id: int, bridge: HueBridge, **kwargs): + super(GroupScreen, self).__init__(**kwargs) + self.sm = sm + self.tinge = tinge + self.id = id + self.group = bridge.get_group_by_id(id) + self.bridge = bridge + self.layout = StackLayout() + self.add_widget(self.layout) + for button in self.get_light_buttons(): + self.layout.add_widget(button) + + def get_light_buttons(self): + buttons = [] + for light in self.group.get_lights(): + button = Button(text=str(light), + size_hint=(1, 0.1), + on_press=lambda _: self.press_button(light)) + buttons.append(button) + backbutton = Button(text="Back", + size_hint=(1, 0.1), + on_press=lambda _: self.press_back_button()) + buttons.append(backbutton) + return buttons + + def press_back_button(self): + self.sm.current = "bridge_" + str(self.bridge.id) + + def press_button(self, light): + print(button, light) + + class SingleBridgeScreen(Screen): def __init__(self, sm, tinge, bridge: HueBridge, **kwargs): @@ -34,26 +68,33 @@ class SingleBridgeScreen(Screen): self.bridge = bridge self.layout = StackLayout() self.add_widget(self.layout) - self.set_group_buttons() - - def set_group_buttons(self): + self.groups = [] for group in self.bridge.get_groups(): - button = Button( - text=str(group), - size_hint=(1, 0.1), - on_press=lambda button: self.press_button(button, group)) - self.layout.add_widget(button) - backbutton = Button( - text="Back", - size_hint=(1, 0.1), - on_press=lambda button: self.press_back_button(button, "back")) - self.layout.add_widget(backbutton) - - def press_back_button(self, button, group): + self.layout.add_widget(self.get_group_button(group)) + self.layout.add_widget( + Button(text="Back", + size_hint=(1, 0.1), + on_press=lambda _: self.press_back_button())) + + def get_group_button(self, group): + button = Button(text=str(group), size_hint=(1, 0.1)) + button.bind(on_press=lambda _: self.press_button(group.get_id())) + return button + + def press_back_button(self): self.sm.current = "bridge_screen" - def press_button(self, light, button): - print(light) + def press_button(self, group: int): + print(group) + group_name: str = 'group_' + str(group) + group_screen = GroupScreen(self.sm, + self.tinge, + group, + self.bridge, + name=group_name) + if not self.sm.has_screen(group_name): + self.sm.add_widget(group_screen) + self.sm.current = group_name class BridgeScreen(Screen): @@ -64,18 +105,20 @@ class BridgeScreen(Screen): super(BridgeScreen, self).__init__(**kwargs) self.sm = sm self.tinge = tinge - for bridge_button in self.set_bridge_buttons(): + for bridge_button in self.get_bridge_buttons(): self.layout.add_widget(bridge_button) - def press_button(self, bridge, button): - self.sm.add_widget( - SingleBridgeScreen(self.sm, - self.tinge, - bridge, - name='single_bridge')) - self.sm.current = 'single_bridge' - - def discover_bridge(self, button): + def press_button(self, bridge): + bridge_name = "bridge_" + str(bridge.id) + if not self.sm.has_screen(bridge_name): + self.sm.add_widget( + SingleBridgeScreen(self.sm, + self.tinge, + bridge, + name=bridge_name)) + self.sm.current = bridge_name + + def discover_bridge(self): bridges = self.tinge.discover_new_bridges() if bridges: for bridge in bridges: @@ -88,33 +131,34 @@ class BridgeScreen(Screen): new_bridge_button = Button( text='Press button on Bridge', size_hint=(1, 0.1), - on_press=lambda button: self.wait_for_button_press( - button, popup, bridge['ipaddress'])) + on_press=lambda _: self.wait_for_button_press( + popup, bridge['ipaddress'])) popup.add_widget(new_bridge_button) popup.open() - def wait_for_button_press(self, button, popup, ipaddress): + def wait_for_button_press(self, popup, ipaddress): + bridge_id = len(self.tinge.get_bridges()) user_or_error = HueUtils.connect(ipaddress) while user_or_error.is_error(): user_or_error = HueUtils.connect(ipaddress) self.tinge.append_bridge( - HueBridge(ipaddress, user_or_error.get_user(), ipaddress)) + HueBridge(bridge_id, ipaddress, user_or_error.get_user(), + ipaddress)) self.tinge.write_all_bridges_to_conf() popup.dismiss() - def set_bridge_buttons(self): + def get_bridge_buttons(self): buttons = [] bridges = self.tinge.get_bridges() for bridge in bridges: bridge_button = Button( text=str(bridge.get_ipaddress()), size_hint=(1, 0.1), - on_press=lambda button: self.press_button(bridge, button)) + on_press=lambda _: self.press_button(bridge)) buttons.append(bridge_button) - bridge_button = Button( - text='Discover Bridges', - size_hint=(1, 0.1), - on_press=lambda button: self.discover_bridge(button)) + bridge_button = Button(text='Discover Bridges', + size_hint=(1, 0.1), + on_press=lambda _: self.discover_bridge()) buttons.append(bridge_button) sizer = Label(disabled=True) buttons.append(sizer) diff --git a/src/tinge/HueBridge/__init__.py b/src/tinge/HueBridge/__init__.py index 153d761..3266152 100644 --- a/src/tinge/HueBridge/__init__.py +++ b/src/tinge/HueBridge/__init__.py @@ -12,16 +12,18 @@ class HueBridge: """This class represents a Hue Bridge """ - def __init__(self, ipaddress: str, username: str, + def __init__(self, id: int, ipaddress: str, username: str, name: str = "", is_reachable: bool = True): """ Constructor Args: + id (int): The id of the bridge ipaddress (str): The ip address of the bridge username (str): The username for this app for this bridge name (str, optional): A human readable name for this bridge. Is set to ipaddress, if not supplied. """ + self.id: int = id self.m_ipaddress: str = ipaddress self.m_username: str = username self.m_is_reachable = is_reachable diff --git a/src/tinge/__init__.py b/src/tinge/__init__.py index d2ca9dd..68d23ce 100644 --- a/src/tinge/__init__.py +++ b/src/tinge/__init__.py @@ -102,14 +102,15 @@ class Tinge: mbridges = toml.loads(configfile.read()) for key, value in mbridges.items(): if key not in self.m_discovered: + id = len(self.m_discovered) response = make_request(key, "{}/".format(value['user'])) if response: name = "{} ({})".format(response.json()['config']['name'], key) - bridge: HueBridge = HueBridge(key, value['user'], name) + bridge: HueBridge = HueBridge(id, key, value['user'], name) self.m_bridges.append(bridge) self.m_discovered.append(key) else: - bridge: HueBridge = HueBridge(key, value['user'], is_reachable=False) + bridge: HueBridge = HueBridge(id, key, value['user'], is_reachable=False) self.m_bridges.append(bridge) self.m_discovered.append(key)