kivy
Micke Nordin 1 year ago
parent 85c997430b
commit 84bdbc2476

@ -6,13 +6,13 @@ kivy.require('2.2.1')
from kivy.app import App from kivy.app import App
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button from kivy.uix.button import Button
from kivy.uix.label import Label from kivy.uix.label import Label
from kivy.uix.popup import Popup from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager 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): class HueApp(App):
@ -25,6 +25,40 @@ class HueApp(App):
return sm 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): class SingleBridgeScreen(Screen):
def __init__(self, sm, tinge, bridge: HueBridge, **kwargs): def __init__(self, sm, tinge, bridge: HueBridge, **kwargs):
@ -34,26 +68,33 @@ class SingleBridgeScreen(Screen):
self.bridge = bridge self.bridge = bridge
self.layout = StackLayout() self.layout = StackLayout()
self.add_widget(self.layout) self.add_widget(self.layout)
self.set_group_buttons() self.groups = []
def set_group_buttons(self):
for group in self.bridge.get_groups(): for group in self.bridge.get_groups():
button = Button( self.layout.add_widget(self.get_group_button(group))
text=str(group), self.layout.add_widget(
size_hint=(1, 0.1), Button(text="Back",
on_press=lambda button: self.press_button(button, group)) size_hint=(1, 0.1),
self.layout.add_widget(button) on_press=lambda _: self.press_back_button()))
backbutton = Button(
text="Back", def get_group_button(self, group):
size_hint=(1, 0.1), button = Button(text=str(group), size_hint=(1, 0.1))
on_press=lambda button: self.press_back_button(button, "back")) button.bind(on_press=lambda _: self.press_button(group.get_id()))
self.layout.add_widget(backbutton) return button
def press_back_button(self, button, group): def press_back_button(self):
self.sm.current = "bridge_screen" self.sm.current = "bridge_screen"
def press_button(self, light, button): def press_button(self, group: int):
print(light) 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): class BridgeScreen(Screen):
@ -64,18 +105,20 @@ class BridgeScreen(Screen):
super(BridgeScreen, self).__init__(**kwargs) super(BridgeScreen, self).__init__(**kwargs)
self.sm = sm self.sm = sm
self.tinge = tinge 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) self.layout.add_widget(bridge_button)
def press_button(self, bridge, button): def press_button(self, bridge):
self.sm.add_widget( bridge_name = "bridge_" + str(bridge.id)
SingleBridgeScreen(self.sm, if not self.sm.has_screen(bridge_name):
self.tinge, self.sm.add_widget(
bridge, SingleBridgeScreen(self.sm,
name='single_bridge')) self.tinge,
self.sm.current = 'single_bridge' bridge,
name=bridge_name))
def discover_bridge(self, button): self.sm.current = bridge_name
def discover_bridge(self):
bridges = self.tinge.discover_new_bridges() bridges = self.tinge.discover_new_bridges()
if bridges: if bridges:
for bridge in bridges: for bridge in bridges:
@ -88,33 +131,34 @@ class BridgeScreen(Screen):
new_bridge_button = Button( new_bridge_button = Button(
text='Press button on Bridge', text='Press button on Bridge',
size_hint=(1, 0.1), size_hint=(1, 0.1),
on_press=lambda button: self.wait_for_button_press( on_press=lambda _: self.wait_for_button_press(
button, popup, bridge['ipaddress'])) popup, bridge['ipaddress']))
popup.add_widget(new_bridge_button) popup.add_widget(new_bridge_button)
popup.open() 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) user_or_error = HueUtils.connect(ipaddress)
while user_or_error.is_error(): while user_or_error.is_error():
user_or_error = HueUtils.connect(ipaddress) user_or_error = HueUtils.connect(ipaddress)
self.tinge.append_bridge( 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() self.tinge.write_all_bridges_to_conf()
popup.dismiss() popup.dismiss()
def set_bridge_buttons(self): def get_bridge_buttons(self):
buttons = [] buttons = []
bridges = self.tinge.get_bridges() bridges = self.tinge.get_bridges()
for bridge in bridges: for bridge in bridges:
bridge_button = Button( bridge_button = Button(
text=str(bridge.get_ipaddress()), text=str(bridge.get_ipaddress()),
size_hint=(1, 0.1), 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) buttons.append(bridge_button)
bridge_button = Button( bridge_button = Button(text='Discover Bridges',
text='Discover Bridges', size_hint=(1, 0.1),
size_hint=(1, 0.1), on_press=lambda _: self.discover_bridge())
on_press=lambda button: self.discover_bridge(button))
buttons.append(bridge_button) buttons.append(bridge_button)
sizer = Label(disabled=True) sizer = Label(disabled=True)
buttons.append(sizer) buttons.append(sizer)

@ -12,16 +12,18 @@ class HueBridge:
"""This class represents a Hue Bridge """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): name: str = "", is_reachable: bool = True):
""" Constructor """ Constructor
Args: Args:
id (int): The id of the bridge
ipaddress (str): The ip address of the bridge ipaddress (str): The ip address of the bridge
username (str): The username for this app for this bridge username (str): The username for this app for this bridge
name (str, optional): A human readable name for this bridge. name (str, optional): A human readable name for this bridge.
Is set to ipaddress, if not supplied. Is set to ipaddress, if not supplied.
""" """
self.id: int = id
self.m_ipaddress: str = ipaddress self.m_ipaddress: str = ipaddress
self.m_username: str = username self.m_username: str = username
self.m_is_reachable = is_reachable self.m_is_reachable = is_reachable

@ -102,14 +102,15 @@ class Tinge:
mbridges = toml.loads(configfile.read()) mbridges = toml.loads(configfile.read())
for key, value in mbridges.items(): for key, value in mbridges.items():
if key not in self.m_discovered: if key not in self.m_discovered:
id = len(self.m_discovered)
response = make_request(key, "{}/".format(value['user'])) response = make_request(key, "{}/".format(value['user']))
if response: if response:
name = "{} ({})".format(response.json()['config']['name'], key) 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_bridges.append(bridge)
self.m_discovered.append(key) self.m_discovered.append(key)
else: 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_bridges.append(bridge)
self.m_discovered.append(key) self.m_discovered.append(key)

Loading…
Cancel
Save