Handle generic_thermostat state unavailable (#32852)

* fix sensor unavailable error

* fix climate.py
This commit is contained in:
Austin Mroczek 2020-03-22 07:35:58 -07:00 committed by GitHub
parent 8423d18d8d
commit c79b3df73f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -30,6 +30,7 @@ from homeassistant.const import (
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import DOMAIN as HA_DOMAIN, callback from homeassistant.core import DOMAIN as HA_DOMAIN, callback
@ -197,7 +198,10 @@ class GenericThermostat(ClimateDevice, RestoreEntity):
def _async_startup(event): def _async_startup(event):
"""Init on startup.""" """Init on startup."""
sensor_state = self.hass.states.get(self.sensor_entity_id) 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._async_update_temp(sensor_state)
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup) 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): async def _async_sensor_changed(self, entity_id, old_state, new_state):
"""Handle temperature changes.""" """Handle temperature changes."""
if new_state is None: if new_state is None or new_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN):
return return
self._async_update_temp(new_state) self._async_update_temp(new_state)

View File

@ -22,6 +22,8 @@ from homeassistant.const import (
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
@ -271,6 +273,44 @@ async def test_sensor_bad_value(hass, setup_comp_2):
assert temp == state.attributes.get("current_temperature") 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): async def test_set_target_temp_heater_on(hass, setup_comp_2):
"""Test if target temperature turn heater on.""" """Test if target temperature turn heater on."""
calls = _setup_switch(hass, False) calls = _setup_switch(hass, False)