Home Assistant Unofficial Reference 2024.12.1
context.py
Go to the documentation of this file.
1 """Models for Recorder."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 from functools import lru_cache
7 import logging
8 from uuid import UUID
9 
10 from homeassistant.util.ulid import ( # noqa: F401
11  bytes_to_ulid,
12  bytes_to_ulid_or_none,
13  ulid_to_bytes,
14  ulid_to_bytes_or_none,
15 )
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
20 @lru_cache(maxsize=16)
21 def uuid_hex_to_bytes_or_none(uuid_hex: str | None) -> bytes | None:
22  """Convert a uuid hex to bytes."""
23  if uuid_hex is None:
24  return None
25  with suppress(ValueError):
26  return UUID(hex=uuid_hex).bytes
27  return None
28 
29 
30 @lru_cache(maxsize=16)
31 def bytes_to_uuid_hex_or_none(_bytes: bytes | None) -> str | None:
32  """Convert bytes to a uuid hex."""
33  if _bytes is None:
34  return None
35  with suppress(ValueError):
36  return UUID(bytes=_bytes).hex
37  return None
str|None bytes_to_uuid_hex_or_none(bytes|None _bytes)
Definition: context.py:31
bytes|None uuid_hex_to_bytes_or_none(str|None uuid_hex)
Definition: context.py:21