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 from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy.uix.screenmanager import Screen, ScreenManager from src.tinge import HueBridge, HueUtils, Tinge class HueApp(App): def build(self): self.tinge = Tinge() sm = ScreenManager() 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, 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): self.sm.add_widget( SingleBridgeScreen(self.sm, self.tinge, bridge, name='single_bridge')) self.sm.current = 'single_bridge' def discover_bridge(self, button): bridges = self.tinge.discover_new_bridges() if bridges: for bridge in bridges: 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 = [] 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)) buttons.append(bridge_button) bridge_button = Button( text='Discover Bridges', size_hint=(1, 0.1), on_press=lambda button: self.discover_bridge(button)) buttons.append(bridge_button) sizer = Label(disabled=True) buttons.append(sizer) return buttons if __name__ == '__main__': HueApp().run()