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. diff --git a/pyW215/pyW215.py b/pyW215/pyW215.py index 4a26a8a..7a12508 100644 --- a/pyW215/pyW215.py +++ b/pyW215/pyW215.py @@ -154,14 +154,33 @@ def SOAPAction(self, Action, responseElement, params = ""): self._error_report = False 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): + _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' diff --git a/setup.py b/setup.py index de9af96..aa8a4d6 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup(name='pyW215', - version='0.3.7', + version='0.4', description='Interface for d-link W215 Smart Plugs.', url='https://github.com/linuxchristian/pyW215', author='Christian Juncker Brædstrup',