mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Add application/xml as an XML to JSON auto converted mime type… (#32289)
* Resolves issue #32280
This commit is contained in:
parent
da959c8f7b
commit
924c313c8a
@ -206,7 +206,10 @@ class RestSensor(Entity):
|
|||||||
# If the http request failed, headers will be 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")
|
||||||
|
or content_type.startswith("application/xml")
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
value = json.dumps(xmltodict.parse(value))
|
value = json.dumps(xmltodict.parse(value))
|
||||||
_LOGGER.debug("JSON converted from XML: %s", value)
|
_LOGGER.debug("JSON converted from XML: %s", value)
|
||||||
|
@ -559,6 +559,39 @@ class TestRestSensor(unittest.TestCase):
|
|||||||
assert "12556" == self.sensor.device_state_attributes["ver"]
|
assert "12556" == self.sensor.device_state_attributes["ver"]
|
||||||
assert "bogus" == self.sensor.state
|
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")
|
@patch("homeassistant.components.rest.sensor._LOGGER")
|
||||||
def test_update_with_xml_convert_bad_xml(self, mock_logger):
|
def test_update_with_xml_convert_bad_xml(self, mock_logger):
|
||||||
"""Test attributes get extracted from a XML result with bad xml."""
|
"""Test attributes get extracted from a XML result with bad xml."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user