diff --git a/custom_components/pyscript/stubs/pyscript_builtins.py b/custom_components/pyscript/stubs/pyscript_builtins.py index 10bd631..cfdee68 100644 --- a/custom_components/pyscript/stubs/pyscript_builtins.py +++ b/custom_components/pyscript/stubs/pyscript_builtins.py @@ -10,7 +10,7 @@ from asyncio import Task from collections.abc import Callable from datetime import datetime -from typing import Any, Literal +from typing import Any, Literal, TypeVar, overload from homeassistant.core import HomeAssistant @@ -18,17 +18,62 @@ WebhookMethod = Literal["GET", "HEAD", "POST", "PUT"] +_F = TypeVar("_F", bound=Callable[..., Any]) -def service( - *service_name: str, supports_response: Literal["none", "only", "optional"] = "none" -) -> Callable[..., Any]: - """Register the wrapped function as a Home Assistant service. - Args: - service_name: Optional ``DOMAIN.SERVICE`` aliases; defaults to ``pyscript.``. - supports_response: Advertised response mode (``"none"``, ``"only"``, or ``"optional"``). - """ - ... +class _service: + """``@service`` decorator, which also provides ``service.call()`` and ``service.has_service()``.""" + + @overload + def __call__(self, func: _F, /) -> _F: ... + + @overload + def __call__( + self, *service_name: str, supports_response: Literal["none", "only", "optional"] = "none" + ) -> Callable[[_F], _F]: ... + + def __call__(self, *service_name: Any, supports_response: Any = "none") -> Any: + """Register the wrapped function as a Home Assistant service. + + Args: + service_name: Optional ``DOMAIN.SERVICE`` aliases; defaults to ``pyscript.``. + supports_response: Advertised response mode (``"none"``, ``"only"``, or ``"optional"``). + """ + ... + + @staticmethod + def call( + domain: str, + name: str, + /, + *, + blocking: bool = False, + return_response: bool = False, + **kwargs: Any, + ) -> Any: + """Call the Home Assistant service ``domain.name``. + + Args: + domain: Service domain, e.g. ``"light"``. + name: Service name, e.g. ``"turn_on"``. + blocking: Wait for the service to finish. + return_response: Return the service's response; this also waits for the service to finish. + **kwargs: Service data. + """ + ... + + @staticmethod + def has_service(domain: str, name: str) -> bool: + """Return whether the service ``domain.name`` exists. + + Args: + domain: Service domain. + name: Service name. + """ + ... + + +service = _service() def state_trigger( diff --git a/tests/test_stubs.py b/tests/test_stubs.py index 002bf35..eadb0e2 100644 --- a/tests/test_stubs.py +++ b/tests/test_stubs.py @@ -10,6 +10,7 @@ import pytest from custom_components.pyscript.const import DOMAIN, FOLDER, SERVICE_GENERATE_STUBS +from custom_components.pyscript.stubs import pyscript_builtins from homeassistant.core import HomeAssistant from tests.test_init import setup_script @@ -501,3 +502,11 @@ def _service_signature( for arg, default in zip(func.args.kwonlyargs, func.args.kw_defaults, strict=True) } raise AssertionError(f"{class_name}.{func_name} not found in generated stubs") + + +def test_builtins_service_stub(): + """The service stub is both the @service decorator and the service.call() namespace (#788).""" + service = pyscript_builtins.service + assert callable(service) + assert callable(service.call) + assert callable(service.has_service)