From 56dd88db1742d7345e350bec608a7a1d33d98a5d Mon Sep 17 00:00:00 2001 From: String-656 Date: Sun, 6 Aug 2023 20:26:56 +0800 Subject: [PATCH] Replace Float 'nan' with None for modbus floats (#93832) Co-authored-by: jan iversen --- homeassistant/components/modbus/base_platform.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 343d5a36b26..e4c657a6c54 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -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):