Support for Entity.available in sensor/rest (#10073)
This commit is contained in:
parent
74e93e5853
commit
d1424714c7
2 changed files with 12 additions and 8 deletions
|
@ -68,10 +68,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
||||||
rest.update()
|
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)
|
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 the unit the value is expressed in."""
|
||||||
return self._unit_of_measurement
|
return self._unit_of_measurement
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return if the sensor data are available."""
|
||||||
|
return self.rest.data is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
|
|
|
@ -45,18 +45,18 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
side_effect=requests.exceptions.ConnectionError())
|
side_effect=requests.exceptions.ConnectionError())
|
||||||
def test_setup_failed_connect(self, mock_req):
|
def test_setup_failed_connect(self, mock_req):
|
||||||
"""Test setup when connection error occurs."""
|
"""Test setup when connection error occurs."""
|
||||||
self.assertFalse(rest.setup_platform(self.hass, {
|
self.assertTrue(rest.setup_platform(self.hass, {
|
||||||
'platform': 'rest',
|
'platform': 'rest',
|
||||||
'resource': 'http://localhost',
|
'resource': 'http://localhost',
|
||||||
}, None))
|
}, lambda devices, update=True: None) is None)
|
||||||
|
|
||||||
@patch('requests.Session.send', side_effect=Timeout())
|
@patch('requests.Session.send', side_effect=Timeout())
|
||||||
def test_setup_timeout(self, mock_req):
|
def test_setup_timeout(self, mock_req):
|
||||||
"""Test setup when connection timeout occurs."""
|
"""Test setup when connection timeout occurs."""
|
||||||
self.assertFalse(rest.setup_platform(self.hass, {
|
self.assertTrue(rest.setup_platform(self.hass, {
|
||||||
'platform': 'rest',
|
'platform': 'rest',
|
||||||
'resource': 'http://localhost',
|
'resource': 'http://localhost',
|
||||||
}, None))
|
}, lambda devices, update=True: None) is None)
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_setup_minimum(self, mock_req):
|
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))
|
'rest.RestData.update', side_effect=self.update_side_effect(None))
|
||||||
self.sensor.update()
|
self.sensor.update()
|
||||||
self.assertEqual(STATE_UNKNOWN, self.sensor.state)
|
self.assertEqual(STATE_UNKNOWN, self.sensor.state)
|
||||||
|
self.assertFalse(self.sensor.available)
|
||||||
|
|
||||||
def test_update_when_value_changed(self):
|
def test_update_when_value_changed(self):
|
||||||
"""Test state gets updated when sensor returns a new status."""
|
"""Test state gets updated when sensor returns a new status."""
|
||||||
|
@ -173,6 +174,7 @@ class TestRestSensor(unittest.TestCase):
|
||||||
'{ "key": "updated_state" }'))
|
'{ "key": "updated_state" }'))
|
||||||
self.sensor.update()
|
self.sensor.update()
|
||||||
self.assertEqual('updated_state', self.sensor.state)
|
self.assertEqual('updated_state', self.sensor.state)
|
||||||
|
self.assertTrue(self.sensor.available)
|
||||||
|
|
||||||
def test_update_with_no_template(self):
|
def test_update_with_no_template(self):
|
||||||
"""Test update when there is no value template."""
|
"""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.hass, self.rest, self.name, self.unit_of_measurement, None)
|
||||||
self.sensor.update()
|
self.sensor.update()
|
||||||
self.assertEqual('plain_state', self.sensor.state)
|
self.assertEqual('plain_state', self.sensor.state)
|
||||||
|
self.assertTrue(self.sensor.available)
|
||||||
|
|
||||||
|
|
||||||
class TestRestData(unittest.TestCase):
|
class TestRestData(unittest.TestCase):
|
||||||
|
|
Loading…
Add table
Reference in a new issue