core/homeassistant/helpers/instance_id.py

35 lines
745 B
Python
Raw Normal View History

"""Helper to create a unique instance ID."""
2021-03-17 17:34:19 +00:00
from __future__ import annotations
import uuid
from homeassistant.core import HomeAssistant
from . import singleton, storage
DATA_KEY = "core.uuid"
DATA_VERSION = 1
LEGACY_UUID_FILE = ".uuid"
@singleton.singleton(DATA_KEY)
async def async_get(hass: HomeAssistant) -> str:
"""Get unique ID for the hass instance."""
2022-07-09 20:32:57 +00:00
store = storage.Store[dict[str, str]](hass, DATA_VERSION, DATA_KEY, True)
data: dict[str, str] | None = await storage.async_migrator(
2020-08-27 11:56:20 +00:00
hass,
hass.config.path(LEGACY_UUID_FILE),
store,
)
if data is not None:
return data["uuid"]
data = {"uuid": uuid.uuid4().hex}
await store.async_save(data)
return data["uuid"]