Skip to content

Commit

Permalink
Update ulid-transform to 0.10.1 (home-assistant#121321)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 5, 2024
1 parent 94db251 commit 89ffee9
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 54 deletions.
29 changes: 6 additions & 23 deletions homeassistant/components/recorder/models/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,16 @@
import logging
from uuid import UUID

from homeassistant.util.ulid import bytes_to_ulid, ulid_to_bytes
from homeassistant.util.ulid import ( # noqa: F401
bytes_to_ulid,
bytes_to_ulid_or_none,
ulid_to_bytes,
ulid_to_bytes_or_none,
)

_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
try:
return ulid_to_bytes(ulid)
except ValueError:
_LOGGER.exception("Error converting ulid %s to bytes", ulid)
return None


def bytes_to_ulid_or_none(_bytes: bytes | None) -> str | None:
"""Convert bytes to a ulid."""
if _bytes is None:
return None
try:
return bytes_to_ulid(_bytes)
except ValueError:
_LOGGER.exception("Error converting bytes %s to ulid", _bytes)
return None


@lru_cache(maxsize=16)
def uuid_hex_to_bytes_or_none(uuid_hex: str | None) -> bytes | None:
"""Convert a uuid hex to bytes."""
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PyYAML==6.0.1
requests==2.32.3
SQLAlchemy==2.0.31
typing-extensions>=4.12.2,<5.0
ulid-transform==0.9.0
ulid-transform==0.10.1
urllib3>=1.26.5,<2
voluptuous-openapi==0.0.4
voluptuous-serialize==2.6.0
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/util/ulid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from ulid_transform import (
bytes_to_ulid,
bytes_to_ulid_or_none,
ulid_at_time,
ulid_hex,
ulid_now,
ulid_to_bytes,
ulid_to_bytes_or_none,
)

__all__ = [
Expand All @@ -17,6 +19,8 @@
"ulid_to_bytes",
"bytes_to_ulid",
"ulid_now",
"ulid_to_bytes_or_none",
"bytes_to_ulid_or_none",
]


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies = [
"requests==2.32.3",
"SQLAlchemy==2.0.31",
"typing-extensions>=4.12.2,<5.0",
"ulid-transform==0.9.0",
"ulid-transform==0.10.1",
# Constrain urllib3 to ensure we deal with CVE-2020-26137 and CVE-2021-33503
# Temporary setting an upper bound, to prevent compat issues with urllib3>=2
# https://github.com/home-assistant/core/issues/97248
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PyYAML==6.0.1
requests==2.32.3
SQLAlchemy==2.0.31
typing-extensions>=4.12.2,<5.0
ulid-transform==0.9.0
ulid-transform==0.10.1
urllib3>=1.26.5,<2
voluptuous==0.15.2
voluptuous-serialize==2.6.0
Expand Down
2 changes: 0 additions & 2 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,6 @@ async def test_saving_event_invalid_context_ulid(
)
}

assert "invalid" in caplog.text

assert len(events) == 1
assert json_loads(events["test_event"]) == event_data

Expand Down
26 changes: 0 additions & 26 deletions tests/components/recorder/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
)
from homeassistant.components.recorder.models import (
LazyState,
bytes_to_ulid_or_none,
process_datetime_to_timestamp,
process_timestamp,
process_timestamp_to_utc_isoformat,
ulid_to_bytes_or_none,
)
from homeassistant.const import EVENT_STATE_CHANGED
import homeassistant.core as ha
Expand Down Expand Up @@ -428,27 +426,3 @@ async def test_process_datetime_to_timestamp_mirrors_utc_isoformat_behavior(
process_datetime_to_timestamp(datetime_hst_timezone)
== dt_util.parse_datetime("2016-07-09T21:00:00+00:00").timestamp()
)


def test_ulid_to_bytes_or_none(caplog: pytest.LogCaptureFixture) -> None:
"""Test ulid_to_bytes_or_none."""

assert (
ulid_to_bytes_or_none("01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1")
== b"\x01w\xaf\xf9w\xe5\xf8~\x1f\x87\xe1\xf8~\x1f\x87\xe1"
)
assert ulid_to_bytes_or_none("invalid") is None
assert "invalid" in caplog.text
assert ulid_to_bytes_or_none(None) is None


def test_bytes_to_ulid_or_none(caplog: pytest.LogCaptureFixture) -> None:
"""Test bytes_to_ulid_or_none."""

assert (
bytes_to_ulid_or_none(b"\x01w\xaf\xf9w\xe5\xf8~\x1f\x87\xe1\xf8~\x1f\x87\xe1")
== "01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1"
)
assert bytes_to_ulid_or_none(b"invalid") is None
assert "invalid" in caplog.text
assert bytes_to_ulid_or_none(None) is None

0 comments on commit 89ffee9

Please sign in to comment.