From e1f0b44ba4a425db6a72419168a899b4d448e3c6 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 9 Aug 2023 14:13:57 +0200 Subject: [PATCH] Use math.isfinite instead of explicitly checking for both nan and inf (#98103) --- homeassistant/components/generic_thermostat/climate.py | 2 +- homeassistant/components/sensor/recorder.py | 2 +- homeassistant/helpers/template.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index d3d80747127..c9fcde87162 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -442,7 +442,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): """Update thermostat with latest state from sensor.""" try: cur_temp = float(state.state) - if math.isnan(cur_temp) or math.isinf(cur_temp): + if not math.isfinite(cur_temp): raise ValueError(f"Sensor has illegal state {state.state}") self._cur_temp = cur_temp except ValueError as ex: diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 2b75c1114ce..e5a35187c99 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -149,7 +149,7 @@ def _equivalent_units(units: set[str | None]) -> bool: def _parse_float(state: str) -> float: """Parse a float string, throw on inf or nan.""" fstate = float(state) - if math.isnan(fstate) or math.isinf(fstate): + if not math.isfinite(fstate): raise ValueError return fstate diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index d40a0289ab8..67c1a3ed52f 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -1934,7 +1934,7 @@ def is_number(value): fvalue = float(value) except (ValueError, TypeError): return False - if math.isnan(fvalue) or math.isinf(fvalue): + if not math.isfinite(fvalue): return False return True