Report modbus buffer too small or too big to unpack (#57838)

This commit is contained in:
jan iversen 2021-10-21 00:22:01 +02:00 committed by GitHub
parent 38586d2cf1
commit cca7da77ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 4 deletions

View file

@ -160,7 +160,7 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
registers.reverse()
return registers
def unpack_structure_result(self, registers: list[int]) -> str:
def unpack_structure_result(self, registers: list[int]) -> str | None:
"""Convert registers to proper result."""
registers = self._swap_registers(registers)
@ -168,7 +168,13 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
if self._data_type == DataType.STRING:
return byte_string.decode()
val = struct.unpack(self._structure, byte_string)
try:
val = struct.unpack(self._structure, byte_string)
except struct.error as err:
recv_size = len(registers) * 2
msg = f"Received {recv_size} bytes, unpack error {err}"
_LOGGER.error(msg)
return None
# Issue: https://github.com/home-assistant/core/issues/41944
# If unpack() returns a tuple greater than 1, don't try to process the value.
# Instead, return the values of unpack(...) separated by commas.