Skip to content

Commit

Permalink
Attribute change from string added
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbach committed Jun 9, 2021
1 parent e393618 commit 138ffbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 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
- Attribute Change from String

### Fixed
- Fix ChargingSettings to String method to use Enum value instead of raw enum
format"
Expand Down
42 changes: 35 additions & 7 deletions weconnect/addressable.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def addObserver(self, observer, flag, priority=None):
self.__observers.add((observer, flag, priority))
LOG.debug('%s: Observer added with flags: %s', self.getGlobalAddress(), flag)

def getObservers(self, flags, asTuple=False):
def getObservers(self, flags):
return [observerEntry[0] for observerEntry in self.getObserverEntries(flags)]

def getObserverEntries(self, flags):
observers = set()
for observerEntry in self.__observers:
observer, observerflags, priority = observerEntry
Expand All @@ -39,10 +42,8 @@ def getObservers(self, flags, asTuple=False):
if flags & observerflags:
observers.add(observerEntry)
if self.__parent is not None:
observers.update(self.__parent.getObservers(flags, asTuple=True))
if asTuple:
return sorted(observers, key=lambda entry: entry[2])
return [observerEntry[0] for observerEntry in sorted(observers, key=lambda entry: entry[2])]
observers.update(self.__parent.getObserverEntries(flags))
return sorted(observers, key=lambda entry: entry[2])

def notify(self, flags):
observers = self.getObservers(flags)
Expand Down Expand Up @@ -198,8 +199,35 @@ def __init__(
super().__init__(localAddress=localAddress, parent=parent, value=value, valueType=valueType,
lastUpdateFromCar=lastUpdateFromCar)

@AddressableAttribute.value.setter
def value(self, newValue):
@AddressableAttribute.value.setter # noqa: C901
def value(self, newValue): # noqa: C901
if isinstance(newValue, str) and self.valueType != str:
try:
if self.valueType in [int, float]:
newValue = self.valueType(newValue)
elif issubclass(self.valueType, Enum):
newValue = self.valueType(newValue)
try:
allowedValues = self.valueType.allowedValues()
if newValue not in allowedValues:
raise ValueError('Value is not in allowed values')
except AttributeError:
pass
newValue = self.valueType(newValue)
except ValueError as vErr:
valueFormat = ''
if self.valueType == int:
valueFormat = 'N (Decimal number)'
elif self.valueType == float:
valueFormat = 'F.F (Floating Point Number)'
elif self.valueType == bool:
valueFormat = 'True/False (Boolean)'
elif issubclass(self.valueType, Enum):
try:
valueFormat = 'select one of [' + ', '.join([enum.value for enum in self.valueType.allowedValues()]) + ']'
except AttributeError:
valueFormat = 'select one of [' + ', '.join([enum.value for enum in self.valueType])
raise ValueError(f'id {self.getGlobalAddress()} cannot be set. You need to provide it in the correct format {valueFormat}') from vErr
self.setValueWithCarTime(newValue=newValue, lastUpdateFromCar=None)


Expand Down

0 comments on commit 138ffbb

Please sign in to comment.