From 26bbaf3aa47999868f4e0417731887c8b4725b7a Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 26 Nov 2023 23:44:48 +0100 Subject: [PATCH] Show rooms --- main.py | 73 +++++++++++++++++++++++++++++----- src/tinge/HueLight/__init__.py | 8 ++++ src/tinge/__init__.py | 9 +++++ 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 7902e8d..95be26c 100755 --- a/main.py +++ b/main.py @@ -1,15 +1,17 @@ from logging import disable + import kivy kivy.require('2.2.1') from kivy.app import App from kivy.uix.boxlayout import BoxLayout -from kivy.uix.button import Button, ColorProperty +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 src.tinge import HueBridge, Tinge +from src.tinge import HueBridge, HueUtils, Tinge class HueApp(App): @@ -17,30 +19,79 @@ class HueApp(App): def build(self): self.tinge = Tinge() sm = ScreenManager() - sm.add_widget(BridgeScreen(self.tinge)) + sm.add_widget(BridgeScreen(sm, self.tinge, name='bridge_screen')) return sm +class SingleBridgeScreen(Screen): + + def __init__(self, sm, tinge, bridge: HueBridge, **kwargs): + super(SingleBridgeScreen, self).__init__(**kwargs) + self.sm = sm + self.tinge = tinge + self.bridge = bridge + self.layout = BoxLayout() + self.add_widget(self.layout) + self.set_group_buttons() + + def set_group_buttons(self): + 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) + + def press_button(self, light, button): + print(light) + + class BridgeScreen(Screen): bridges = [] layout = BoxLayout() - def __init__(self, tinge, **kwargs): + def __init__(self, sm, tinge, **kwargs): super(BridgeScreen, self).__init__(**kwargs) + self.sm = sm self.tinge = tinge for bridge_button in self.set_bridge_buttons(): self.layout.add_widget(bridge_button) def press_button(self, bridge, button): - print(bridge.get_ipaddress()) + self.sm.add_widget( + SingleBridgeScreen(self.sm, + self.tinge, + bridge, + name='single_bridge')) + self.sm.current = 'single_bridge' - def manually_add(self, button): - bridges = self.tinge.discover_new_bridges() + def discover_bridge(self, button): + bridges = self.tinge.discover_new_bridges() if bridges: for bridge in bridges: - if not bridge in self.tinge.get_bridges(): - self.tinge.append_bridge(bridge) + print(bridge) + if not self.tinge.bridge_discovered(bridge['ipaddress']): + # Display popup lable to add bridge and get username + popup = Popup(title='Connect Bridge', + size_hint=(None, None), + size=(400, 400)) + 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'])) + popup.add_widget(new_bridge_button) + popup.open() + + def wait_for_button_press(self, button, popup, ipaddress): + 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)) + self.tinge.write_all_bridges_to_conf() + popup.dismiss() def set_bridge_buttons(self): buttons = [] @@ -54,9 +105,9 @@ class BridgeScreen(Screen): bridge_button = Button( text='Discover Bridges', size_hint=(1, 0.1), - on_press=lambda button: self.manually_add(button)) + on_press=lambda button: self.discover_bridge(button)) buttons.append(bridge_button) - sizer = Label(disabled = True) + sizer = Label(disabled=True) buttons.append(sizer) return buttons diff --git a/src/tinge/HueLight/__init__.py b/src/tinge/HueLight/__init__.py index b89898a..e631ff4 100644 --- a/src/tinge/HueLight/__init__.py +++ b/src/tinge/HueLight/__init__.py @@ -72,6 +72,14 @@ class HueLight: """ return self.m_hue + def get_name(self) -> str: + """Get the name of the light + + Returns: + str: Name of the light + """ + return self.m_name + def get_sat(self): """Get current saturation of the light diff --git a/src/tinge/__init__.py b/src/tinge/__init__.py index 299d9af..d2ca9dd 100644 --- a/src/tinge/__init__.py +++ b/src/tinge/__init__.py @@ -85,6 +85,15 @@ class Tinge: """ return self.m_bridges + def bridge_discovered(self, ipaddress: str) -> bool: + """ + Check if a bridge has been discovered + """ + for bridge in self.m_bridges: + if bridge.ipaddress == ipaddress: + return True + return False + def read_bridges_from_file(self) -> None: """Read config file and add back previously discovered bridges """