2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Roku."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-05-08 21:44:34 +00:00
|
|
|
import logging
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2021-07-05 08:27:52 +00:00
|
|
|
from rokuecp import RokuConnectionError, RokuError
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2020-03-16 04:13:04 +00:00
|
|
|
from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN
|
|
|
|
from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN
|
2020-10-29 08:51:48 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-06-11 11:51:18 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
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
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2020-12-12 21:24:16 +00:00
|
|
|
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
2019-01-14 07:44:30 +00:00
|
|
|
|
2020-03-16 04:13:04 +00:00
|
|
|
PLATFORMS = [MEDIA_PLAYER_DOMAIN, REMOTE_DOMAIN]
|
2020-05-08 21:44:34 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
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-04-11 17:35:42 +00:00
|
|
|
coordinator = hass.data[DOMAIN].get(entry.entry_id)
|
|
|
|
if not coordinator:
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-06-04 16:59:39 +00:00
|
|
|
def roku_exception_handler(func):
|
|
|
|
"""Decorate Roku calls to handle Roku exceptions."""
|
|
|
|
|
|
|
|
async def handler(self, *args, **kwargs):
|
|
|
|
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)
|
|
|
|
|
|
|
|
return handler
|