Home Assistant Unofficial Reference 2024.12.1
state_attributes.py
Go to the documentation of this file.
1 """State attributes models."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.util.json import json_loads_object
9 
10 EMPTY_JSON_OBJECT = "{}"
11 _LOGGER = logging.getLogger(__name__)
12 
13 
15  source: Any, attr_cache: dict[str, dict[str, Any]]
16 ) -> dict[str, Any]:
17  """Decode attributes from a row source."""
18  if not source or source == EMPTY_JSON_OBJECT:
19  return {}
20  if (attributes := attr_cache.get(source)) is not None:
21  return attributes
22  try:
23  attr_cache[source] = attributes = json_loads_object(source)
24  except ValueError:
25  _LOGGER.exception("Error converting row to state attributes: %s", source)
26  attr_cache[source] = attributes = {}
27  return attributes
dict[str, Any] decode_attributes_from_source(Any source, dict[str, dict[str, Any]] attr_cache)
JsonObjectType json_loads_object(bytes|bytearray|memoryview|str obj)
Definition: json.py:54