Standardize uuid generation for events/storage/registry (#39184)
parent
28332f23b3
commit
bee6d87e7a
|
@ -4,7 +4,6 @@ import functools
|
|||
import logging
|
||||
from types import MappingProxyType
|
||||
from typing import Any, Callable, Dict, List, Optional, Set, Union, cast
|
||||
import uuid
|
||||
import weakref
|
||||
|
||||
import attr
|
||||
|
@ -16,6 +15,7 @@ from homeassistant.helpers import entity_registry
|
|||
from homeassistant.helpers.event import Event
|
||||
from homeassistant.setup import async_process_deps_reqs, async_setup_component
|
||||
from homeassistant.util.decorator import Registry
|
||||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_UNDEF: dict = {}
|
||||
|
@ -135,7 +135,7 @@ class ConfigEntry:
|
|||
) -> None:
|
||||
"""Initialize a config entry."""
|
||||
# Unique id of the config entry
|
||||
self.entry_id = entry_id or uuid.uuid4().hex
|
||||
self.entry_id = entry_id or uuid_util.uuid_v1mc_hex()
|
||||
|
||||
# Version of the configuration.
|
||||
self.version = version
|
||||
|
|
|
@ -12,7 +12,6 @@ from ipaddress import ip_address
|
|||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import random
|
||||
import re
|
||||
import threading
|
||||
from time import monotonic
|
||||
|
@ -33,7 +32,6 @@ from typing import (
|
|||
Union,
|
||||
cast,
|
||||
)
|
||||
import uuid
|
||||
|
||||
import attr
|
||||
import voluptuous as vol
|
||||
|
@ -77,6 +75,7 @@ import homeassistant.util.dt as dt_util
|
|||
from homeassistant.util.thread import fix_threading_exception_logging
|
||||
from homeassistant.util.timeout import TimeoutManager
|
||||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
|
||||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
# Typing imports that create a circular dependency
|
||||
if TYPE_CHECKING:
|
||||
|
@ -510,13 +509,7 @@ class Context:
|
|||
|
||||
user_id: str = attr.ib(default=None)
|
||||
parent_id: Optional[str] = attr.ib(default=None)
|
||||
# The uuid1 uses a random multicast MAC address instead of the real MAC address
|
||||
# of the machine without the overhead of calling the getrandom() system call.
|
||||
#
|
||||
# This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
|
||||
id: str = attr.ib(
|
||||
factory=lambda: uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex
|
||||
)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
|
||||
def as_dict(self) -> dict:
|
||||
"""Return a dictionary representation of the context."""
|
||||
|
|
|
@ -3,12 +3,12 @@ from asyncio import Event
|
|||
from collections import OrderedDict
|
||||
import logging
|
||||
from typing import Dict, Iterable, List, MutableMapping, Optional, cast
|
||||
import uuid
|
||||
|
||||
import attr
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.loader import bind_hass
|
||||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
from .typing import HomeAssistantType
|
||||
|
||||
|
@ -26,7 +26,7 @@ class AreaEntry:
|
|||
"""Area Registry Entry."""
|
||||
|
||||
name: Optional[str] = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
|
||||
|
||||
class AreaRegistry:
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
from collections import OrderedDict
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union
|
||||
import uuid
|
||||
|
||||
import attr
|
||||
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
|
||||
from homeassistant.core import Event, callback
|
||||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
from .debounce import Debouncer
|
||||
from .singleton import singleton
|
||||
|
@ -73,7 +73,7 @@ class DeviceEntry:
|
|||
area_id: str = attr.ib(default=None)
|
||||
name_by_user: str = attr.ib(default=None)
|
||||
entry_type: str = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=lambda: uuid.uuid4().hex)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
# This value is not stored, just used to keep track of events to fire.
|
||||
is_new: bool = attr.ib(default=False)
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
"""Helpers to generate uuids."""
|
||||
|
||||
import random
|
||||
import uuid
|
||||
|
||||
|
||||
def uuid_v1mc_hex() -> str:
|
||||
"""Generate a uuid1 with a random multicast MAC address.
|
||||
|
||||
The uuid1 uses a random multicast MAC address instead of the real MAC address
|
||||
of the machine without the overhead of calling the getrandom() system call.
|
||||
|
||||
This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
|
||||
"""
|
||||
return uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex
|
|
@ -0,0 +1,11 @@
|
|||
"""Test Home Assistant uuid util methods."""
|
||||
|
||||
import uuid
|
||||
|
||||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
|
||||
async def test_uuid_v1mc_hex():
|
||||
"""Verify we can generate a uuid_v1mc and return hex."""
|
||||
assert len(uuid_util.uuid_v1mc_hex()) == 32
|
||||
assert uuid.UUID(uuid_util.uuid_v1mc_hex())
|
Loading…
Reference in New Issue