Ensure rest sensors are marked unavailable when http requests… (#32309)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-02-28 11:41:41 -08:00
1 parent 601f2c693d
commit c7f128f286
2 files changed
+41 -10

No files matched your search

+12 -10
View File
@@ -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 = {}