From 2d5da3e9589d5d0c71668da2a1ed51f9f9d2fc60 Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Tue, 25 Apr 2017 00:32:31 -0400 Subject: [PATCH 1/8] Catch `KeyError`; Add `response.text` to error message --- homeassistant/components/device_tracker/tplink.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index 8d476136d23..b1ad68af3aa 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -195,8 +195,9 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): self.sysauth = regex_result.group(1) _LOGGER.info(self.sysauth) return True - except ValueError: - _LOGGER.error("Couldn't fetch auth tokens!") + except (ValueError, KeyError) as e: + _LOGGER.error("Couldn't fetch auth tokens!" + "Response was: {}".format(response.text)) return False @Throttle(MIN_TIME_BETWEEN_SCANS) From 450fd7f2b5efb7408668f9713991a39fe782b0cd Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Tue, 25 Apr 2017 00:34:26 -0400 Subject: [PATCH 2/8] Log out of router admin interface after devices are recorded. --- .../components/device_tracker/tplink.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index b1ad68af3aa..770d5f22a4d 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -162,6 +162,7 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): def scan_devices(self): """Scan for new devices and return a list with found device IDs.""" self._update_info() + self._log_out() return self.last_results.keys() # pylint: disable=no-self-use @@ -251,6 +252,21 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): return False + def _log_out(self): + with self.lock: + _LOGGER.info("Logging out of router admin interface...") + + url = ('http://{}/cgi-bin/luci/;stok={}/admin/system?' + 'form=logout').format(self.host, self.stok) + referer = 'http://{}/webpages/index.html'.format(self.host) + + response = requests.post(url, + params={'operation': 'write'}, + headers={'referer': referer}, + cookies={'sysauth': self.sysauth}) + self.stok = '' + self.sysauth = '' + class Tplink4DeviceScanner(TplinkDeviceScanner): """This class queries an Archer C7 router with TP-Link firmware 150427.""" From 943861a8a3105d28c02707ae6dde2fd6d688342c Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Tue, 25 Apr 2017 00:45:59 -0400 Subject: [PATCH 3/8] Remove unused var --- homeassistant/components/device_tracker/tplink.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index 770d5f22a4d..1aeabbea15c 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -260,10 +260,10 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): 'form=logout').format(self.host, self.stok) referer = 'http://{}/webpages/index.html'.format(self.host) - response = requests.post(url, - params={'operation': 'write'}, - headers={'referer': referer}, - cookies={'sysauth': self.sysauth}) + requests.post(url, + params={'operation': 'write'}, + headers={'referer': referer}, + cookies={'sysauth': self.sysauth}) self.stok = '' self.sysauth = '' From 8bf1c217386b3e12b745873f721adc99ec19a927 Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Tue, 25 Apr 2017 00:48:23 -0400 Subject: [PATCH 4/8] Add space --- homeassistant/components/device_tracker/tplink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index 1aeabbea15c..d80213dd549 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -198,7 +198,7 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): return True except (ValueError, KeyError) as e: _LOGGER.error("Couldn't fetch auth tokens!" - "Response was: {}".format(response.text)) + " Response was: {}".format(response.text)) return False @Throttle(MIN_TIME_BETWEEN_SCANS) From b6827ce57a92955dcb9886b82f23a0af43a0e9e0 Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Sun, 30 Apr 2017 21:02:03 -0400 Subject: [PATCH 5/8] Use throwaray variable name --- homeassistant/components/device_tracker/tplink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index d80213dd549..560babe3d61 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -196,7 +196,7 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): self.sysauth = regex_result.group(1) _LOGGER.info(self.sysauth) return True - except (ValueError, KeyError) as e: + except (ValueError, KeyError) as _: _LOGGER.error("Couldn't fetch auth tokens!" " Response was: {}".format(response.text)) return False From dd7690f26552f94b3333caabf02e50c9d88e3f48 Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Sun, 30 Apr 2017 21:31:55 -0400 Subject: [PATCH 6/8] Use % formatting --- homeassistant/components/device_tracker/tplink.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index 560babe3d61..032e5db8247 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -198,7 +198,7 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): return True except (ValueError, KeyError) as _: _LOGGER.error("Couldn't fetch auth tokens!" - " Response was: {}".format(response.text)) + " Response was: %s" % response.text) return False @Throttle(MIN_TIME_BETWEEN_SCANS) From bc0559813c7ccb091444ebdcdc0cfa082e1aaf9f Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Sun, 30 Apr 2017 22:26:16 -0400 Subject: [PATCH 7/8] Dont add two strings inside logger call --- homeassistant/components/device_tracker/tplink.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index 032e5db8247..c456d5d7adc 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -197,8 +197,8 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): _LOGGER.info(self.sysauth) return True except (ValueError, KeyError) as _: - _LOGGER.error("Couldn't fetch auth tokens!" - " Response was: %s" % response.text) + m = "Couldn't fetch auth tokens! Response was: %s" % response.text + _LOGGER.error(m) return False @Throttle(MIN_TIME_BETWEEN_SCANS) From 7a24e210ae74dd8c7a9cecdf6b270bb8646b144f Mon Sep 17 00:00:00 2001 From: Jack Minardi Date: Mon, 1 May 2017 09:31:23 -0400 Subject: [PATCH 8/8] Try again to pass string to error msg --- homeassistant/components/device_tracker/tplink.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/device_tracker/tplink.py b/homeassistant/components/device_tracker/tplink.py index c456d5d7adc..f38e7d07b45 100755 --- a/homeassistant/components/device_tracker/tplink.py +++ b/homeassistant/components/device_tracker/tplink.py @@ -197,8 +197,8 @@ class Tplink3DeviceScanner(TplinkDeviceScanner): _LOGGER.info(self.sysauth) return True except (ValueError, KeyError) as _: - m = "Couldn't fetch auth tokens! Response was: %s" % response.text - _LOGGER.error(m) + _LOGGER.error("Couldn't fetch auth tokens! Response was: %s", + response.text) return False @Throttle(MIN_TIME_BETWEEN_SCANS)