Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setup.py and tox.ini. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Control your [Energenie EG-PMS-LAN](https://energenie.com/item.aspx?id=6668) with python.

## geniecli.py CLI
## geniecli CLI

**Usage:**

```
~/energenie-connect0r$ ./geniecli.py --help
usage: geniecli.py [-h] [--status] [--nologout] [--toggle {1,2,3,4}]
geniecli --help
usage: geniecli [-h] [--status] [--nologout] [--toggle {1,2,3,4}]
[--on {1,2,3,4}] [--off {1,2,3,4}] [--restart {1,2,3,4}]
[--restartdelaysec RESTARTDELAYSEC]
http://192.168.2.5 pwd
Expand All @@ -30,21 +30,21 @@ optional arguments:
**Get device status in JSON format:**

```
~/energenie-connect0r$ ./geniecli.py http://192.168.2.5 passw0rd --status
geniecli http://192.168.2.5 passw0rd --status
{"status": true, "data": {"sockets": [1, 0, 1, 1]}, "message": null}
```

**Switch socket with `--on` or `--off`:**

```
~/energenie-connect0r$ ./geniecli.py http://192.168.2.5 passw0rd --on 2
geniecli http://192.168.2.5 passw0rd --on 2
{"status": true, "message": "State of socket 2 changed", "data": {"sockets": [1, 1, 1, 1]}}
```

**Toggle socket (if on then off; if off then on):**

```
~/energenie-connect0r$ ./geniecli.py http://192.168.2.5 passw0rd --toggle 2
geniecli http://192.168.2.5 passw0rd --toggle 2
{"status": true, "message": "State of socket 2 changed to 0", "data": {"sockets": [1, 0, 1, 1]}}
```

Expand All @@ -55,7 +55,7 @@ optional arguments:
- Switch socket on

```
~/energenie-connect0r$ ./geniecli.py http://192.168.2.5 passw0rd --restart 2 --restartdelaysec 3
geniecli http://192.168.2.5 passw0rd --restart 2 --restartdelaysec 3
{"data": {"sockets": [1, 1, 1, 1]}, "message": "Socket 2 restarted", "status": true}
```

Expand All @@ -69,22 +69,22 @@ you can use the `--nologout` option to prevent collisions.
#!/bin/bash

# execute two commands parallel
./geniecli.py http://192.168.2.7 "" --toggle 3 --nologout &
./geniecli.py http://192.168.2.7 "" --toggle 4 --nologout &
geniecli http://192.168.2.7 "" --toggle 3 --nologout &
geniecli http://192.168.2.7 "" --toggle 4 --nologout &

# wait for both to finish
wait

# logout
./geniecli.py http://192.168.2.7 "" --logout
geniecli http://192.168.2.7 "" --logout
```

## geniemassstatus.py CLI
## geniemassstatus CLI

Fetch the socket status from multiple energenie devices:

```
~/energenie-connect0r$ ./geniemassstatus.py --host http://192.168.2.5 --pwd passw0rd --host http://192.168.2.7 --pwd passw0rd
geniemassstatus --host http://192.168.2.5 --pwd passw0rd --host http://192.168.2.7 --pwd passw0rd
{"http://192.168.2.7": [1, 1, 1, 0], "http://192.168.2.5": [1, 1, 1, 1]}
```

Expand All @@ -107,7 +107,7 @@ status = eg.getstatus()
# 3 = Unknown login result

# Login if not logged in and get status again
if status['login']==1:
if status['login'] == 1:
if eg.login():
status = eg.getstatus()

Expand All @@ -120,7 +120,7 @@ print(status)
eg.changesocket(2, 1)

# Logout
if status['login']==0:
eg.logout()
if status['login'] == 0:
eg.logout()
```

1 change: 1 addition & 0 deletions energenieconnector/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from energenieconnector.energenieconnector import EnergenieConnector
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import re


class EnergenieConnector:

def __init__(self, baseurl, pwd):
Expand Down
75 changes: 39 additions & 36 deletions geniecli.py → energenieconnector/geniecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
from energenieconnector import EnergenieConnector


# Parse command line arguments
def getargs():
parser = argparse.ArgumentParser()
Expand All @@ -14,10 +15,10 @@ def getargs():
parser.add_argument('--status', action='store_true', help='fetch socket status')
parser.add_argument('--logout', action='store_true', help='just log out and exit script')
parser.add_argument('--nologout', action='store_true', help='do not logout')
parser.add_argument('--toggle', type=int, choices=range(1,5), help='toggle socket state')
parser.add_argument('--on', type=int, choices=range(1,5), help='switch socket on')
parser.add_argument('--off', type=int, choices=range(1,5), help='switch socket off')
parser.add_argument('--restart', type=int, choices=range(1,5), help='restart socket')
parser.add_argument('--toggle', type=int, choices=range(1, 5), help='toggle socket state')
parser.add_argument('--on', type=int, choices=range(1, 5), help='switch socket on')
parser.add_argument('--off', type=int, choices=range(1, 5), help='switch socket off')
parser.add_argument('--restart', type=int, choices=range(1, 5), help='restart socket')
parser.add_argument('--restartdelaysec', type=int, default=5, help='delay between restart off and on')
args = parser.parse_args()
return args
Expand All @@ -31,90 +32,92 @@ def run(args):
status = eg.getstatus()

# Just log out
if args.logout and status['login']==0:
if args.logout and status['login'] == 0:
eg.logout()
print(json.dumps({ "status": True, "message": "Logout successful", "data": None }))
print(json.dumps({"status": True, "message": "Logout successful", "data": None}))
return 0
# Peform login if not logged in
if status['login']==1:

# Perform login if not logged in
if status['login'] == 1:
if eg.login():
status = eg.getstatus()
else:
print(json.dumps({ "status": False, "message": "Login failed", "data": None }))
print(json.dumps({"status": False, "message": "Login failed", "data": None}))

# Get socket states
if args.status:
print(json.dumps({
"status": status['login']==0,
"message": None if status['login']==0 else status['logintxt'],
"data": { "sockets": status['sockets'] }
print(json.dumps({
"status": status['login'] == 0,
"message": None if status['login'] == 0 else status['logintxt'],
"data": {"sockets": status['sockets']}
}))

# Switch socket on or off
elif args.on is not None or args.off is not None:
targetstate = 1 if args.on is not None else 0
targetsocket = args.on if args.on is not None else args.off

if targetstate!=status['sockets'][targetsocket-1]:
if targetstate != status['sockets'][targetsocket - 1]:
# Perform socket state change
eg.changesocket(targetsocket, targetstate)
status = eg.getstatus()
print(json.dumps({
"status": status.get("login")==0,
"message": "State of socket "+str(targetsocket)+" changed",
"data": { "sockets": status.get("sockets") }
print(json.dumps({
"status": status.get("login") == 0,
"message": "State of socket " + str(targetsocket) + " changed",
"data": {"sockets": status.get("sockets")}
}))
else:
# No change necessary
print(json.dumps({
"status": False,
"message": "State of socket "+str(targetsocket)+" is already "+str(targetstate),
"data": { "sockets": status.get("sockets") }
print(json.dumps({
"status": False,
"message": "State of socket " + str(targetsocket) + " is already " + str(targetstate),
"data": {"sockets": status.get("sockets")}
}))

# Toggle socket
elif args.toggle is not None:
targetstate = 1 if status['sockets'][args.toggle-1]==0 else 0
targetstate = 1 if status['sockets'][args.toggle - 1] == 0 else 0
targetsocket = args.toggle
eg.changesocket(targetsocket, targetstate)
status = eg.getstatus()
print(json.dumps({
"status": status.get("login")==0,
"message": "State of socket "+str(targetsocket)+" changed to "+str(targetstate),
"data": { "sockets": status.get("sockets") }
"status": status.get("login") == 0,
"message": "State of socket " + str(targetsocket) + " changed to " + str(targetstate),
"data": {"sockets": status.get("sockets")}
}))

# Restart socket
elif args.restart is not None:
sock = args.restart
if status['sockets'][sock-1]==1:
if status['sockets'][sock - 1] == 1:
eg.changesocket(sock, 0)
time.sleep(args.restartdelaysec)
eg.changesocket(sock, 1)

status = eg.getstatus()
print(json.dumps({
"status": status.get("login")==0,
"message": "Socket "+str(sock)+" restarted",
"data": { "sockets": status.get("sockets") }
"status": status.get("login") == 0,
"message": "Socket " + str(sock) + " restarted",
"data": {"sockets": status.get("sockets")}
}))

# No action
else:
print(json.dumps({ "status":False, "message":"Nothing to do", "data":{ "sockets": status.get("sockets") } }))

print(json.dumps({"status": False, "message": "Nothing to do", "data": {"sockets": status.get("sockets")}}))

# Logout
if status.get("login")==0 and args.nologout==False:
if status.get("login") == 0 and args.nologout == False:
eg.logout()

return 0


# Parse arguments and run main function
if __name__ == "__main__":
# Parse arguments and run cli function
def cli():
shellargs = getargs()
result = run(shellargs)
sys.exit(result)


if __name__ == "__main__":
cli()
9 changes: 7 additions & 2 deletions geniemassstatus.py → energenieconnector/geniemassstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
from energenieconnector import EnergenieConnector


# Parse command line arguments
def getargs():
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -34,9 +35,13 @@ def run(args):
return 0


# Parse arguments and run main function
if __name__ == "__main__":
# Parse arguments and run cli function
def cli():
shellargs = getargs()
result = run(shellargs)
sys.exit(result)


if __name__ == "__main__":
cli()

34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from setuptools import find_packages
import setuptools


with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name="energenieconnector",
version="1.0.0",
author="perryflynn",
author_email="[email protected]",
description="Control your Energenie EG-PMS-LAN with Python.",
long_description=long_description,
license="MIT License",
long_description_content_type="text/markdown",
url="https://github.com/perryflynn/energenie-connect0r",
project_urls={
"Bug Tracker": "https://github.com/perryflynn/energenie-connect0r/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
packages=find_packages(),
python_requires=">=3.6",
entry_points={
'console_scripts': [
'geniecli = energenieconnector.geniecli:cli',
'geniemassstatus = energenieconnector.geniemassstatus:cli',
],
}
)
24 changes: 24 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# content of: tox.ini , put in same dir as setup.py
[tox]
envlist = py38
# Apparently there is no way to make tox use bdist_wheel by default
# So skip it and do manually as a command
# skipsdist = True

[testenv]
# Wheel 0.30 because of Danfoss file manager that does not support
# newer wheel format. Keep it as long as it does not cause any bad
# side-effects.
deps = requests

# Using Typhoon-HIL-API branch for increased timeout for slow QA VMs.
# Once it gets merged and uploaded to PyPI we can remove the pip git install line
commands =
python setup.py build_ext bdist_wheel clean --all
# pip uninstall -y typhoontest typhoon-hil-api
# python create_timestamp.py
# python setup.py build_ext bdist_wheel clean --all
# pip install ..\api
# pip install --find-links={toxinidir}/dist typhoontest
# python verify_wheel.py
# python -m pytest -x {posargs:} tests -o log_cli=False --log-cli-level=INFO