From 98d5648ab321cfc3c1bdb86c4e1d4ed915b8d673 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Sat, 4 Feb 2017 21:55:34 +1100 Subject: [PATCH 1/3] Use /my_cgi.cgi for current consumption on legacy protocol --- pyW215/pyW215.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pyW215/pyW215.py b/pyW215/pyW215.py index 4a26a8a..928538d 100644 --- a/pyW215/pyW215.py +++ b/pyW215/pyW215.py @@ -154,14 +154,32 @@ def SOAPAction(self, Action, responseElement, params = ""): self._error_report = False return value + def fetchMyCgi(self): + try: + response = urlopen(Request('http://{}/my_cgi.cgi'.format(self.ip), b'request=create_chklst')); + except (HTTPError, URLError): + _LOGGER.warning("Failed to open url to {}".format(self.ip)) + self._error_report = True + return None + + lines = response.readlines() + return {line.decode().split(':')[0].strip(): line.decode().split(':')[1].strip() for line in lines} + @property def current_consumption(self): """Get the current power consumption in Watt.""" res = 'N/A' - try: - res = self.SOAPAction('GetCurrentPowerConsumption', 'CurrentConsumption', self.moduleParameters("2")) - except: - return 'N/A' + if self.use_legacy_protocol: + # Use /my_cgi.cgi to retrieve current consumption + try: + res = self.fetchMyCgi()['Meter Watt'] + except: + return 'N/A' + else: + try: + res = self.SOAPAction('GetCurrentPowerConsumption', 'CurrentConsumption', self.moduleParameters("2")) + except: + return 'N/A' if res is None: return 'N/A' From 8f940c1db4574cd3b190e8cac2b6211a3a8a9a84 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Sat, 4 Feb 2017 21:58:04 +1100 Subject: [PATCH 2/3] Add docstring to #fetchMyCgi --- pyW215/pyW215.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyW215/pyW215.py b/pyW215/pyW215.py index 928538d..7a12508 100644 --- a/pyW215/pyW215.py +++ b/pyW215/pyW215.py @@ -155,6 +155,7 @@ def SOAPAction(self, Action, responseElement, params = ""): return value def fetchMyCgi(self): + """Fetches statistics from my_cgi.cgi""" try: response = urlopen(Request('http://{}/my_cgi.cgi'.format(self.ip), b'request=create_chklst')); except (HTTPError, URLError): From 277a22adee65b81dec02a1f6451deb1e20072508 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Sat, 4 Feb 2017 22:00:22 +1100 Subject: [PATCH 3/3] Update readme.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7a96e36..6b759c3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The library is largely inspired by the javascript implementation by @bikerp [dsp from pyW215 import SmartPlug sp = SmartPlug('192.168.1.110', '******') - + # Get values if available otherwise return N/A print(sp.current_consumption) print(sp.temperature) @@ -29,7 +29,7 @@ Note: You need to know the IP and password of you device. The password is writte Note: If you experience problems with the switch first try upgrading to the latest supported firmware through the D-Link app. If the problem persists feel free to open an issue about the problem. ## Partial support -* v1.24 (State changing working, but no support for reading temperature or current consumption) +* v1.24 (State changing and current consumption working, but no support for reading temperature) * D-Link W110 smart switch (only state changing is supported) If you have it working on other firmware or hardware versions please let me know.