Guard against invalid ULIDs in contexts while recording events (#90889)

This commit is contained in:
J. Nick Koston
2023-04-05 22:34:54 -10:00
committed by GitHub
parent 19f71b3bb9
commit 96c5e845e5
3 changed files with 67 additions and 3 deletions

View File

@@ -3,23 +3,36 @@ from __future__ import annotations
from contextlib import suppress
from functools import lru_cache
import logging
from uuid import UUID
from homeassistant.util.ulid import bytes_to_ulid, ulid_to_bytes
_LOGGER = logging.getLogger(__name__)
def ulid_to_bytes_or_none(ulid: str | None) -> bytes | None:
"""Convert an ulid to bytes."""
if ulid is None:
return None
return ulid_to_bytes(ulid)
try:
return ulid_to_bytes(ulid)
except ValueError as ex:
_LOGGER.error("Error converting ulid %s to bytes: %s", ulid, ex, exc_info=True)
return None
def bytes_to_ulid_or_none(_bytes: bytes | None) -> str | None:
"""Convert bytes to a ulid."""
if _bytes is None:
return None
return bytes_to_ulid(_bytes)
try:
return bytes_to_ulid(_bytes)
except ValueError as ex:
_LOGGER.error(
"Error converting bytes %s to ulid: %s", _bytes, ex, exc_info=True
)
return None
@lru_cache(maxsize=16)