2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Roku."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-07 01:13:01 +00:00
|
|
|
from collections.abc import Awaitable, Callable, Coroutine
|
|
|
|
from functools import wraps
|
2020-05-08 21:44:34 +00:00
|
|
|
import logging
|
2022-02-07 01:13:01 +00:00
|
|
|
from typing import Any, TypeVar
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2021-07-05 08:27:52 +00:00
|
|
|
from rokuecp import RokuConnectionError, RokuError
|
2022-02-07 01:13:01 +00:00
|
|
|
from typing_extensions import Concatenate, ParamSpec
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2020-10-29 08:51:48 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 12:19:49 +00:00
|
|
|
from homeassistant.const import CONF_HOST, Platform
|
2021-04-22 17:58:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-03-16 04:13:04 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2020-05-08 21:44:34 +00:00
|
|
|
|
2021-06-11 11:51:18 +00:00
|
|
|
from .const import DOMAIN
|
2021-07-05 08:27:52 +00:00
|
|
|
from .coordinator import RokuDataUpdateCoordinator
|
2022-02-07 01:13:01 +00:00
|
|
|
from .entity import RokuEntity
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2021-12-21 11:46:10 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2022-01-17 02:39:18 +00:00
|
|
|
PLATFORMS = [
|
|
|
|
Platform.BINARY_SENSOR,
|
|
|
|
Platform.MEDIA_PLAYER,
|
|
|
|
Platform.REMOTE,
|
|
|
|
Platform.SENSOR,
|
|
|
|
]
|
2020-05-08 21:44:34 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2022-02-07 01:13:01 +00:00
|
|
|
_T = TypeVar("_T", bound="RokuEntity")
|
|
|
|
_P = ParamSpec("_P")
|
|
|
|
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2021-04-22 17:58:02 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-16 04:13:04 +00:00
|
|
|
"""Set up Roku from a config entry."""
|
2021-04-16 16:22:56 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2021-10-21 06:33:10 +00:00
|
|
|
if not (coordinator := hass.data[DOMAIN].get(entry.entry_id)):
|
2021-04-11 17:35:42 +00:00
|
|
|
coordinator = RokuDataUpdateCoordinator(hass, host=entry.data[CONF_HOST])
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
2020-05-08 21:44:34 +00:00
|
|
|
|
2021-04-11 17:35:42 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-04-27 20:10:04 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2020-03-16 04:13:04 +00:00
|
|
|
return True
|
2019-01-14 07:44:30 +00:00
|
|
|
|
|
|
|
|
2021-04-22 17:58:02 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-16 04:13:04 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 20:10:04 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-03-16 04:13:04 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|
2020-05-08 21:44:34 +00:00
|
|
|
|
|
|
|
|
2022-02-07 01:13:01 +00:00
|
|
|
def roku_exception_handler(
|
|
|
|
func: Callable[Concatenate[_T, _P], Awaitable[None]] # type: ignore[misc]
|
|
|
|
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]: # type: ignore[misc]
|
2020-06-04 16:59:39 +00:00
|
|
|
"""Decorate Roku calls to handle Roku exceptions."""
|
|
|
|
|
2022-02-07 01:13:01 +00:00
|
|
|
@wraps(func)
|
|
|
|
async def wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
2020-06-04 16:59:39 +00:00
|
|
|
try:
|
|
|
|
await func(self, *args, **kwargs)
|
|
|
|
except RokuConnectionError as error:
|
|
|
|
if self.available:
|
|
|
|
_LOGGER.error("Error communicating with API: %s", error)
|
|
|
|
except RokuError as error:
|
|
|
|
if self.available:
|
|
|
|
_LOGGER.error("Invalid response from API: %s", error)
|
|
|
|
|
2022-02-07 01:13:01 +00:00
|
|
|
return wrapper
|