From 42c50c5b5e6d87cc2a796c9447bfbab840ef0ea0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20D=C3=ADez?=
<2961735+manutenfruits@users.noreply.github.com>
Date: Wed, 31 Jul 2019 00:01:51 -0400
Subject: [PATCH] Add support for Roku TVs to be powered on or off (#25590)
---
homeassistant/components/roku/__init__.py | 4 +--
homeassistant/components/roku/manifest.json | 2 +-
homeassistant/components/roku/media_player.py | 27 ++++++++++++++-----
homeassistant/components/roku/remote.py | 8 +++---
requirements_all.txt | 6 ++---
5 files changed, 30 insertions(+), 17 deletions(-)
diff --git a/homeassistant/components/roku/__init__.py b/homeassistant/components/roku/__init__.py
index 3444f8a3183..a737b48f572 100644
--- a/homeassistant/components/roku/__init__.py
+++ b/homeassistant/components/roku/__init__.py
@@ -74,7 +74,7 @@ def scan_for_rokus(hass):
continue
devices.append('Name: {0}
Host: {1}
'.format(
r_info.userdevicename if r_info.userdevicename
- else "{} {}".format(r_info.modelname, r_info.sernum),
+ else "{} {}".format(r_info.modelname, r_info.serial_num),
roku.host))
if not devices:
devices = ['No device(s) found']
@@ -98,7 +98,7 @@ def _setup_roku(hass, hass_config, roku_config):
r_info = roku.device_info
hass.data[DATA_ROKU][host] = {
- ATTR_ROKU: r_info.sernum
+ ATTR_ROKU: r_info.serial_num
}
discovery.load_platform(
diff --git a/homeassistant/components/roku/manifest.json b/homeassistant/components/roku/manifest.json
index 7f7befbe418..6bdc1f6bf3d 100644
--- a/homeassistant/components/roku/manifest.json
+++ b/homeassistant/components/roku/manifest.json
@@ -3,7 +3,7 @@
"name": "Roku",
"documentation": "https://www.home-assistant.io/components/roku",
"requirements": [
- "python-roku==3.1.5"
+ "roku==3.0.0"
],
"dependencies": [],
"codeowners": []
diff --git a/homeassistant/components/roku/media_player.py b/homeassistant/components/roku/media_player.py
index 41eff531de3..134bc71ea81 100644
--- a/homeassistant/components/roku/media_player.py
+++ b/homeassistant/components/roku/media_player.py
@@ -6,9 +6,9 @@ from homeassistant.components.media_player import MediaPlayerDevice
from homeassistant.components.media_player.const import (
MEDIA_TYPE_MOVIE, SUPPORT_NEXT_TRACK, SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK, SUPPORT_SELECT_SOURCE,
- SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET)
+ SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_TURN_ON, SUPPORT_TURN_OFF)
from homeassistant.const import (CONF_HOST, STATE_HOME, STATE_IDLE,
- STATE_PLAYING)
+ STATE_PLAYING, STATE_OFF)
DEFAULT_PORT = 8060
@@ -16,7 +16,7 @@ _LOGGER = logging.getLogger(__name__)
SUPPORT_ROKU = SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK |\
SUPPORT_PLAY_MEDIA | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\
- SUPPORT_SELECT_SOURCE | SUPPORT_PLAY
+ SUPPORT_SELECT_SOURCE | SUPPORT_PLAY | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
async def async_setup_platform(
@@ -41,11 +41,13 @@ class RokuDevice(MediaPlayerDevice):
self.channels = []
self.current_app = None
self._device_info = {}
+ self._power_state = "Unknown"
def update(self):
"""Retrieve latest state."""
try:
self._device_info = self.roku.device_info
+ self._power_state = self.roku.power_state
self.ip_address = self.roku.host
self.channels = self.get_source_list()
@@ -69,13 +71,16 @@ class RokuDevice(MediaPlayerDevice):
@property
def name(self):
"""Return the name of the device."""
- if self._device_info.userdevicename:
- return self._device_info.userdevicename
- return "Roku {}".format(self._device_info.sernum)
+ if self._device_info.user_device_name:
+ return self._device_info.user_device_name
+ return "Roku {}".format(self._device_info.serial_num)
@property
def state(self):
"""Return the state of the device."""
+ if self._power_state == "Off":
+ return STATE_OFF
+
if self.current_app is None:
return None
@@ -97,7 +102,7 @@ class RokuDevice(MediaPlayerDevice):
@property
def unique_id(self):
"""Return a unique, HASS-friendly identifier for this entity."""
- return self._device_info.sernum
+ return self._device_info.serial_num
@property
def media_content_type(self):
@@ -148,6 +153,14 @@ class RokuDevice(MediaPlayerDevice):
"""List of available input sources."""
return self.channels
+ def turn_on(self):
+ """Turn on the Roku."""
+ self.roku.power()
+
+ def turn_off(self):
+ """Turn off the Roku."""
+ self.roku.poweroff()
+
def media_play_pause(self):
"""Send play/pause command."""
if self.current_app is not None:
diff --git a/homeassistant/components/roku/remote.py b/homeassistant/components/roku/remote.py
index 59ecbe351ad..b4995311d6e 100644
--- a/homeassistant/components/roku/remote.py
+++ b/homeassistant/components/roku/remote.py
@@ -36,14 +36,14 @@ class RokuRemote(remote.RemoteDevice):
@property
def name(self):
"""Return the name of the device."""
- if self._device_info.userdevicename:
- return self._device_info.userdevicename
- return "Roku {}".format(self._device_info.sernum)
+ if self._device_info.user_device_name:
+ return self._device_info.user_device_name
+ return "Roku {}".format(self._device_info.serial_num)
@property
def unique_id(self):
"""Return a unique ID."""
- return self._device_info.sernum
+ return self._device_info.serial_num
@property
def is_on(self):
diff --git a/requirements_all.txt b/requirements_all.txt
index 254e8ee58a3..aa6d90fb736 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -1494,9 +1494,6 @@ python-qbittorrent==0.3.1
# homeassistant.components.ripple
python-ripple-api==0.0.3
-# homeassistant.components.roku
-python-roku==3.1.5
-
# homeassistant.components.sochain
python-sochain-api==0.0.2
@@ -1642,6 +1639,9 @@ rjpl==0.3.5
# homeassistant.components.rocketchat
rocketchat-API==0.6.1
+# homeassistant.components.roku
+roku==3.0.0
+
# homeassistant.components.roomba
roombapy==1.3.1