1 """Helpers for Roku."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable, Coroutine
6 from functools
import wraps
7 from typing
import Any, Concatenate
9 from rokuecp
import RokuConnectionError, RokuConnectionTimeoutError, RokuError
13 from .entity
import RokuEntity
15 type _FuncType[_T, **_P] = Callable[Concatenate[_T, _P], Awaitable[Any]]
16 type _ReturnFuncType[_T, **_P] = Callable[
17 Concatenate[_T, _P], Coroutine[Any, Any,
None]
22 """Format a Roku Channel name."""
23 if channel_name
is not None and channel_name !=
"":
24 return f
"{channel_name} ({channel_number})"
29 def roku_exception_handler[_RokuEntityT: RokuEntity, **_P](
30 ignore_timeout: bool =
False,
31 ) -> Callable[[_FuncType[_RokuEntityT, _P]], _ReturnFuncType[_RokuEntityT, _P]]:
32 """Decorate Roku calls to handle Roku exceptions."""
35 func: _FuncType[_RokuEntityT, _P],
36 ) -> _ReturnFuncType[_RokuEntityT, _P]:
39 self: _RokuEntityT, *args: _P.args, **kwargs: _P.kwargs
42 await func(self, *args, **kwargs)
43 except RokuConnectionTimeoutError
as error:
44 if not ignore_timeout:
46 "Timeout communicating with Roku API"
48 except RokuConnectionError
as error:
50 except RokuError
as error:
str format_channel_name(str channel_number, str|None channel_name=None)