core/homeassistant/components/roku/helpers.py

57 lines
1.9 KiB
Python
Raw Normal View History

2022-02-12 02:52:31 +00:00
"""Helpers for Roku."""
from __future__ import annotations
from collections.abc import Awaitable, Callable, Coroutine
from functools import wraps
from typing import Any, TypeVar
from rokuecp import RokuConnectionError, RokuConnectionTimeoutError, RokuError
from typing_extensions import Concatenate, ParamSpec
2022-05-29 02:03:13 +00:00
from homeassistant.exceptions import HomeAssistantError
2022-05-29 02:03:13 +00:00
from .entity import RokuEntity
_RokuEntityT = TypeVar("_RokuEntityT", bound=RokuEntity)
_P = ParamSpec("_P")
2022-02-12 02:52:31 +00:00
def format_channel_name(channel_number: str, channel_name: str | None = None) -> str:
"""Format a Roku Channel name."""
if channel_name is not None and channel_name != "":
return f"{channel_name} ({channel_number})"
return channel_number
def roku_exception_handler(
ignore_timeout: bool = False,
) -> Callable[
[Callable[Concatenate[_RokuEntityT, _P], Awaitable[Any]]],
Callable[Concatenate[_RokuEntityT, _P], Coroutine[Any, Any, None]],
]:
"""Decorate Roku calls to handle Roku exceptions."""
def decorator(
func: Callable[Concatenate[_RokuEntityT, _P], Awaitable[Any]],
) -> Callable[Concatenate[_RokuEntityT, _P], Coroutine[Any, Any, None]]:
@wraps(func)
async def wrapper(
self: _RokuEntityT, *args: _P.args, **kwargs: _P.kwargs
) -> None:
try:
await func(self, *args, **kwargs)
except RokuConnectionTimeoutError as error:
2022-05-29 02:03:13 +00:00
if not ignore_timeout:
raise HomeAssistantError(
"Timeout communicating with Roku API"
) from error
except RokuConnectionError as error:
2022-05-29 02:03:13 +00:00
raise HomeAssistantError("Error communicating with Roku API") from error
except RokuError as error:
2022-05-29 02:03:13 +00:00
raise HomeAssistantError("Invalid response from Roku API") from error
return wrapper
return decorator