GithubHelp home page GithubHelp logo

python-dlipower's People

Contributors

dfransen avatar dwighthubbard avatar lanquarden avatar sly74fr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-dlipower's Issues

Login Issue

Hi,

I just setup python module for the web power switch and wanted to run the sample code to turn off the a outlet.
Following is the code I am using:
from future import print_function
import dlipower

print('Connecting to a DLI PowerSwitch')
switch = dlipower.PowerSwitch(hostname="http://192.168.0.100/", userid="admin",password="1234")

print('Turning off the first outlet')
switch.off(1)

And following is the error log:

$ python HelloWebPowerSwitch.py
Connecting to a DLI PowerSwitch
Turning off the first outlet
Traceback (most recent call last):
File "HelloWebPowerSwitch.py", line 8, in
switch.off(1)
File "/usr/local/lib/python2.7/dist-packages/dlipower-0.7.152-py2.7.egg/dlipower/dlipower.py", line 341, in off
self.geturl(url='outlet?%d=OFF' % self.determine_outlet(outlet))
File "/usr/local/lib/python2.7/dist-packages/dlipower-0.7.152-py2.7.egg/dlipower/dlipower.py", line 311, in determine_outlet
if outlet_int <= 0 or outlet_int > self.len():
File "/usr/local/lib/python2.7/dist-packages/dlipower-0.7.152-py2.7.egg/dlipower/dlipower.py", line 188, in len
self.__len = len(self.statuslist())
TypeError: object of type 'NoneType' has no len()

I think the root cause it its unable to login the web power switch, hence erroring out when I try to access a outlet.

Kindly let me know, how I can fix this issue..

Thanks,
Sushant

Hang after a while...

Re-hello,

I discovered a strong issue with a test script that queries each outlet status forever.
After a random amount of time (between 1 and less than 5 minutes), the script is stuck for no apparent reason ☚ī¸
Any idea ?

I even try to recreate the dqlipower.PowerSwitch object at each turn without better behavior...

test_wrapper.txt

Best regards

dlipower.py doesn't throw exception on DNS failure

I'm using the digitialloggers integration with Home Assistant (https://www.home-assistant.io/integrations/digitalloggers/), and it looks like it doesn't throw an exception on DNS lookup failures. The exception at the following line only catches exceptions from the requests module, it won't catch ConnectionError exceptions:
https://github.com/dwighthubbard/python-dlipower/blob/master/dlipower/dlipower.py#L416

There are several exceptions in requests that are not subclasses of RequestException: https://2.python-requests.org/en/v3.0.0/_modules/requests/exceptions/

When using dqlipower.py under Home Assistant (which should be based on 0.7.165), it is throwing the following error:

2020-05-20 13:43:48 ERROR (MainThread) [homeassistant.components.switch] Error w
hile setting up digitalloggers platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 1
78, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/digitalloggers/switch.py
", line 61, in setup_platform
    if not power_switch.verify():
  File "/usr/local/lib/python3.7/site-packages/dlipower/dlipower.py", line 352, in verify
    if self.geturl():
  File "/usr/local/lib/python3.7/site-packages/dlipower/dlipower.py", line 373, in geturl
    logger.debug('Response code: %s', request.status_code)
AttributeError: 'NoneType' object has no attribute 'status_code

If I use the IP address instead of the hostname, that error goes away.

No timeout ?

Hello,

First thank you very much for this very useful module !
Browsing your code, I believe the timeout setting is never passed to request.get(...)
Am I missing something here ?

Best regards,
Sly

Powerswitch connection fails from Python script when running from Jenkins

I am trying to control the web socket switch from my python script - when running the script directly on the test system everything works fine as expected but when trying to trigger the same from Jenkins, the script is throwing 'Challenge' error when trying to open the connection.

All the other python modules works as expected.

Is there anything else needs to be handled or passed when running from Jenkins pipeline(Python script called from a Batch file)?

Outlet number 5 out of range

If there is a user setup on the switch with access to only 3 outlets (2,3 and 5 for example) then if you try to operate outlet 5 you get

