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.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",
self.layout.add_widget(self.get_group_button(group))
self.layout.add_widget(
Button(text="Back",
size_hint=(1, 0.1),
on_press=lambda button: self.press_back_button(button, "back"))
self.layout.add_widget(backbutton)
on_press=lambda _: self.press_back_button()))
def press_back_button(self, button, group):
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):
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='single_bridge'))
self.sm.current = 'single_bridge'
name=bridge_name))
self.sm.current = bridge_name
def discover_bridge(self, button):
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',
bridge_button = Button(text='Discover Bridges',
size_hint=(1, 0.1),
on_press=lambda button: self.discover_bridge(button))
on_press=lambda _: self.discover_bridge())
buttons.append(bridge_button)
sizer = Label(disabled=True)
buttons.append(sizer)

@ -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

@ -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)

Loading…
Cancel
Save