Skip to content

Commit

Permalink
Add HassKey support to singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed May 2, 2024
1 parent 5ac6068 commit e8ed9a0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions homeassistant/helpers/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
import asyncio
from collections.abc import Callable
import functools
from typing import Any, TypeVar, cast
from typing import Any, TypeVar, cast, overload

from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass
from homeassistant.util.hass_dict import HassKey

_T = TypeVar("_T")

_FuncType = Callable[[HomeAssistant], _T]


def singleton(data_key: str) -> Callable[[_FuncType[_T]], _FuncType[_T]]:
@overload
def singleton(data_key: HassKey[_T]) -> Callable[[_FuncType[_T]], _FuncType[_T]]: ...


@overload
def singleton(data_key: str) -> Callable[[_FuncType[_T]], _FuncType[_T]]: ...


def singleton(data_key: Any) -> Callable[[_FuncType[_T]], _FuncType[_T]]:
"""Decorate a function that should be called once per instance.
Result will be cached and simultaneous calls will be handled.
Expand Down

0 comments on commit e8ed9a0

Please sign in to comment.