diff --git a/pyproject.toml b/pyproject.toml index 8368141..7de2e62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ namespaces = true # PEP 621 https://peps.python.org/pep-0621/ [project] name = "onebase" -version = "0.1.13" +version = "0.1.14" # for automated versioning by pypa/setuptools_scm. see https://peps.python.org/pep-0621/#dynamic # dynamic = ["version"] description = "A python package for local communication with Viessmann OneBase devices over CAN, Ethernet or WiFi." diff --git a/src/onebase/core/codecs.py b/src/onebase/core/codecs.py index fe11c06..824bf9b 100644 --- a/src/onebase/core/codecs.py +++ b/src/onebase/core/codecs.py @@ -117,10 +117,22 @@ def __init__(self, paramNumBytes: int, paramDIDName: str, paramOffset: int = 0): def encode(self, string_ascii: Any, paramRaw:bool=False) -> bytes: if(paramRaw): _hexString = string_ascii - return CodecRaw.encode(self, _hexString) + _encodedBytes = CodecRaw.encode(self, _hexString) else: _byteArrayUTF8 = string_ascii.encode(encoding="utf-8") - return _byteArrayUTF8 + _encodedBytes = _byteArrayUTF8 + + + _paddingByte = b'\x00' + if len(_encodedBytes) < self._numBytes: + _resultPadddedCropped = _encodedBytes.ljust(self._numBytes, _paddingByte) + elif len(_encodedBytes) > self._numBytes: + _resultPadddedCropped = _encodedBytes[0:self._numBytes] + else: + _resultPadddedCropped = _encodedBytes + + + return _resultPadddedCropped def decode(self, paramEncodedBytes: bytes, paramRaw:bool=False) -> Any: if(paramRaw): @@ -466,7 +478,7 @@ def __init__(self, paramNumBytes:int, paramDIDName:str, subTypes : list): def encode(self, string_ascii:Any, paramRaw:bool=False) -> bytes: if(paramRaw): - _result = CodecRaw.encode(self, string_ascii) #just convert hex string to bytes + _encodedBytes = CodecRaw.encode(self, string_ascii) #just convert hex string to bytes else: try: _encodedBytes = bytes() @@ -474,17 +486,8 @@ def encode(self, string_ascii:Any, paramRaw:bool=False) -> bytes: _encodedBytes+=subType.encode(string_ascii[subType._DIDName]) except KeyError as e: raise ValueError(f"Cannot encode value due to missing key: {e}") - _result = _encodedBytes - - _paddingByte = b'\x00' - if len(_result) < self._numBytes: - _resultPadddedCropped = _result.ljust(self._numBytes, _paddingByte) - elif len(_result) > self._numBytes: - _resultPadddedCropped = _result[0:self._numBytes] - else: - _resultPadddedCropped = _result - return _resultPadddedCropped + return _encodedBytes def decode(self, paramEncodedBytes: bytes, paramRaw:bool=False) -> Any: if(paramRaw): #just convert hex string to bytes