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 GitHub
parent 56ddfa9ff8
commit 89b655c192
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations
from functools import partial
from math import isfinite
from typing import Any, cast
from aioesphomeapi import (
@ -238,9 +239,13 @@ class EsphomeClimateEntity(EsphomeEntity[ClimateInfo, ClimateState], ClimateEnti
@esphome_state_property
def current_humidity(self) -> int | None:
"""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 round(self._state.current_humidity)
return round(val)
@property
@esphome_float_state_property

View File

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