1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

Switch recorder and templates to use json helper (#73876)

- These were using orjson directly, its a bit cleaner
  to use the helper so everything is easier to adjust
  in the future if we need to change anything about
  the loading
This commit is contained in:
J. Nick Koston 2022-06-23 07:32:26 -05:00 committed by GitHub
parent 2742bf86e3
commit 8015bb98a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 20 deletions

View File

@ -8,7 +8,6 @@ from typing import Any, cast
import ciso8601 import ciso8601
from fnvhash import fnv1a_32 from fnvhash import fnv1a_32
import orjson
from sqlalchemy import ( from sqlalchemy import (
JSON, JSON,
BigInteger, BigInteger,
@ -39,7 +38,12 @@ from homeassistant.const import (
MAX_LENGTH_STATE_STATE, MAX_LENGTH_STATE_STATE,
) )
from homeassistant.core import Context, Event, EventOrigin, State, split_entity_id from homeassistant.core import Context, Event, EventOrigin, State, split_entity_id
from homeassistant.helpers.json import JSON_DUMP, json_bytes from homeassistant.helpers.json import (
JSON_DECODE_EXCEPTIONS,
JSON_DUMP,
json_bytes,
json_loads,
)
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import ALL_DOMAIN_EXCLUDE_ATTRS from .const import ALL_DOMAIN_EXCLUDE_ATTRS
@ -188,15 +192,15 @@ class Events(Base): # type: ignore[misc,valid-type]
try: try:
return Event( return Event(
self.event_type, self.event_type,
orjson.loads(self.event_data) if self.event_data else {}, json_loads(self.event_data) if self.event_data else {},
EventOrigin(self.origin) EventOrigin(self.origin)
if self.origin if self.origin
else EVENT_ORIGIN_ORDER[self.origin_idx], else EVENT_ORIGIN_ORDER[self.origin_idx],
process_timestamp(self.time_fired), process_timestamp(self.time_fired),
context=context, context=context,
) )
except ValueError: except JSON_DECODE_EXCEPTIONS:
# When orjson.loads fails # When json_loads fails
_LOGGER.exception("Error converting to event: %s", self) _LOGGER.exception("Error converting to event: %s", self)
return None return None
@ -243,8 +247,8 @@ class EventData(Base): # type: ignore[misc,valid-type]
def to_native(self) -> dict[str, Any]: def to_native(self) -> dict[str, Any]:
"""Convert to an HA state object.""" """Convert to an HA state object."""
try: try:
return cast(dict[str, Any], orjson.loads(self.shared_data)) return cast(dict[str, Any], json_loads(self.shared_data))
except ValueError: except JSON_DECODE_EXCEPTIONS:
_LOGGER.exception("Error converting row to event data: %s", self) _LOGGER.exception("Error converting row to event data: %s", self)
return {} return {}
@ -330,9 +334,9 @@ class States(Base): # type: ignore[misc,valid-type]
parent_id=self.context_parent_id, parent_id=self.context_parent_id,
) )
try: try:
attrs = orjson.loads(self.attributes) if self.attributes else {} attrs = json_loads(self.attributes) if self.attributes else {}
except ValueError: except JSON_DECODE_EXCEPTIONS:
# When orjson.loads fails # When json_loads fails
_LOGGER.exception("Error converting row to state: %s", self) _LOGGER.exception("Error converting row to state: %s", self)
return None return None
if self.last_changed is None or self.last_changed == self.last_updated: if self.last_changed is None or self.last_changed == self.last_updated:
@ -402,15 +406,15 @@ class StateAttributes(Base): # type: ignore[misc,valid-type]
@staticmethod @staticmethod
def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int: def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int:
"""Return the hash of orjson encoded shared attributes.""" """Return the hash of json encoded shared attributes."""
return cast(int, fnv1a_32(shared_attrs_bytes)) return cast(int, fnv1a_32(shared_attrs_bytes))
def to_native(self) -> dict[str, Any]: def to_native(self) -> dict[str, Any]:
"""Convert to an HA state object.""" """Convert to an HA state object."""
try: try:
return cast(dict[str, Any], orjson.loads(self.shared_attrs)) return cast(dict[str, Any], json_loads(self.shared_attrs))
except ValueError: except JSON_DECODE_EXCEPTIONS:
# When orjson.loads fails # When json_loads fails
_LOGGER.exception("Error converting row to state attributes: %s", self) _LOGGER.exception("Error converting row to state attributes: %s", self)
return {} return {}

View File

@ -5,7 +5,6 @@ from datetime import datetime
import logging import logging
from typing import Any, TypedDict, overload from typing import Any, TypedDict, overload
import orjson
from sqlalchemy.engine.row import Row from sqlalchemy.engine.row import Row
from homeassistant.components.websocket_api.const import ( from homeassistant.components.websocket_api.const import (
@ -15,6 +14,7 @@ from homeassistant.components.websocket_api.const import (
COMPRESSED_STATE_STATE, COMPRESSED_STATE_STATE,
) )
from homeassistant.core import Context, State from homeassistant.core import Context, State
from homeassistant.helpers.json import json_loads
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
# pylint: disable=invalid-name # pylint: disable=invalid-name
@ -253,7 +253,7 @@ def decode_attributes_from_row(
if not source or source == EMPTY_JSON_OBJECT: if not source or source == EMPTY_JSON_OBJECT:
return {} return {}
try: try:
attr_cache[source] = attributes = orjson.loads(source) attr_cache[source] = attributes = json_loads(source)
except ValueError: except ValueError:
_LOGGER.exception("Error converting row to state attributes: %s", source) _LOGGER.exception("Error converting row to state attributes: %s", source)
attr_cache[source] = attributes = {} attr_cache[source] = attributes = {}

View File

@ -27,7 +27,6 @@ import jinja2
from jinja2 import pass_context, pass_environment from jinja2 import pass_context, pass_environment
from jinja2.sandbox import ImmutableSandboxedEnvironment from jinja2.sandbox import ImmutableSandboxedEnvironment
from jinja2.utils import Namespace from jinja2.utils import Namespace
import orjson
import voluptuous as vol import voluptuous as vol
from homeassistant.const import ( from homeassistant.const import (
@ -58,6 +57,7 @@ from homeassistant.util.async_ import run_callback_threadsafe
from homeassistant.util.thread import ThreadWithException from homeassistant.util.thread import ThreadWithException
from . import area_registry, device_registry, entity_registry, location as loc_helper from . import area_registry, device_registry, entity_registry, location as loc_helper
from .json import JSON_DECODE_EXCEPTIONS, json_loads
from .typing import TemplateVarsType from .typing import TemplateVarsType
# mypy: allow-untyped-defs, no-check-untyped-defs # mypy: allow-untyped-defs, no-check-untyped-defs
@ -566,8 +566,8 @@ class Template:
variables = dict(variables or {}) variables = dict(variables or {})
variables["value"] = value variables["value"] = value
with suppress(ValueError, TypeError): with suppress(*JSON_DECODE_EXCEPTIONS):
variables["value_json"] = orjson.loads(value) variables["value_json"] = json_loads(value)
try: try:
return _render_with_context( return _render_with_context(
@ -1744,7 +1744,7 @@ def ordinal(value):
def from_json(value): def from_json(value):
"""Convert a JSON string to an object.""" """Convert a JSON string to an object."""
return orjson.loads(value) return json_loads(value)
def to_json(value, ensure_ascii=True): def to_json(value, ensure_ascii=True):