File "C:\xxx\dlipower\dlipower.py", line 431, in determine_outlet
raise DLIPowerException('Outlet number %d out of range' % outlet_int)
dlipower.dlipower.DLIPowerException: Outlet number 5 out of range

It assumes because it can only see 3 outlets they should be numbered 1,2,3

The relevant variables are:

outlet =5
outlets=[[2, '4-2', 'ON'], [3, '4-3', 'ON'], [5, '4-5', 'ON']]
len=3

Without changing the code the only solution seems to be to use 'admin' user.

Crash on lost connection

No handlers could be found for logger "dlipower.dlipower"
Traceback (most recent call last):
File "test_wrapper.py", line 23, in
pdu.off(i)
File "/home/xtramgr/Dev/VirtualEnvs/AIT/local/lib/python2.7/site-packages/dlipower/dlipower.py", line 419, in off
self.geturl(url='outlet?%d=OFF' % self.determine_outlet(outlet))
File "/home/xtramgr/Dev/VirtualEnvs/AIT/local/lib/python2.7/site-packages/dlipower/dlipower.py", line 381, in determine_outlet
outlets = self.statuslist()
File "/home/xtramgr/Dev/VirtualEnvs/AIT/local/lib/python2.7/site-packages/dlipower/dlipower.py", line 447, in statuslist
url = self.geturl('index.htm')
File "/home/xtramgr/Dev/VirtualEnvs/AIT/local/lib/python2.7/site-packages/dlipower/dlipower.py", line 373, in geturl
logger.debug('Response code: %s', request.status_code)
AttributeError: 'NoneType' object has no attribute 'status_code'

No Logout function for disconnection

I'm trying to supplement the code with a logout function on my own. This code is a little above my skill level and was hoping you could tell me which packages and methods directly I'd need to write a logout method.

I see BeautifulSoup and urllib3 for web scraping, but I'm not sure how to activate the href for logout as they don't do direct clicking like Selenium would (from what I understand). The direct endpoint is http://192.168.0.100/logout on the default setup.

Once I finish I'd love to contribute it to the project.

Thank you,

1.0.176 doesn't handle missing switch gracefully

I have a script that imports dlipower and runs on all of my hosts, but not all of my hosts have the webswitch installed on them. If a switch happens to be disconnected (or not connected at all), I get a big error when trying to connect to it. I noticed that there's a verify() method, but a call to that still generates the error. I also then tried running dlipower at the command prompt and it looks like specifying the wrong IP also generates an error. It would be really nice if the module would handle this a bit more gracefully, or at least make it so that the verify() method would handle this gracefully such that I can instantiate the class and call the verify() method to make sure the switch is available. Here's the output (I intentionally used the wrong IP to simulate a missing switch):

`$ dlipower --hostname=192.168.0.101
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 137, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 91, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 81, in create_connection
sock.connect(sa)
OSError: [Errno 113] No route to host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 560, in urlopen
body=body, headers=headers)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 162, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 146, in _new_conn
self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7fb3ccf59518>: Failed to establish a new connection: [Errno 113] No route to host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 376, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 610, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 273, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.0.101', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb3ccf59518>: Failed to establish a new connection: [Errno 113] No route to host',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/test/.local/bin/dlipower", line 111, in
cycletime=options.cycletime, use_https=options.ssl
File "/home/test/.local/lib/python3.5/site-packages/dlipower/dlipower.py", line 262, in init
self.login()
File "/home/test/.local/lib/python3.5/site-packages/dlipower/dlipower.py", line 325, in login
response = self.session.get(self.base_url, verify=False)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 492, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 480, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 588, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 437, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.0.101', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb3ccf59518>: Failed to establish a new connection: [Errno 113] No route to host',))
`

Version tags

Would it be possible to get version tags that are the same as in PyPI?

NameError when `set_outlet_name` used with hyphen

I expect dlipower set_outlet_name 1 foo-bar to set the outlet name to "foo-bar", but instead it throws this NameError exception:

$ dlipower set_outlet_name 1 "foo-bar"
Traceback (most recent call last):
  File "/home/.../dlipower", line 117, in <module>
    outlet_range = _block_to_list(','.join(args[1:]))
  File "/home/.../dlipower", line 49, in _block_to_list
    for value in outlet_range(int(val1), int(val2) + 1):
