From c7f128f2864197cb0e576754e7fccd9eac6923c3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 28 Feb 2020 09:41:41 -1000 Subject: [PATCH] =?UTF-8?q?Ensure=20rest=20sensors=20are=20marked=20unavai?= =?UTF-8?q?lable=20when=20http=20requests=E2=80=A6=20(#32309)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homeassistant/components/rest/sensor.py | 22 ++++++++++--------- tests/components/rest/test_sensor.py | 29 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/rest/sensor.py b/homeassistant/components/rest/sensor.py index 70424325241..7c8cfb9d3d0 100644 --- a/homeassistant/components/rest/sensor.py +++ b/homeassistant/components/rest/sensor.py @@ -202,17 +202,19 @@ class RestSensor(Entity): self.rest.update() value = self.rest.data _LOGGER.debug("Data fetched from resource: %s", value) - content_type = self.rest.headers.get("content-type") + if self.rest.headers is not None: + # If the http request failed, headers will be None + content_type = self.rest.headers.get("content-type") - if content_type and content_type.startswith("text/xml"): - try: - value = json.dumps(xmltodict.parse(value)) - _LOGGER.debug("JSON converted from XML: %s", value) - except ExpatError: - _LOGGER.warning( - "REST xml result could not be parsed and converted to JSON." - ) - _LOGGER.debug("Erroneous XML: %s", value) + if content_type and content_type.startswith("text/xml"): + try: + value = json.dumps(xmltodict.parse(value)) + _LOGGER.debug("JSON converted from XML: %s", value) + except ExpatError: + _LOGGER.warning( + "REST xml result could not be parsed and converted to JSON." + ) + _LOGGER.debug("Erroneous XML: %s", value) if self._json_attrs: self._attributes = {} diff --git a/tests/components/rest/test_sensor.py b/tests/components/rest/test_sensor.py index 30eeae9a8e3..5018418f493 100644 --- a/tests/components/rest/test_sensor.py +++ b/tests/components/rest/test_sensor.py @@ -589,6 +589,35 @@ class TestRestSensor(unittest.TestCase): assert mock_logger.warning.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): """Tests for RestData."""