Skip to content

Commit

Permalink
Replace Float 'nan' with None for modbus floats (#93832)
Browse files Browse the repository at this point in the history
Co-authored-by: jan iversen <jancasacondor@gmail.com>
  • Loading branch information
String-656 and janiversen committed Aug 6, 2023
1 parent c4a5373 commit 56dd88d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions homeassistant/components/modbus/base_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def unpack_structure_result(self, registers: list[int]) -> str | None:
# 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))
Expand All @@ -228,6 +231,10 @@ def unpack_structure_result(self, registers: list[int]) -> str | None:
# 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):
Expand Down

0 comments on commit 56dd88d

Please sign in to comment.