2020-04-30 23:47:14 +00:00
|
|
|
"""Helper to help coordinating calls."""
|
2024-03-08 15:36:11 +00:00
|
|
|
|
2021-03-17 17:34:19 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-30 23:47:14 +00:00
|
|
|
import asyncio
|
2022-01-12 06:56:35 +00:00
|
|
|
from collections.abc import Callable
|
2020-04-30 23:47:14 +00:00
|
|
|
import functools
|
2024-05-18 09:41:46 +00:00
|
|
|
from typing import Any, cast, overload
|
2020-04-30 23:47:14 +00:00
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
2020-05-04 18:23:12 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2024-05-07 08:53:13 +00:00
|
|
|
from homeassistant.util.hass_dict import HassKey
|
2020-04-30 23:47:14 +00:00
|
|
|
|
2024-05-18 09:41:46 +00:00
|
|
|
type _FuncType[_T] = Callable[[HomeAssistant], _T]
|
2020-04-30 23:47:14 +00:00
|
|
|
|
|
|
|
|
2024-05-07 08:53:13 +00:00
|
|
|
@overload
|
2024-05-18 09:41:46 +00:00
|
|
|
def singleton[_T](
|
|
|
|
data_key: HassKey[_T],
|
|
|
|
) -> Callable[[_FuncType[_T]], _FuncType[_T]]: ...
|
2024-05-07 08:53:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-05-18 09:41:46 +00:00
|
|
|
def singleton[_T](data_key: str) -> Callable[[_FuncType[_T]], _FuncType[_T]]: ...
|
2024-05-07 08:53:13 +00:00
|
|
|
|
|
|
|
|
2024-05-18 09:41:46 +00:00
|
|
|
def singleton[_T](data_key: Any) -> Callable[[_FuncType[_T]], _FuncType[_T]]:
|
2020-04-30 23:47:14 +00:00
|
|
|
"""Decorate a function that should be called once per instance.
|
|
|
|
|
|
|
|
Result will be cached and simultaneous calls will be handled.
|
|
|
|
"""
|
|
|
|
|
2022-07-20 22:19:02 +00:00
|
|
|
def wrapper(func: _FuncType[_T]) -> _FuncType[_T]:
|
2020-04-30 23:47:14 +00:00
|
|
|
"""Wrap a function with caching logic."""
|
2020-10-18 20:41:22 +00:00
|
|
|
if not asyncio.iscoroutinefunction(func):
|
|
|
|
|
2024-04-28 17:11:08 +00:00
|
|
|
@functools.lru_cache(maxsize=1)
|
2020-10-18 20:41:22 +00:00
|
|
|
@bind_hass
|
|
|
|
@functools.wraps(func)
|
2022-03-17 18:09:55 +00:00
|
|
|
def wrapped(hass: HomeAssistant) -> _T:
|
2021-09-11 19:02:01 +00:00
|
|
|
if data_key not in hass.data:
|
|
|
|
hass.data[data_key] = func(hass)
|
2022-03-17 18:09:55 +00:00
|
|
|
return cast(_T, hass.data[data_key])
|
2020-10-18 20:41:22 +00:00
|
|
|
|
|
|
|
return wrapped
|
2020-04-30 23:47:14 +00:00
|
|
|
|
2020-05-04 18:23:12 +00:00
|
|
|
@bind_hass
|
2020-04-30 23:47:14 +00:00
|
|
|
@functools.wraps(func)
|
2022-07-20 22:19:02 +00:00
|
|
|
async def async_wrapped(hass: HomeAssistant) -> Any:
|
2021-09-11 19:02:01 +00:00
|
|
|
if data_key not in hass.data:
|
2020-04-30 23:47:14 +00:00
|
|
|
evt = hass.data[data_key] = asyncio.Event()
|
2023-06-21 14:12:51 +00:00
|
|
|
result = await func(hass)
|
2020-04-30 23:47:14 +00:00
|
|
|
hass.data[data_key] = result
|
|
|
|
evt.set()
|
2022-03-17 18:09:55 +00:00
|
|
|
return cast(_T, result)
|
2020-04-30 23:47:14 +00:00
|
|
|
|
2021-09-11 19:02:01 +00:00
|
|
|
obj_or_evt = hass.data[data_key]
|
|
|
|
|
2020-04-30 23:47:14 +00:00
|
|
|
if isinstance(obj_or_evt, asyncio.Event):
|
2021-09-11 19:02:01 +00:00
|
|
|
await obj_or_evt.wait()
|
2022-03-17 18:09:55 +00:00
|
|
|
return cast(_T, hass.data[data_key])
|
2020-04-30 23:47:14 +00:00
|
|
|
|
2022-03-17 18:09:55 +00:00
|
|
|
return cast(_T, obj_or_evt)
|
2020-04-30 23:47:14 +00:00
|
|
|
|
2022-07-20 22:19:02 +00:00
|
|
|
return async_wrapped # type: ignore[return-value]
|
2020-04-30 23:47:14 +00:00
|
|
|
|
|
|
|
return wrapper
|