mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Improve performance of generating non-cryptographically secure uuids (#41314)
This commit is contained in:
parent
451f27bba1
commit
8d5c124deb
@ -139,7 +139,7 @@ class ConfigEntry:
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a config entry."""
|
"""Initialize a config entry."""
|
||||||
# Unique id of the config entry
|
# Unique id of the config entry
|
||||||
self.entry_id = entry_id or uuid_util.uuid_v1mc_hex()
|
self.entry_id = entry_id or uuid_util.random_uuid_hex()
|
||||||
|
|
||||||
# Version of the configuration.
|
# Version of the configuration.
|
||||||
self.version = version
|
self.version = version
|
||||||
|
@ -511,7 +511,7 @@ class Context:
|
|||||||
|
|
||||||
user_id: str = attr.ib(default=None)
|
user_id: str = attr.ib(default=None)
|
||||||
parent_id: Optional[str] = attr.ib(default=None)
|
parent_id: Optional[str] = attr.ib(default=None)
|
||||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||||
|
|
||||||
def as_dict(self) -> dict:
|
def as_dict(self) -> dict:
|
||||||
"""Return a dictionary representation of the context."""
|
"""Return a dictionary representation of the context."""
|
||||||
|
@ -26,7 +26,7 @@ class AreaEntry:
|
|||||||
"""Area Registry Entry."""
|
"""Area Registry Entry."""
|
||||||
|
|
||||||
name: Optional[str] = attr.ib(default=None)
|
name: Optional[str] = attr.ib(default=None)
|
||||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||||
|
|
||||||
|
|
||||||
class AreaRegistry:
|
class AreaRegistry:
|
||||||
|
@ -73,7 +73,7 @@ class DeviceEntry:
|
|||||||
area_id: str = attr.ib(default=None)
|
area_id: str = attr.ib(default=None)
|
||||||
name_by_user: str = attr.ib(default=None)
|
name_by_user: str = attr.ib(default=None)
|
||||||
entry_type: str = attr.ib(default=None)
|
entry_type: str = attr.ib(default=None)
|
||||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||||
# This value is not stored, just used to keep track of events to fire.
|
# This value is not stored, just used to keep track of events to fire.
|
||||||
is_new: bool = attr.ib(default=False)
|
is_new: bool = attr.ib(default=False)
|
||||||
|
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
"""Helpers to generate uuids."""
|
"""Helpers to generate uuids."""
|
||||||
|
|
||||||
import random
|
from random import getrandbits
|
||||||
import uuid
|
|
||||||
|
|
||||||
|
|
||||||
def uuid_v1mc_hex() -> str:
|
def random_uuid_hex() -> str:
|
||||||
"""Generate a uuid1 with a random multicast MAC address.
|
"""Generate a random UUID hex.
|
||||||
|
|
||||||
The uuid1 uses a random multicast MAC address instead of the real MAC address
|
This uuid should not be used for cryptographically secure
|
||||||
of the machine without the overhead of calling the getrandom() system call.
|
operations.
|
||||||
|
|
||||||
This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
|
|
||||||
"""
|
"""
|
||||||
return uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex
|
return "%032x" % getrandbits(32 * 4)
|
||||||
|
@ -5,7 +5,7 @@ import uuid
|
|||||||
import homeassistant.util.uuid as uuid_util
|
import homeassistant.util.uuid as uuid_util
|
||||||
|
|
||||||
|
|
||||||
async def test_uuid_v1mc_hex():
|
async def test_uuid_util_random_uuid_hex():
|
||||||
"""Verify we can generate a uuid_v1mc and return hex."""
|
"""Verify we can generate a random uuid."""
|
||||||
assert len(uuid_util.uuid_v1mc_hex()) == 32
|
assert len(uuid_util.random_uuid_hex()) == 32
|
||||||
assert uuid.UUID(uuid_util.uuid_v1mc_hex())
|
assert uuid.UUID(uuid_util.random_uuid_hex())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user