From d1424714c7a7e34584a4f1293a67edac3abb9e13 Mon Sep 17 00:00:00 2001
From: pezinek <pezinek@gmail.com>
Date: Mon, 23 Oct 2017 23:29:41 +0200
Subject: [PATCH] Support for Entity.available in sensor/rest (#10073)

---
 homeassistant/components/sensor/rest.py |  9 +++++----
 tests/components/sensor/test_rest.py    | 11 +++++++----
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py
index c8d5591f2fa..2ae1c3674ea 100644
--- a/homeassistant/components/sensor/rest.py
+++ b/homeassistant/components/sensor/rest.py
@@ -68,10 +68,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
     rest = RestData(method, resource, auth, headers, payload, verify_ssl)
     rest.update()
 
-    if rest.data is None:
-        _LOGGER.error("Unable to fetch REST data")
-        return False
-
     add_devices([RestSensor(hass, rest, name, unit, value_template)], True)
 
 
@@ -97,6 +93,11 @@ class RestSensor(Entity):
         """Return the unit the value is expressed in."""
         return self._unit_of_measurement
 
+    @property
+    def available(self):
+        """Return if the sensor data are available."""
+        return self.rest.data is not None
+
     @property
     def state(self):
         """Return the state of the device."""
diff --git a/tests/components/sensor/test_rest.py b/tests/components/sensor/test_rest.py
index a80477d4bb7..a083dbfb1a2 100644
--- a/tests/components/sensor/test_rest.py
+++ b/tests/components/sensor/test_rest.py
@@ -45,18 +45,18 @@ class TestRestSensorSetup(unittest.TestCase):
            side_effect=requests.exceptions.ConnectionError())
     def test_setup_failed_connect(self, mock_req):
         """Test setup when connection error occurs."""
-        self.assertFalse(rest.setup_platform(self.hass, {
+        self.assertTrue(rest.setup_platform(self.hass, {
             'platform': 'rest',
             'resource': 'http://localhost',
-        }, None))
+        }, lambda devices, update=True: None) is None)
 
     @patch('requests.Session.send', side_effect=Timeout())
     def test_setup_timeout(self, mock_req):
         """Test setup when connection timeout occurs."""
-        self.assertFalse(rest.setup_platform(self.hass, {
+        self.assertTrue(rest.setup_platform(self.hass, {
             'platform': 'rest',
             'resource': 'http://localhost',
-        }, None))
+        }, lambda devices, update=True: None) is None)
 
     @requests_mock.Mocker()
     def test_setup_minimum(self, mock_req):
@@ -165,6 +165,7 @@ class TestRestSensor(unittest.TestCase):
             'rest.RestData.update', side_effect=self.update_side_effect(None))
         self.sensor.update()
         self.assertEqual(STATE_UNKNOWN, self.sensor.state)
+        self.assertFalse(self.sensor.available)
 
     def test_update_when_value_changed(self):
         """Test state gets updated when sensor returns a new status."""
@@ -173,6 +174,7 @@ class TestRestSensor(unittest.TestCase):
                                     '{ "key": "updated_state" }'))
         self.sensor.update()
         self.assertEqual('updated_state', self.sensor.state)
+        self.assertTrue(self.sensor.available)
 
     def test_update_with_no_template(self):
         """Test update when there is no value template."""
@@ -183,6 +185,7 @@ class TestRestSensor(unittest.TestCase):
             self.hass, self.rest, self.name, self.unit_of_measurement, None)
         self.sensor.update()
         self.assertEqual('plain_state', self.sensor.state)
+        self.assertTrue(self.sensor.available)
 
 
 class TestRestData(unittest.TestCase):