Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions custom_components/pyscript/stubs/pyscript_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,70 @@
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

hass: HomeAssistant

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.<function>``.
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.<function>``.
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(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Loading