From 82db8a297ef429510372fe5f4273e0ecea2bbe35 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Mon, 24 May 2021 14:52:17 +0000 Subject: [PATCH] This is a fix for Issue#15 (#18) --- main.py | 25 ++++++++++++++++++------- tinge/HueBridge/__init__.py | 19 ++++++++++++++----- tinge/__init__.py | 21 +++++++++++++++------ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index ca838f6..3a1729e 100644 --- a/main.py +++ b/main.py @@ -55,14 +55,26 @@ class Hui(wx.Frame): """Add bridges to sizer, the entry point of the program """ self.SetTitle('Tinge - All Bridges') + one_unreachable: bool = False + no_bridges: bool = True if self.m_tinge.get_bridges(): + no_bridges = False for bridge in self.m_tinge.get_bridges(): - btn: wx.Button = wx.Button(self.pnl, label=str(bridge)) - self.sizer.Add(btn, 0, wx.EXPAND) - self.Bind(wx.EVT_BUTTON, - lambda event, mbridge=bridge: self.goto_bridge(mbridge), btn) - else: - btn: wx.Button = wx.Button(self.pnl, label="Press Hue Bridge button, and then press here") + if bridge.is_reachable(): + btn: wx.Button = wx.Button(self.pnl, label=str(bridge)) + self.sizer.Add(btn, 0, wx.EXPAND) + self.Bind(wx.EVT_BUTTON, + lambda event, mbridge=bridge: self.goto_bridge(mbridge), btn) + 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.Bind(wx.EVT_BUTTON, lambda event: self.discover_new_bridges(), btn) @@ -235,7 +247,6 @@ class Hui(wx.Frame): self.add_bridges() return found_any - def get_ok_cancel_answer_from_modal(self, message: str) -> bool: """Display a message dialog and return ok or cancel diff --git a/tinge/HueBridge/__init__.py b/tinge/HueBridge/__init__.py index 48e5212..813315c 100644 --- a/tinge/HueBridge/__init__.py +++ b/tinge/HueBridge/__init__.py @@ -12,7 +12,7 @@ class HueBridge: """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 Args: @@ -22,14 +22,20 @@ class HueBridge: """ self.m_ipaddress: str = ipaddress self.m_username: str = username + self.m_is_reachable = is_reachable if name: self.m_name: str = name else: self.m_name = self.m_ipaddress - self.m_lights: list[HueLight] = self.discover_lights() - self.discover_new_lights() - self.m_new_lights: list[HueLight] = self.get_new_lights() - self.m_groups: list[HueGroup] = self.discover_groups() + if is_reachable: + self.m_lights: list[HueLight] = self.discover_lights() + self.discover_new_lights() + 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: """The string representation of this bridge @@ -216,3 +222,6 @@ class HueBridge: for group in self.m_groups: group.remove_light(light) self.m_lights.remove(light) + + def is_reachable(self): + return self.m_is_reachable \ No newline at end of file diff --git a/tinge/__init__.py b/tinge/__init__.py index 0ab95ee..2f5c522 100644 --- a/tinge/__init__.py +++ b/tinge/__init__.py @@ -61,9 +61,11 @@ class Tinge: if 'error' in resp.keys(): m_keys = resp['error'].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'][ - 'address'] == "/lights" and resp['error']['type'] == 1: - discovered = True + # This is kinda ugly but line is too long with and statement + if resp['error']['description'] == "unauthorized user": + if resp['error']['address'] == "/lights": + if resp['error']['type'] == 1: + discovered = True except simplejson.errors.JSONDecodeError: pass @@ -89,9 +91,16 @@ class Tinge: mbridges = toml.loads(configfile.read()) for key, value in mbridges.items(): if key not in self.m_discovered: - bridge: HueBridge = HueBridge(key, value['user']) - self.m_bridges.append(bridge) - self.m_discovered.append(key) + response = make_request(key, "{}/".format(value['user'])) + if response: + 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): """Save to file