diff --git a/README.md b/README.md index d5e1ba4..face139 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,16 @@ A python module for reading and changing status of panasonic climate devices thr ## Command line usage ``` -usage: pcomfortcloud.py [-h] [-t TOKEN] username password {list,get,set} ... +usage: pcomfortcloud.py [-h] [-t TOKEN] username password {list,getall,get,set} ... Read or change status of Panasonic Climate devices positional arguments: username Username for Panasonic Comfort Cloud password Password for Panasonic Comfort Cloud - {list,get,set,dump} commands + {list,getall,get,set,dump} commands list Get a list of all devices + getall Get status of all devices get Get status of a device set Set status of a device dump Dump raw data of a device @@ -22,6 +23,8 @@ optional arguments: -h, --help show this help message and exit -t TOKEN, --token TOKEN File to store token in + -a AUTHFILE, --authfile AUTHFILE + File to store credentials in (instead of using username and password) -s [BOOL], --skipVerify [BOOL] Skip Ssl verification -r [BOOL], --raw [BOOL] @@ -119,3 +122,13 @@ can be found at https://pypi.org/project/pcomfortcloud/ ### How to publish package; - `python .\setup.py sdist bdist_wheel` - `python -m twine upload dist/*` + +## Auth File +Instead of specifying the username and password on the command line, they can optionally, be stored in a YAML file. +To use the auth file, on the command line, set the username to authfile and set the password to the full path of the YAML file + +The format of the auth file is: +``` +username: USERNAME +password: PASSWORD +``` \ No newline at end of file diff --git a/pcomfortcloud/__main__.py b/pcomfortcloud/__main__.py index b2a19a6..a1d3b1a 100644 --- a/pcomfortcloud/__main__.py +++ b/pcomfortcloud/__main__.py @@ -1,5 +1,6 @@ import argparse import json +import yaml import pcomfortcloud from enum import Enum @@ -37,11 +38,11 @@ def main(): parser.add_argument( 'username', - help='Username for pcomfortcloud Comfort Cloud') + help='Username for pcomfortcloud Comfort Cloud. If "#" is used then the password parameter is a YAML file containing both the username and password') parser.add_argument( 'password', - help='Password for pcomfortcloud Comfort Cloud') + help='Password for pcomfortcloud Comfort Cloud. If "#" used as the username, then this parameter is a YAML file containing both the username and password') parser.add_argument( '-t', '--token', @@ -68,6 +69,10 @@ def main(): 'list', help="Get a list of all devices") + get_parser = commandparser.add_parser( + 'getall', + help="Get status of all devices") + get_parser = commandparser.add_parser( 'get', help="Get status of a device") @@ -197,6 +202,27 @@ def main(): help='date of day like 20190807') args = parser.parse_args() + + # if username has been specified as the string "auth" then password is actually a filename containing both the username and password + if args.username == "#": + # args.password should actually be a filename, try and read it as as yaml file + with open(args.password, "r") as file: + try: + cfg = yaml.load(file, Loader=yaml.FullLoader) + # grab the user and pass out of the auth file + # replacing that which was specified on the command line + args.username=cfg["username"] + args.password=cfg["password"] + print ("Retrieved username and password from authfile") + except yaml.YAMLError as e: + print("YAML format error with authfile: " + args.authfile) + print(e) + quit() + except Exception as e: + print("YAML configuration error with authfile (missing fields?): " + args.authfile) + print(e) + quit() + session = pcomfortcloud.Session(args.username, args.password, args.token, args.raw, args.skipVerify == False) session.login() @@ -210,11 +236,23 @@ def main(): print("device #{}".format(idx + 1)) print_result(device, 4) + if args.command == 'getall': + print("status of all devices") + for idx, device in enumerate(session.get_devices()): + if(idx > 0): + print('') + + print("device #{}".format(idx + 1)) + device = session.get_devices()[idx - 1] + print("reading from device '{}' ({})".format(device['name'], device['id'])) + print_result( session.get_device(device['id']) ) + if args.command == 'get': if int(args.device) <= 0 or int(args.device) > len(session.get_devices()): raise Exception("device not found, acceptable device id is from {} to {}".format(1, len(session.get_devices()))) device = session.get_devices()[int(args.device) - 1] + print("reading from device '{}' ({})".format(device['name'], device['id'])) print_result( session.get_device(device['id']) )