Fix handling of NaN float values for current humidity in ESPHome (#139600)

fixes #131837
This commit is contained in:
J. Nick Koston 2025-03-01 16:13:04 -06:00 committed by Bram Kragten
parent c257b228f1
commit 74e8ffa555
2 changed files with 9 additions and 4 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
from functools import partial from functools import partial
from math import isfinite
from typing import Any, cast from typing import Any, cast
from aioesphomeapi import ( from aioesphomeapi import (
@ -238,9 +239,13 @@ class EsphomeClimateEntity(EsphomeEntity[ClimateInfo, ClimateState], ClimateEnti
@esphome_state_property @esphome_state_property
def current_humidity(self) -> int | None: def current_humidity(self) -> int | None:
"""Return the current humidity.""" """Return the current humidity."""
if not self._static_info.supports_current_humidity: if (
not self._static_info.supports_current_humidity
or (val := self._state.current_humidity) is None
or not isfinite(val)
):
return None return None
return round(self._state.current_humidity) return round(val)
@property @property
@esphome_float_state_property @esphome_float_state_property

View File

@ -407,7 +407,7 @@ async def test_climate_entity_with_inf_value(
target_temperature=math.inf, target_temperature=math.inf,
fan_mode=ClimateFanMode.AUTO, fan_mode=ClimateFanMode.AUTO,
swing_mode=ClimateSwingMode.BOTH, swing_mode=ClimateSwingMode.BOTH,
current_humidity=20.1, current_humidity=math.inf,
target_humidity=25.7, target_humidity=25.7,
) )
] ]
@ -422,7 +422,7 @@ async def test_climate_entity_with_inf_value(
assert state is not None assert state is not None
assert state.state == HVACMode.AUTO assert state.state == HVACMode.AUTO
attributes = state.attributes attributes = state.attributes
assert attributes[ATTR_CURRENT_HUMIDITY] == 20 assert ATTR_CURRENT_HUMIDITY not in attributes
assert attributes[ATTR_HUMIDITY] == 26 assert attributes[ATTR_HUMIDITY] == 26
assert attributes[ATTR_MAX_HUMIDITY] == 30 assert attributes[ATTR_MAX_HUMIDITY] == 30
assert attributes[ATTR_MIN_HUMIDITY] == 10 assert attributes[ATTR_MIN_HUMIDITY] == 10