diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a8914a..3c5bb4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added +- Better suggestions for possible Enum values on set command + ## [0.5.0] - 2021-06-04 ### Added - Possibility to set attributes and control services diff --git a/weconnect_cli/weconnect_cli.py b/weconnect_cli/weconnect_cli.py index 4dc4c23..74904fb 100644 --- a/weconnect_cli/weconnect_cli.py +++ b/weconnect_cli/weconnect_cli.py @@ -130,7 +130,7 @@ def main(): # noqa: C901 # pylint: disable=too-many-statements,too-many-branche if not args.noTokenStorage: tokenfile = args.tokenfile - try: + try: # pylint: disable=too-many-nested-blocks weConnect = weconnect.WeConnect(username=username, password=password, tokenfile=tokenfile, updateAfterLogin=False, loginOnInit=False) if args.noCache or not os.path.isfile(args.cachefile): @@ -173,7 +173,14 @@ def main(): # noqa: C901 # pylint: disable=too-many-statements,too-many-branche try: if element.valueType in [int, float]: newValue = element.valueType(args.value) - if issubclass(element.valueType, Enum): + elif issubclass(element.valueType, Enum): + newValue = element.valueType(args.value) + try: + allowedValues = element.valueType.allowedValues() + if newValue not in allowedValues: + raise ValueError('Value is not in allowed values') + except AttributeError: + pass newValue = element.valueType(args.value) else: newValue = args.value @@ -190,7 +197,10 @@ def main(): # noqa: C901 # pylint: disable=too-many-statements,too-many-branche elif element.valueType == bool: valueFormat = 'True/False (Boolean)' elif issubclass(element.valueType, Enum): - valueFormat = 'select one of ' + ', '.join([enum.value for enum in element.valueType]) + try: + valueFormat = 'select one of [' + ', '.join([enum.value for enum in element.valueType.allowedValues()]) + ']' + except AttributeError: + valueFormat = 'select one of [' + ', '.join([enum.value for enum in element.valueType]) print(f'id {args.id} cannot be set. You need to provide it in the correct format {valueFormat}', file=sys.stderr) sys.exit(-1)