Skip to content

Commit

Permalink
Merge pull request #10 from nvella/consumption-through-mycgi
Browse files Browse the repository at this point in the history
Consumption through my_cgi for legacy firmware.
  • Loading branch information
LinuxChristian authored and Christian Fredborg Brædstrup committed Feb 10, 2017
2 parents c581f76 + 277a22a commit e4c8693
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
27 changes: 23 additions & 4 deletions pyW215/pyW215.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit e4c8693

Please sign in to comment.