Correct checks for non-finite numbers in ESPHome (#98102)

This commit is contained in:
Erik Montnemery 2023-08-12 09:10:25 +02:00 committed by GitHub
parent 8912b19cf4
commit 8b99d4678f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 4 deletions

View File

@ -105,8 +105,8 @@ def esphome_state_property(
if not self._has_state: if not self._has_state:
return None return None
val = func(self) val = func(self)
if isinstance(val, float) and math.isnan(val): if isinstance(val, float) and not math.isfinite(val):
# Home Assistant doesn't use NAN values in state machine # Home Assistant doesn't use NaN or inf values in state machine
# (not JSON serializable) # (not JSON serializable)
return None return None
return val return val

View File

@ -73,7 +73,7 @@ class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
def native_value(self) -> float | None: def native_value(self) -> float | None:
"""Return the state of the entity.""" """Return the state of the entity."""
state = self._state state = self._state
if state.missing_state or math.isnan(state.state): if state.missing_state or not math.isfinite(state.state):
return None return None
return state.state return state.state

View File

@ -96,7 +96,7 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
def native_value(self) -> datetime | str | None: def native_value(self) -> datetime | str | None:
"""Return the state of the entity.""" """Return the state of the entity."""
state = self._state state = self._state
if math.isnan(state.state) or state.missing_state: if state.missing_state or not math.isfinite(state.state):
return None return None
if self._attr_device_class == SensorDeviceClass.TIMESTAMP: if self._attr_device_class == SensorDeviceClass.TIMESTAMP:
return dt_util.utc_from_timestamp(state.state) return dt_util.utc_from_timestamp(state.state)