Add roku exception handling for service calls (#36328)

This commit is contained in:
Chris Talkington 2020-06-04 11:59:39 -05:00 committed by GitHub
parent 36b157b85a
commit f06c0a8b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -4,7 +4,7 @@ from datetime import timedelta
import logging import logging
from typing import Any, Dict from typing import Any, Dict
from rokuecp import Roku, RokuError from rokuecp import Roku, RokuConnectionError, RokuError
from rokuecp.models import Device from rokuecp.models import Device
import voluptuous as vol import voluptuous as vol
@ -92,6 +92,22 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
return unload_ok return unload_ok
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
class RokuDataUpdateCoordinator(DataUpdateCoordinator): class RokuDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching Roku data.""" """Class to manage fetching Roku data."""

View File

@ -19,7 +19,7 @@ from homeassistant.components.media_player.const import (
) )
from homeassistant.const import STATE_HOME, STATE_IDLE, STATE_PLAYING, STATE_STANDBY from homeassistant.const import STATE_HOME, STATE_IDLE, STATE_PLAYING, STATE_STANDBY
from . import RokuDataUpdateCoordinator, RokuEntity from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from .const import DOMAIN from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -161,49 +161,60 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
"""List of available input sources.""" """List of available input sources."""
return ["Home"] + sorted(app.name for app in self.coordinator.data.apps) return ["Home"] + sorted(app.name for app in self.coordinator.data.apps)
@roku_exception_handler
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn on the Roku.""" """Turn on the Roku."""
await self.coordinator.roku.remote("poweron") await self.coordinator.roku.remote("poweron")
@roku_exception_handler
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off the Roku.""" """Turn off the Roku."""
await self.coordinator.roku.remote("poweroff") await self.coordinator.roku.remote("poweroff")
@roku_exception_handler
async def async_media_pause(self) -> None: async def async_media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
if self.state != STATE_STANDBY: if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play") await self.coordinator.roku.remote("play")
@roku_exception_handler
async def async_media_play(self) -> None: async def async_media_play(self) -> None:
"""Send play command.""" """Send play command."""
if self.state != STATE_STANDBY: if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play") await self.coordinator.roku.remote("play")
@roku_exception_handler
async def async_media_play_pause(self) -> None: async def async_media_play_pause(self) -> None:
"""Send play/pause command.""" """Send play/pause command."""
if self.state != STATE_STANDBY: if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play") await self.coordinator.roku.remote("play")
@roku_exception_handler
async def async_media_previous_track(self) -> None: async def async_media_previous_track(self) -> None:
"""Send previous track command.""" """Send previous track command."""
await self.coordinator.roku.remote("reverse") await self.coordinator.roku.remote("reverse")
@roku_exception_handler
async def async_media_next_track(self) -> None: async def async_media_next_track(self) -> None:
"""Send next track command.""" """Send next track command."""
await self.coordinator.roku.remote("forward") await self.coordinator.roku.remote("forward")
@roku_exception_handler
async def async_mute_volume(self, mute) -> None: async def async_mute_volume(self, mute) -> None:
"""Mute the volume.""" """Mute the volume."""
await self.coordinator.roku.remote("volume_mute") await self.coordinator.roku.remote("volume_mute")
@roku_exception_handler
async def async_volume_up(self) -> None: async def async_volume_up(self) -> None:
"""Volume up media player.""" """Volume up media player."""
await self.coordinator.roku.remote("volume_up") await self.coordinator.roku.remote("volume_up")
@roku_exception_handler
async def async_volume_down(self) -> None: async def async_volume_down(self) -> None:
"""Volume down media player.""" """Volume down media player."""
await self.coordinator.roku.remote("volume_down") await self.coordinator.roku.remote("volume_down")
@roku_exception_handler
async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None: async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None:
"""Tune to channel.""" """Tune to channel."""
if media_type != MEDIA_TYPE_CHANNEL: if media_type != MEDIA_TYPE_CHANNEL:
@ -216,6 +227,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
await self.coordinator.roku.tune(media_id) await self.coordinator.roku.tune(media_id)
@roku_exception_handler
async def async_select_source(self, source: str) -> None: async def async_select_source(self, source: str) -> None:
"""Select input source.""" """Select input source."""
if source == "Home": if source == "Home":

View File

@ -5,7 +5,7 @@ from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from . import RokuDataUpdateCoordinator, RokuEntity from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from .const import DOMAIN from .const import DOMAIN
@ -43,14 +43,17 @@ class RokuRemote(RokuEntity, RemoteEntity):
"""Return true if device is on.""" """Return true if device is on."""
return not self.coordinator.data.state.standby return not self.coordinator.data.state.standby
@roku_exception_handler
async def async_turn_on(self, **kwargs) -> None: async def async_turn_on(self, **kwargs) -> None:
"""Turn the device on.""" """Turn the device on."""
await self.coordinator.roku.remote("poweron") await self.coordinator.roku.remote("poweron")
@roku_exception_handler
async def async_turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs) -> None:
"""Turn the device off.""" """Turn the device off."""
await self.coordinator.roku.remote("poweroff") await self.coordinator.roku.remote("poweroff")
@roku_exception_handler
async def async_send_command(self, command: List, **kwargs) -> None: async def async_send_command(self, command: List, **kwargs) -> None:
"""Send a command to one device.""" """Send a command to one device."""
num_repeats = kwargs[ATTR_NUM_REPEATS] num_repeats = kwargs[ATTR_NUM_REPEATS]