Replace Float 'nan' with None for modbus floats (#93832)

Co-authored-by: jan iversen <jancasacondor@gmail.com>
This commit is contained in:
String-656 2023-08-06 20:26:56 +08:00 committed by GitHub
parent c4a5373976
commit 56dd88db17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -218,6 +218,9 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
# the conversion only when it's absolutely necessary.
if isinstance(v_temp, int) and self._precision == 0:
v_result.append(str(v_temp))
elif v_temp != v_temp: # noqa: PLR0124
# NaN float detection replace with None
v_result.append("nan") # pragma: no cover
else:
v_result.append(f"{float(v_temp):.{self._precision}f}")
return ",".join(map(str, v_result))
@ -228,6 +231,10 @@ class BaseStructPlatform(BasePlatform, RestoreEntity):
# We could convert int to float, and the code would still work; however
# we lose some precision, and unit tests will fail. Therefore, we do
# the conversion only when it's absolutely necessary.
# NaN float detection replace with None
if val_result != val_result: # noqa: PLR0124
return None # pragma: no cover
if isinstance(val_result, int) and self._precision == 0:
return str(val_result)
if isinstance(val_result, str):