Skip to content

Commit

Permalink
add better hints of possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbach committed Jun 4, 2021
1 parent 73a123a commit abefb7c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions weconnect_cli/weconnect_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit abefb7c

Please sign in to comment.