mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Switch rest to use the json helper (#73867)
This commit is contained in:
parent
33c263d09b
commit
ab30d38469
@ -1,7 +1,6 @@
|
|||||||
"""Support for RESTful API sensors."""
|
"""Support for RESTful API sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
from xml.parsers.expat import ExpatError
|
from xml.parsers.expat import ExpatError
|
||||||
|
|
||||||
@ -26,6 +25,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.json import json_dumps, json_loads
|
||||||
from homeassistant.helpers.template_entity import TemplateSensor
|
from homeassistant.helpers.template_entity import TemplateSensor
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
@ -141,7 +141,7 @@ class RestSensor(BaseRestEntity, TemplateSensor):
|
|||||||
or content_type.startswith("application/rss+xml")
|
or content_type.startswith("application/rss+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)
|
||||||
except ExpatError:
|
except ExpatError:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
@ -153,7 +153,7 @@ class RestSensor(BaseRestEntity, TemplateSensor):
|
|||||||
self._attributes = {}
|
self._attributes = {}
|
||||||
if value:
|
if value:
|
||||||
try:
|
try:
|
||||||
json_dict = json.loads(value)
|
json_dict = json_loads(value)
|
||||||
if self._json_attrs_path is not None:
|
if self._json_attrs_path is not None:
|
||||||
json_dict = jsonpath(json_dict, self._json_attrs_path)
|
json_dict = jsonpath(json_dict, self._json_attrs_path)
|
||||||
# jsonpath will always store the result in json_dict[0]
|
# jsonpath will always store the result in json_dict[0]
|
||||||
|
@ -86,4 +86,7 @@ def json_dumps(data: Any) -> str:
|
|||||||
).decode("utf-8")
|
).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
json_loads = orjson.loads
|
||||||
|
|
||||||
|
|
||||||
JSON_DUMP: Final = json_dumps
|
JSON_DUMP: Final = json_dumps
|
||||||
|
Loading…
x
Reference in New Issue
Block a user