Improve decorator type annotations [matter] (#104822)

This commit is contained in:
Marc Mueller 2023-11-30 18:47:18 +01:00 committed by GitHub
parent 8e2f4a347c
commit 6ffc298986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,9 @@
"""Handle websocket api for Matter.""" """Handle websocket api for Matter."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable, Coroutine
from functools import wraps from functools import wraps
from typing import Any from typing import Any, Concatenate, ParamSpec
from matter_server.common.errors import MatterError from matter_server.common.errors import MatterError
import voluptuous as vol import voluptuous as vol
@ -15,6 +15,8 @@ from homeassistant.core import HomeAssistant, callback
from .adapter import MatterAdapter from .adapter import MatterAdapter
from .helpers import get_matter from .helpers import get_matter
_P = ParamSpec("_P")
ID = "id" ID = "id"
TYPE = "type" TYPE = "type"
@ -28,12 +30,19 @@ def async_register_api(hass: HomeAssistant) -> None:
websocket_api.async_register_command(hass, websocket_set_wifi_credentials) websocket_api.async_register_command(hass, websocket_set_wifi_credentials)
def async_get_matter_adapter(func: Callable) -> Callable: def async_get_matter_adapter(
func: Callable[
[HomeAssistant, ActiveConnection, dict[str, Any], MatterAdapter],
Coroutine[Any, Any, None],
],
) -> Callable[
[HomeAssistant, ActiveConnection, dict[str, Any]], Coroutine[Any, Any, None]
]:
"""Decorate function to get the MatterAdapter.""" """Decorate function to get the MatterAdapter."""
@wraps(func) @wraps(func)
async def _get_matter( async def _get_matter(
hass: HomeAssistant, connection: ActiveConnection, msg: dict hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Provide the Matter client to the function.""" """Provide the Matter client to the function."""
matter = get_matter(hass) matter = get_matter(hass)
@ -43,7 +52,15 @@ def async_get_matter_adapter(func: Callable) -> Callable:
return _get_matter return _get_matter
def async_handle_failed_command(func: Callable) -> Callable: def async_handle_failed_command(
func: Callable[
Concatenate[HomeAssistant, ActiveConnection, dict[str, Any], _P],
Coroutine[Any, Any, None],
],
) -> Callable[
Concatenate[HomeAssistant, ActiveConnection, dict[str, Any], _P],
Coroutine[Any, Any, None],
]:
"""Decorate function to handle MatterError and send relevant error.""" """Decorate function to handle MatterError and send relevant error."""
@wraps(func) @wraps(func)
@ -51,8 +68,8 @@ def async_handle_failed_command(func: Callable) -> Callable:
hass: HomeAssistant, hass: HomeAssistant,
connection: ActiveConnection, connection: ActiveConnection,
msg: dict[str, Any], msg: dict[str, Any],
*args: Any, *args: _P.args,
**kwargs: Any, **kwargs: _P.kwargs,
) -> None: ) -> None:
"""Handle MatterError within function and send relevant error.""" """Handle MatterError within function and send relevant error."""
try: try: