Add application/xml as an XML to JSON auto converted mime type… (#32289)

* Resolves issue #32280
This commit is contained in:
J. Nick Koston 2020-03-02 01:22:01 -06:00 committed by GitHub
parent da959c8f7b
commit 924c313c8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -206,7 +206,10 @@ class RestSensor(Entity):
# 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"):
if content_type and (
content_type.startswith("text/xml")
or content_type.startswith("application/xml")
):
try:
value = json.dumps(xmltodict.parse(value))
_LOGGER.debug("JSON converted from XML: %s", value)

View File

@ -559,6 +559,39 @@ class TestRestSensor(unittest.TestCase):
assert "12556" == self.sensor.device_state_attributes["ver"]
assert "bogus" == self.sensor.state
def test_update_with_application_xml_convert_json_attrs_with_jsonattr_template(
self,
):
"""Test attributes get extracted from a JSON result that was converted from XML with application/xml mime type."""
json_attrs_path = "$.main"
value_template = template("{{ value_json.main.dog }}")
value_template.hass = self.hass
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect(
"<main><dog>1</dog><cat>3</cat></main>",
CaseInsensitiveDict({"Content-Type": "application/xml"}),
),
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
value_template,
["dog", "cat"],
self.force_update,
self.resource_template,
json_attrs_path,
)
self.sensor.update()
assert "3" == self.sensor.device_state_attributes["cat"]
assert "1" == self.sensor.device_state_attributes["dog"]
assert "1" == self.sensor.state
@patch("homeassistant.components.rest.sensor._LOGGER")
def test_update_with_xml_convert_bad_xml(self, mock_logger):
"""Test attributes get extracted from a XML result with bad xml."""