diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index ef3cf11fa1c..e83852d122f 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -1,6 +1,7 @@ """Adds support for generic thermostat units.""" import asyncio import logging +import math import voluptuous as vol @@ -419,7 +420,10 @@ class GenericThermostat(ClimateEntity, RestoreEntity): def _async_update_temp(self, state): """Update thermostat with latest state from sensor.""" try: - self._cur_temp = float(state.state) + cur_temp = float(state.state) + if math.isnan(cur_temp) or math.isinf(cur_temp): + raise ValueError(f"Sensor has illegal state {state.state}") + self._cur_temp = cur_temp except ValueError as ex: _LOGGER.error("Unable to update from sensor: %s", ex) diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index c2c1435464e..f5a27ac8b97 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -331,9 +331,18 @@ async def test_sensor_bad_value(hass, setup_comp_2): _setup_sensor(hass, None) await hass.async_block_till_done() - state = hass.states.get(ENTITY) - assert temp == state.attributes.get("current_temperature") + assert state.attributes.get("current_temperature") == temp + + _setup_sensor(hass, "inf") + await hass.async_block_till_done() + state = hass.states.get(ENTITY) + assert state.attributes.get("current_temperature") == temp + + _setup_sensor(hass, "nan") + await hass.async_block_till_done() + state = hass.states.get(ENTITY) + assert state.attributes.get("current_temperature") == temp async def test_sensor_unknown(hass):