2019-02-13 20:21:14 +00:00
|
|
|
"""Support for the Roku media player."""
|
2016-05-15 20:00:31 +00:00
|
|
|
import logging
|
2019-12-01 11:03:51 +00:00
|
|
|
|
2019-01-14 07:44:30 +00:00
|
|
|
import requests.exceptions
|
2019-12-01 11:03:51 +00:00
|
|
|
from roku import Roku
|
2016-09-05 16:05:27 +00:00
|
|
|
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player import MediaPlayerDevice
|
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MEDIA_TYPE_MOVIE,
|
|
|
|
SUPPORT_NEXT_TRACK,
|
|
|
|
SUPPORT_PLAY,
|
|
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
|
|
SUPPORT_SELECT_SOURCE,
|
2019-12-01 11:03:51 +00:00
|
|
|
SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_VOLUME_MUTE,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
)
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
STATE_HOME,
|
|
|
|
STATE_IDLE,
|
|
|
|
STATE_PLAYING,
|
2019-10-23 20:03:52 +00:00
|
|
|
STATE_STANDBY,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-15 20:00:31 +00:00
|
|
|
|
2020-03-04 22:24:59 +00:00
|
|
|
from .const import DEFAULT_PORT
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_ROKU = (
|
|
|
|
SUPPORT_PREVIOUS_TRACK
|
|
|
|
| SUPPORT_NEXT_TRACK
|
|
|
|
| SUPPORT_VOLUME_SET
|
|
|
|
| SUPPORT_VOLUME_MUTE
|
|
|
|
| SUPPORT_SELECT_SOURCE
|
|
|
|
| SUPPORT_PLAY
|
|
|
|
| SUPPORT_TURN_ON
|
|
|
|
| SUPPORT_TURN_OFF
|
|
|
|
)
|
2016-05-15 20:00:31 +00:00
|
|
|
|
2016-09-05 16:05:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Roku platform."""
|
2019-01-14 07:44:30 +00:00
|
|
|
if not discovery_info:
|
|
|
|
return
|
2016-05-15 20:00:31 +00:00
|
|
|
|
2019-01-14 07:44:30 +00:00
|
|
|
host = discovery_info[CONF_HOST]
|
|
|
|
async_add_entities([RokuDevice(host)], True)
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RokuDevice(MediaPlayerDevice):
|
|
|
|
"""Representation of a Roku device on the network."""
|
|
|
|
|
|
|
|
def __init__(self, host):
|
|
|
|
"""Initialize the Roku device."""
|
|
|
|
|
|
|
|
self.roku = Roku(host)
|
2016-07-03 22:17:08 +00:00
|
|
|
self.ip_address = host
|
|
|
|
self.channels = []
|
|
|
|
self.current_app = None
|
2018-08-25 08:59:28 +00:00
|
|
|
self._device_info = {}
|
2019-07-31 04:01:51 +00:00
|
|
|
self._power_state = "Unknown"
|
2016-07-03 22:17:08 +00:00
|
|
|
|
2016-05-15 20:00:31 +00:00
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-05-29 21:24:06 +00:00
|
|
|
try:
|
2018-08-25 08:59:28 +00:00
|
|
|
self._device_info = self.roku.device_info
|
2019-07-31 04:01:51 +00:00
|
|
|
self._power_state = self.roku.power_state
|
2016-05-29 21:24:06 +00:00
|
|
|
self.ip_address = self.roku.host
|
|
|
|
self.channels = self.get_source_list()
|
|
|
|
|
|
|
|
if self.roku.current_app is not None:
|
|
|
|
self.current_app = self.roku.current_app
|
|
|
|
else:
|
|
|
|
self.current_app = None
|
2019-07-31 19:25:30 +00:00
|
|
|
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout):
|
2016-08-13 16:45:09 +00:00
|
|
|
pass
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def get_source_list(self):
|
|
|
|
"""Get the list of applications to be used as sources."""
|
|
|
|
return ["Home"] + sorted(channel.name for channel in self.roku.apps)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Device should be polled."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2019-07-31 04:01:51 +00:00
|
|
|
if self._device_info.user_device_name:
|
|
|
|
return self._device_info.user_device_name
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"Roku {self._device_info.serial_num}"
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2019-07-31 04:01:51 +00:00
|
|
|
if self._power_state == "Off":
|
2019-10-23 20:03:52 +00:00
|
|
|
return STATE_STANDBY
|
2019-07-31 04:01:51 +00:00
|
|
|
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is None:
|
2019-01-24 07:20:20 +00:00
|
|
|
return None
|
2016-05-29 21:24:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.current_app.name == "Power Saver" or self.current_app.is_screensaver:
|
2016-05-15 20:00:31 +00:00
|
|
|
return STATE_IDLE
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name == "Roku":
|
2016-05-15 20:00:31 +00:00
|
|
|
return STATE_HOME
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name is not None:
|
2016-05-15 20:00:31 +00:00
|
|
|
return STATE_PLAYING
|
|
|
|
|
2019-01-24 07:20:20 +00:00
|
|
|
return None
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2016-05-15 20:00:31 +00:00
|
|
|
return SUPPORT_ROKU
|
|
|
|
|
2018-05-07 17:18:51 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2019-07-31 04:01:51 +00:00
|
|
|
return self._device_info.serial_num
|
2018-05-07 17:18:51 +00:00
|
|
|
|
2016-05-15 20:00:31 +00:00
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
"""Content type of current playing media."""
|
|
|
|
if self.current_app is None:
|
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name == "Power Saver":
|
2016-05-15 20:00:31 +00:00
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name == "Roku":
|
2016-05-15 20:00:31 +00:00
|
|
|
return None
|
2018-04-05 16:44:38 +00:00
|
|
|
return MEDIA_TYPE_MOVIE
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_image_url(self):
|
|
|
|
"""Image url of current playing media."""
|
|
|
|
if self.current_app is None:
|
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name == "Roku":
|
2016-05-15 20:00:31 +00:00
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.name == "Power Saver":
|
2016-05-15 20:00:31 +00:00
|
|
|
return None
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.current_app.id is None:
|
2016-05-15 20:00:31 +00:00
|
|
|
return None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return "http://{0}:{1}/query/icon/{2}".format(
|
|
|
|
self.ip_address, DEFAULT_PORT, self.current_app.id
|
|
|
|
)
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def app_name(self):
|
|
|
|
"""Name of the current running app."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
return self.current_app.name
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def app_id(self):
|
|
|
|
"""Return the ID of the current running app."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
return self.current_app.id
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
return self.current_app.name
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return self.channels
|
|
|
|
|
2019-07-31 04:01:51 +00:00
|
|
|
def turn_on(self):
|
|
|
|
"""Turn on the Roku."""
|
2019-11-27 17:19:10 +00:00
|
|
|
self.roku.poweron()
|
2019-07-31 04:01:51 +00:00
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn off the Roku."""
|
|
|
|
self.roku.poweroff()
|
|
|
|
|
2016-05-15 20:00:31 +00:00
|
|
|
def media_play_pause(self):
|
|
|
|
"""Send play/pause command."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.play()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def media_previous_track(self):
|
|
|
|
"""Send previous track command."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.reverse()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def media_next_track(self):
|
|
|
|
"""Send next track command."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.forward()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute the volume."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.volume_mute()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def volume_up(self):
|
|
|
|
"""Volume up media player."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.volume_up()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def volume_down(self):
|
|
|
|
"""Volume down media player."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
self.roku.volume_down()
|
2016-05-15 20:00:31 +00:00
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Select input source."""
|
2016-05-29 21:24:06 +00:00
|
|
|
if self.current_app is not None:
|
|
|
|
if source == "Home":
|
|
|
|
self.roku.home()
|
|
|
|
else:
|
|
|
|
channel = self.roku[source]
|
|
|
|
channel.launch()
|