Skip to content

Commit e4c8693

Browse files
LinuxChristianChristian Fredborg Brædstrup
authored andcommitted
Merge pull request #10 from nvella/consumption-through-mycgi
Consumption through my_cgi for legacy firmware.
2 parents c581f76 + 277a22a commit e4c8693

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The library is largely inspired by the javascript implementation by @bikerp [dsp
99
from pyW215 import SmartPlug
1010

1111
sp = SmartPlug('192.168.1.110', '******')
12-
12+
1313
# Get values if available otherwise return N/A
1414
print(sp.current_consumption)
1515
print(sp.temperature)
@@ -29,7 +29,7 @@ Note: You need to know the IP and password of you device. The password is writte
2929
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.
3030

3131
## Partial support
32-
* v1.24 (State changing working, but no support for reading temperature or current consumption)
32+
* v1.24 (State changing and current consumption working, but no support for reading temperature)
3333
* D-Link W110 smart switch (only state changing is supported)
3434

3535
If you have it working on other firmware or hardware versions please let me know.

pyW215/pyW215.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,33 @@ def SOAPAction(self, Action, responseElement, params = ""):
154154
self._error_report = False
155155
return value
156156

157+
def fetchMyCgi(self):
158+
"""Fetches statistics from my_cgi.cgi"""
159+
try:
160+
response = urlopen(Request('http://{}/my_cgi.cgi'.format(self.ip), b'request=create_chklst'));
161+
except (HTTPError, URLError):
162+
_LOGGER.warning("Failed to open url to {}".format(self.ip))
163+
self._error_report = True
164+
return None
165+
166+
lines = response.readlines()
167+
return {line.decode().split(':')[0].strip(): line.decode().split(':')[1].strip() for line in lines}
168+
157169
@property
158170
def current_consumption(self):
159171
"""Get the current power consumption in Watt."""
160172
res = 'N/A'
161-
try:
162-
res = self.SOAPAction('GetCurrentPowerConsumption', 'CurrentConsumption', self.moduleParameters("2"))
163-
except:
164-
return 'N/A'
173+
if self.use_legacy_protocol:
174+
# Use /my_cgi.cgi to retrieve current consumption
175+
try:
176+
res = self.fetchMyCgi()['Meter Watt']
177+
except:
178+
return 'N/A'
179+
else:
180+
try:
181+
res = self.SOAPAction('GetCurrentPowerConsumption', 'CurrentConsumption', self.moduleParameters("2"))
182+
except:
183+
return 'N/A'
165184

166185
if res is None:
167186
return 'N/A'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup
22

33
setup(name='pyW215',
4-
version='0.3.7',
4+
version='0.4',
55
description='Interface for d-link W215 Smart Plugs.',
66
url='https://github.com/linuxchristian/pyW215',
77
author='Christian Juncker Brædstrup',

0 commit comments

Comments
 (0)