Skip to content

Commit

Permalink
Added support for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
CJNE committed Sep 7, 2021
1 parent 14e47ca commit 1b49fe5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,44 @@ pip install pymyenergi -U

Setup will add a cli under the name myenergicli, see below for usage

## CLI usage
## CLI

A simple cli is provided with this library.

If no username or password is supplied as input arguments you will be prompted.
If no username or password is supplied as input arguments and no configuration file is found you will be prompted.
Conifguration file will be searched for in ./.myenergi.cfg and ~/.myenergi.cfg

### Example configuration file

```
[hub]
serial=12345678
password=yourpassword
```

### CLI usage

```
usage: cli.py [-h] [-u USERNAME] [-p PASSWORD] [-k KIND] [-d] {list}
usage: myenergi [-h] [-u USERNAME] [-p PASSWORD] [-d] [-j]
{list,overview,zappi,eddi,harvi} ...
myenergi CLI.
positional arguments:
{list}
{list,overview,zappi,eddi,harvi}
sub-command help
list list devices
overview show overview
zappi use zappi --help for available commands
eddi use eddi --help for available commands
harvi use harvi --help for available commands
optional arguments:
-h, --help show this help message and exit
-u USERNAME, --username USERNAME
-p PASSWORD, --password PASSWORD
-k KIND, --kind KIND
-d, --debug
-j, --json
```

## Library usage
Expand Down
37 changes: 28 additions & 9 deletions pymyenergi/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import argparse
import asyncio
import configparser
import json
import logging
import os
import sys
from getpass import getpass

Expand Down Expand Up @@ -98,35 +100,52 @@ async def main(args):
f"Start smart boosting with {args.arg[0]}kWh complete by {args.arg[1]}"
)
else:
sys.exit("A serial number is needed")
sys.exit(
"Dont know what to do, type myenergi --help form available commands"
)
except WrongCredentials:
sys.exit("Wrong username or password")


def cli():
config = configparser.ConfigParser()
config["hub"] = {"serial": "", "password": ""}
config.read([".myenergi.cfg", os.path.expanduser("~/.myenergi.cfg")])
parser = argparse.ArgumentParser(prog="myenergi", description="myenergi CLI.")
parser.add_argument("-u", "--username", dest="username", default=None)
parser.add_argument("-p", "--password", dest="password", default=None)
parser.add_argument(
"-u",
"--username",
dest="username",
default=config.get("hub", "serial"),
)
parser.add_argument(
"-p", "--password", dest="password", default=config.get("hub", "password")
)
parser.add_argument("-d", "--debug", dest="debug", action="store_true")
parser.add_argument("-j", "--json", dest="json", action="store_true", default=False)
subparsers = parser.add_subparsers(dest="command", help="sub-command help")
subparser_list = subparsers.add_parser("list", help="list help")
subparser_list = subparsers.add_parser("list", help="list devices")
subparser_list.add_argument("-k", "--kind", dest="kind", default="all")
subparsers.add_parser("overview", help="overview help")
subparsers.add_parser("energy", help="energy help")
subparser_zappi = subparsers.add_parser("zappi", help="zappi help")
subparsers.add_parser("overview", help="show overview")
subparser_zappi = subparsers.add_parser(
"zappi", help="use zappi --help for available commands"
)
subparser_zappi.add_argument("serial", default=None)
subparser_zappi.add_argument(
"action",
choices=["show", "energy", "stop", "mode", "boost", "smart-boost", "mingreen"],
)
subparser_zappi.add_argument("arg", nargs="*")
subparser_eddi = subparsers.add_parser("eddi", help="eddi help")
subparser_eddi = subparsers.add_parser(
"eddi", help="use eddi --help for available commands"
)
subparser_eddi.add_argument("serial", default=None)
subparser_eddi.add_argument("action", choices=["show", "energy"])
subparser_eddi.add_argument("arg", nargs="*")

subparser_harvi = subparsers.add_parser("harvi", help="harvi help")
subparser_harvi = subparsers.add_parser(
"harvi", help="use harvi --help for available commands"
)
subparser_harvi.add_argument("serial", default=None)
subparser_harvi.add_argument("action", choices=["show"])
subparser_harvi.add_argument("arg", nargs="*")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="pymyenergi",
version="0.0.12",
version="0.0.13",
author="Johan Isaksson",
author_email="johan@generatorhallen.se",
description="Python library and CLI for communicating with myenergi API.",
Expand Down

0 comments on commit 1b49fe5

Please sign in to comment.