This is a fix for Issue#15 (#18)

master
Micke Nordin 4 years ago
parent df5403a519
commit fbabfdf0ea
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -55,14 +55,26 @@ class Hui(wx.Frame):
"""Add bridges to sizer, the entry point of the program """Add bridges to sizer, the entry point of the program
""" """
self.SetTitle('Tinge - All Bridges') self.SetTitle('Tinge - All Bridges')
one_unreachable: bool = False
no_bridges: bool = True
if self.m_tinge.get_bridges(): if self.m_tinge.get_bridges():
no_bridges = False
for bridge in self.m_tinge.get_bridges(): for bridge in self.m_tinge.get_bridges():
btn: wx.Button = wx.Button(self.pnl, label=str(bridge)) if bridge.is_reachable():
self.sizer.Add(btn, 0, wx.EXPAND) btn: wx.Button = wx.Button(self.pnl, label=str(bridge))
self.Bind(wx.EVT_BUTTON, self.sizer.Add(btn, 0, wx.EXPAND)
lambda event, mbridge=bridge: self.goto_bridge(mbridge), btn) self.Bind(wx.EVT_BUTTON,
else: lambda event, mbridge=bridge: self.goto_bridge(mbridge), btn)
btn: wx.Button = wx.Button(self.pnl, label="Press Hue Bridge button, and then press here") else:
one_unreachable = True
if one_unreachable or no_bridges:
if one_unreachable:
warn_label = "{} At least one previously discovered bridge unreachable.".format(self.m_unreachable_icon)
warning: wx.StaticText = wx.StaticText(self.pnl, label=warn_label, style=wx.ALIGN_CENTER)
self.sizer.Add(warning, 0, wx.EXPAND)
label = "Press Hue Bridge button, and then here within 30 seconds to connect"
btn: wx.Button = wx.Button(self.pnl, label=label)
self.sizer.Add(btn, 0, wx.EXPAND) self.sizer.Add(btn, 0, wx.EXPAND)
self.Bind(wx.EVT_BUTTON, self.Bind(wx.EVT_BUTTON,
lambda event: self.discover_new_bridges(), btn) lambda event: self.discover_new_bridges(), btn)
@ -235,7 +247,6 @@ class Hui(wx.Frame):
self.add_bridges() self.add_bridges()
return found_any return found_any
def get_ok_cancel_answer_from_modal(self, message: str) -> bool: def get_ok_cancel_answer_from_modal(self, message: str) -> bool:
"""Display a message dialog and return ok or cancel """Display a message dialog and return ok or cancel

@ -12,7 +12,7 @@ class HueBridge:
"""This class represents a Hue Bridge """This class represents a Hue Bridge
""" """
def __init__(self, ipaddress: str, username: str, name: str = ""): def __init__(self, ipaddress: str, username: str, name: str = "", is_reachable: bool = True):
""" Constructor """ Constructor
Args: Args:
@ -22,14 +22,20 @@ class HueBridge:
""" """
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
if name: if name:
self.m_name: str = name self.m_name: str = name
else: else:
self.m_name = self.m_ipaddress self.m_name = self.m_ipaddress
self.m_lights: list[HueLight] = self.discover_lights() if is_reachable:
self.discover_new_lights() self.m_lights: list[HueLight] = self.discover_lights()
self.m_new_lights: list[HueLight] = self.get_new_lights() self.discover_new_lights()
self.m_groups: list[HueGroup] = self.discover_groups() self.m_new_lights: list[HueLight] = self.get_new_lights()
self.m_groups: list[HueGroup] = self.discover_groups()
else:
self.m_lights: list[HueLight] = list()
self.m_new_lights: list[HueLight] = list()
self.m_groups: list[HueGroup] = list()
def __str__(self) -> str: def __str__(self) -> str:
"""The string representation of this bridge """The string representation of this bridge
@ -216,3 +222,6 @@ class HueBridge:
for group in self.m_groups: for group in self.m_groups:
group.remove_light(light) group.remove_light(light)
self.m_lights.remove(light) self.m_lights.remove(light)
def is_reachable(self):
return self.m_is_reachable

@ -61,9 +61,11 @@ class Tinge:
if 'error' in resp.keys(): if 'error' in resp.keys():
m_keys = resp['error'].keys() m_keys = resp['error'].keys()
if 'description' in m_keys and 'address' in m_keys and 'type' in m_keys: if 'description' in m_keys and 'address' in m_keys and 'type' in m_keys:
if resp['error']['description'] == "unauthorized user" and resp['error'][ # This is kinda ugly but line is too long with and statement
'address'] == "/lights" and resp['error']['type'] == 1: if resp['error']['description'] == "unauthorized user":
discovered = True if resp['error']['address'] == "/lights":
if resp['error']['type'] == 1:
discovered = True
except simplejson.errors.JSONDecodeError: except simplejson.errors.JSONDecodeError:
pass pass
@ -89,9 +91,16 @@ 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:
bridge: HueBridge = HueBridge(key, value['user']) response = make_request(key, "{}/".format(value['user']))
self.m_bridges.append(bridge) if response:
self.m_discovered.append(key) name = "{} ({})".format(response.json()['config']['name'], key)
bridge: HueBridge = HueBridge(key, value['user'], name)
self.m_bridges.append(bridge)
self.m_discovered.append(key)
else:
bridge: HueBridge = HueBridge(key, value['user'], is_reachable=False)
self.m_bridges.append(bridge)
self.m_discovered.append(key)
def write_all_bridges_to_conf(self): def write_all_bridges_to_conf(self):
"""Save to file """Save to file

Loading…
Cancel
Save