diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 8714ddcfbe608c78ada04c3942eb73019f479108..a7ddcc083149147602f5a202243b95fac5c515f6 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -30,6 +30,7 @@ from homeassistant.const import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, + STATE_UNAVAILABLE, STATE_UNKNOWN, ) from homeassistant.core import DOMAIN as HA_DOMAIN, callback @@ -197,7 +198,10 @@ class GenericThermostat(ClimateDevice, RestoreEntity): def _async_startup(event): """Init on startup.""" sensor_state = self.hass.states.get(self.sensor_entity_id) - if sensor_state and sensor_state.state != STATE_UNKNOWN: + if sensor_state and sensor_state.state not in ( + STATE_UNAVAILABLE, + STATE_UNKNOWN, + ): self._async_update_temp(sensor_state) self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup) @@ -352,7 +356,7 @@ class GenericThermostat(ClimateDevice, RestoreEntity): async def _async_sensor_changed(self, entity_id, old_state, new_state): """Handle temperature changes.""" - if new_state is None: + if new_state is None or new_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN): return self._async_update_temp(new_state) diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index 776d8f39f69613889996ac99e0a6bf358c123f4b..264146a6fdaf9bf365b35241cde4df7df341e427 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -22,6 +22,8 @@ from homeassistant.const import ( SERVICE_TURN_ON, STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, + STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT, ) @@ -271,6 +273,44 @@ async def test_sensor_bad_value(hass, setup_comp_2): assert temp == state.attributes.get("current_temperature") +async def test_sensor_unknown(hass): + """Test when target sensor is Unknown.""" + hass.states.async_set("sensor.unknown", STATE_UNKNOWN) + assert await async_setup_component( + hass, + "climate", + { + "climate": { + "platform": "generic_thermostat", + "name": "unknown", + "heater": ENT_SWITCH, + "target_sensor": "sensor.unknown", + } + }, + ) + state = hass.states.get("climate.unknown") + assert state.attributes.get("current_temperature") is None + + +async def test_sensor_unavailable(hass): + """Test when target sensor is Unavailable.""" + hass.states.async_set("sensor.unavailable", STATE_UNAVAILABLE) + assert await async_setup_component( + hass, + "climate", + { + "climate": { + "platform": "generic_thermostat", + "name": "unavailable", + "heater": ENT_SWITCH, + "target_sensor": "sensor.unavailable", + } + }, + ) + state = hass.states.get("climate.unavailable") + assert state.attributes.get("current_temperature") is None + + async def test_set_target_temp_heater_on(hass, setup_comp_2): """Test if target temperature turn heater on.""" calls = _setup_switch(hass, False)