mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Ensure rest sensors are marked unavailable when http requests… (#32309)
This commit is contained in:
parent
601f2c693d
commit
c7f128f286
@ -202,6 +202,8 @@ class RestSensor(Entity):
|
|||||||
self.rest.update()
|
self.rest.update()
|
||||||
value = self.rest.data
|
value = self.rest.data
|
||||||
_LOGGER.debug("Data fetched from resource: %s", value)
|
_LOGGER.debug("Data fetched from resource: %s", value)
|
||||||
|
if self.rest.headers is not None:
|
||||||
|
# If the http request failed, headers will be None
|
||||||
content_type = self.rest.headers.get("content-type")
|
content_type = self.rest.headers.get("content-type")
|
||||||
|
|
||||||
if content_type and content_type.startswith("text/xml"):
|
if content_type and content_type.startswith("text/xml"):
|
||||||
|
@ -589,6 +589,35 @@ class TestRestSensor(unittest.TestCase):
|
|||||||
assert mock_logger.warning.called
|
assert mock_logger.warning.called
|
||||||
assert mock_logger.debug.called
|
assert mock_logger.debug.called
|
||||||
|
|
||||||
|
@patch("homeassistant.components.rest.sensor._LOGGER")
|
||||||
|
def test_update_with_failed_get(self, mock_logger):
|
||||||
|
"""Test attributes get extracted from a XML result with bad xml."""
|
||||||
|
value_template = template("{{ value_json.toplevel.master_value }}")
|
||||||
|
value_template.hass = self.hass
|
||||||
|
|
||||||
|
self.rest.update = Mock(
|
||||||
|
"rest.RestData.update", side_effect=self.update_side_effect(None, None),
|
||||||
|
)
|
||||||
|
self.sensor = rest.RestSensor(
|
||||||
|
self.hass,
|
||||||
|
self.rest,
|
||||||
|
self.name,
|
||||||
|
self.unit_of_measurement,
|
||||||
|
self.device_class,
|
||||||
|
value_template,
|
||||||
|
["key"],
|
||||||
|
self.force_update,
|
||||||
|
self.resource_template,
|
||||||
|
self.json_attrs_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.sensor.update()
|
||||||
|
assert {} == self.sensor.device_state_attributes
|
||||||
|
assert mock_logger.warning.called
|
||||||
|
assert mock_logger.debug.called
|
||||||
|
assert self.sensor.state is None
|
||||||
|
assert self.sensor.available is False
|
||||||
|
|
||||||
|
|
||||||
class TestRestData(unittest.TestCase):
|
class TestRestData(unittest.TestCase):
|
||||||
"""Tests for RestData."""
|
"""Tests for RestData."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user