NameError: name 'outlet_range' is not defined

Setting to "foobar" works. I suspect the name is being interpreted as some kind of range but it's not clear how to escape the - character to avoid this behaviour.

Speed issue

I am running below script. First of all, I did not find how to turn on all outputs from PowerSwitch object so that is why I am iterating through all outputs.
The issue is that when I run the script, I see about 3 second delay between each output turning on... I think it is processing time because I tried to change the sequency delay configuration on the DIN Relay unit to 0 seconds and still I got same results. So I wanted to ask if you have any suggestions?

import dlipower
rl1 = dlipower.PowerSwitch(hostname='192.168.0.100',password='1234')
for rl in rl1:
	rl.state='ON'

Debug output line causes AttributeError

This line of code is causing an attibute error.

logger.debug('Response code: %s', request.status_code)

logger.debug('Response code: %s', request.status_code)

request is not guaranteed to have a value and so if the request fails, the code is trying to get the 'status_code' attribute of None which will result in an AttributeError exception being raised.

Example:

AttributeError: 'NoneType' object has no attribute 'status_code'

Doesn't work with new "Pro" switch

Something has apparently changed in the new LPC9 "pro" version of the switch (which also has wireless now) because the dlipower script won't connect to it.

If I do a curl against my old LPC7 switch, without authentication, I get this:

$ curl http://192.168.1.4/index.htm
<HTML><HEAD>

<META HTTP-EQUIV="refresh" content="0; URL=/">

</HEAD><BODY></BODY>

And if I include authentication, I get the actual index page (which dlipower would then parse). But with the LPC9, if I do a curl with (or without) authentication, I get this:

$ curl http://192.168.1.5/index.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="/style.css">
<title>Redirecting...</title>

<META HTTP-EQUIV="refresh" content="0; URL=/">

</head>

<body>
</body>

</html>

It's not actually loading the index.htm page. Any suggestions on how I could fix this? The web UI of the LPC9 looks pretty much the same as the LPC7. It just has some additional features like wireless, event notifications, a new scripting engine, etc. The basic outlet control seems the same.

Thanks!

Python 3 will not connect

I developed a python 2.7 application that made use of this library (which was very helpful). However, the project recently switched to python 3.5. After the switch, we noticed that our switch no longer connected with the library. Instead:

>>> import dlipower
>>> switch = dlipower.PowerSwitch(hostname='ourhostname', userid='admin', password='*****')
>>> switch
Digital Loggers Web Powerswitch ourhostname (UNCONNECTED)

The library still behaves correctly if these same commands are used in 2.7.

Unable to login

I am seeing below message when I try to connect using dlipower.
dlipower.PowerSwitch(hostname="10.1.48.11", userid="admin", password="1234")
Digital Loggers Web Powerswitch 10.1.48.11 (UNCONNECTED)

Also on the server log below I see below error message for every attempt using dlipower module.
Jun 19 21:20:26 LPC970 user.notice auth[1494]: login denied for [email protected] (plaintext): password check failed
Jun 19 21:20:26 LPC970 user.notice www[1510]: login admin failed from 10.1.48.158

Any help on this would be much appreciated.

Allow legacy plaintext login methods required to login

Hello

Unless Allow legacy plaintext login methods is enabled on the switch, authentication fails when attempting to login when using the following call from python:
dlipower.PowerSwitch(hostname=switch_hostname, userid=userid, password=password, use_https= use_https)

The switch reports the following:
Mon Nov 27 13:09:36 2023 user.notice auth[1525]: login denied for @ (plaintext): password check failed
Mon Nov 27 13:09:36 2023 user.notice www[2153]: login sect failed from

Installation via pip fails

$ pip install dlipower 
Downloading/unpacking dlipower
  Downloading dlipower-0.2.75.linux-x86_64.tar.gz
  Running setup.py egg_info for package dlipower
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
    IOError: [Errno 2] No such file or directory: '/home/mike/build/dlipower/setup.py'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 14, in <module>

IOError: [Errno 2] No such file or directory: '/home/mike/build/dlipower/setup.py'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.