From c79b3df73f7dc4ed28dfb2d1dda90d554350ea2a Mon Sep 17 00:00:00 2001
From: Austin Mroczek <austin@mroczek.org>
Date: Sun, 22 Mar 2020 07:35:58 -0700
Subject: [PATCH] Handle generic_thermostat state unavailable (#32852)

* fix sensor unavailable error

* fix climate.py
---
 .../components/generic_thermostat/climate.py  |  8 +++-
 .../generic_thermostat/test_climate.py        | 40 +++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py
index 8714ddcfbe6..a7ddcc08314 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 776d8f39f69..264146a6fda 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)
-- 
GitLab