Skip to content

Commit

Permalink
Improve hassio decorator typing (#110545)
Browse files Browse the repository at this point in the history
* Improve hassio decorator typing

* Fix typing
  • Loading branch information
cdce8p authored Feb 14, 2024
1 parent 0bbe46d commit 269f6be
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions homeassistant/components/hassio/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import annotations

import asyncio
from collections.abc import Coroutine
from collections.abc import Callable, Coroutine
from http import HTTPStatus
import logging
import os
from typing import Any
from typing import Any, ParamSpec

import aiohttp
from yarl import URL
Expand All @@ -23,17 +23,21 @@

from .const import ATTR_DISCOVERY, DOMAIN, X_HASS_SOURCE

_P = ParamSpec("_P")

_LOGGER = logging.getLogger(__name__)


class HassioAPIError(RuntimeError):
"""Return if a API trow a error."""


def _api_bool(funct):
def _api_bool(
funct: Callable[_P, Coroutine[Any, Any, dict[str, Any]]],
) -> Callable[_P, Coroutine[Any, Any, bool]]:
"""Return a boolean."""

async def _wrapper(*argv, **kwargs):
async def _wrapper(*argv: _P.args, **kwargs: _P.kwargs) -> bool:
"""Wrap function."""
try:
data = await funct(*argv, **kwargs)
Expand All @@ -44,10 +48,12 @@ async def _wrapper(*argv, **kwargs):
return _wrapper


def api_data(funct):
def api_data(
funct: Callable[_P, Coroutine[Any, Any, dict[str, Any]]],
) -> Callable[_P, Coroutine[Any, Any, Any]]:
"""Return data of an api."""

async def _wrapper(*argv, **kwargs):
async def _wrapper(*argv: _P.args, **kwargs: _P.kwargs) -> Any:
"""Wrap function."""
data = await funct(*argv, **kwargs)
if data["result"] == "ok":
Expand Down Expand Up @@ -80,7 +86,7 @@ async def async_get_addon_store_info(hass: HomeAssistant, slug: str) -> dict:


@bind_hass
async def async_update_diagnostics(hass: HomeAssistant, diagnostics: bool) -> dict:
async def async_update_diagnostics(hass: HomeAssistant, diagnostics: bool) -> bool:
"""Update Supervisor diagnostics toggle.
The caller of the function should handle HassioAPIError.
Expand Down Expand Up @@ -255,7 +261,7 @@ async def async_update_core(

@bind_hass
@_api_bool
async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> bool:
async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> dict:
"""Apply a suggestion from supervisor's resolution center.
The caller of the function should handle HassioAPIError.
Expand Down

0 comments on commit 269f6be

Please sign in to comment.