Skip to content

Commit

Permalink
Add roku exception handling for service calls (home-assistant#36328)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington authored Jun 4, 2020
1 parent 36b157b commit f06c0a8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
18 changes: 17 additions & 1 deletion homeassistant/components/roku/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from typing import Any, Dict

from rokuecp import Roku, RokuError
from rokuecp import Roku, RokuConnectionError, RokuError
from rokuecp.models import Device
import voluptuous as vol

Expand Down Expand Up @@ -92,6 +92,22 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
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 to manage fetching Roku data."""

Expand Down
14 changes: 13 additions & 1 deletion homeassistant/components/roku/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
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

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -161,49 +161,60 @@ def source_list(self) -> List:
"""List of available input sources."""
return ["Home"] + sorted(app.name for app in self.coordinator.data.apps)

@roku_exception_handler
async def async_turn_on(self) -> None:
"""Turn on the Roku."""
await self.coordinator.roku.remote("poweron")

@roku_exception_handler
async def async_turn_off(self) -> None:
"""Turn off the Roku."""
await self.coordinator.roku.remote("poweroff")

@roku_exception_handler
async def async_media_pause(self) -> None:
"""Send pause command."""
if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play")

@roku_exception_handler
async def async_media_play(self) -> None:
"""Send play command."""
if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play")

@roku_exception_handler
async def async_media_play_pause(self) -> None:
"""Send play/pause command."""
if self.state != STATE_STANDBY:
await self.coordinator.roku.remote("play")

@roku_exception_handler
async def async_media_previous_track(self) -> None:
"""Send previous track command."""
await self.coordinator.roku.remote("reverse")

@roku_exception_handler
async def async_media_next_track(self) -> None:
"""Send next track command."""
await self.coordinator.roku.remote("forward")

@roku_exception_handler
async def async_mute_volume(self, mute) -> None:
"""Mute the volume."""
await self.coordinator.roku.remote("volume_mute")

@roku_exception_handler
async def async_volume_up(self) -> None:
"""Volume up media player."""
await self.coordinator.roku.remote("volume_up")

@roku_exception_handler
async def async_volume_down(self) -> None:
"""Volume down media player."""
await self.coordinator.roku.remote("volume_down")

@roku_exception_handler
async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None:
"""Tune to channel."""
if media_type != MEDIA_TYPE_CHANNEL:
Expand All @@ -216,6 +227,7 @@ async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> No

await self.coordinator.roku.tune(media_id)

@roku_exception_handler
async def async_select_source(self, source: str) -> None:
"""Select input source."""
if source == "Home":
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/roku/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType

from . import RokuDataUpdateCoordinator, RokuEntity
from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from .const import DOMAIN


Expand Down Expand Up @@ -43,14 +43,17 @@ def is_on(self) -> bool:
"""Return true if device is on."""
return not self.coordinator.data.state.standby

@roku_exception_handler
async def async_turn_on(self, **kwargs) -> None:
"""Turn the device on."""
await self.coordinator.roku.remote("poweron")

@roku_exception_handler
async def async_turn_off(self, **kwargs) -> None:
"""Turn the device off."""
await self.coordinator.roku.remote("poweroff")

@roku_exception_handler
async def async_send_command(self, command: List, **kwargs) -> None:
"""Send a command to one device."""
num_repeats = kwargs[ATTR_NUM_REPEATS]
Expand Down

0 comments on commit f06c0a8

Please sign in to comment.