Guard against invalid ULIDs in contexts while recording events (#90889)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user