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