Add XML support to RESTful binary sensor (#110062)

* Add XML support to RESTful binary sensor

* Add test for binary sensor with XML input data

* Address mypy validation results by handling None returns

* Use proper incorrect XML instead of blank

* Change failure condition to match the behavior of the library method

* Change error handling for bad XML to expect ExpatError

* Parametrize bad XML test to catch both empty and invalid XML

* Move exception handling out of the shared method

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Oleg Kurapov 2024-05-30 16:29:50 +02:00 committed by GitHub
parent 4b95ea864f
commit 2cc38b426a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 107 additions and 18 deletions

View file

@ -4,7 +4,6 @@ from __future__ import annotations
import logging
import ssl
from xml.parsers.expat import ExpatError
import httpx
import xmltodict
@ -79,14 +78,8 @@ class RestData:
and (content_type := headers.get("content-type"))
and content_type.startswith(XML_MIME_TYPES)
):
try:
value = json_dumps(xmltodict.parse(value))
except ExpatError:
_LOGGER.warning(
"REST xml result could not be parsed and converted to JSON"
)
else:
_LOGGER.debug("JSON converted from XML: %s", value)
value = json_dumps(xmltodict.parse(value))
_LOGGER.debug("JSON converted from XML: %s", value)
return value
async def async_update(self, log_errors: bool = True) -> None: