Add config flow to roku (#31988)

* create a dedicated const.py

* add DEFAULT_PORT to const.py

* work on config flow conversion.

* remove discovery.

* work on config flow and add tests. other cleanup.

* work on config flow and add tests. other cleanup.

* add quality scale to manifest.

* work on config flow and add tests. other cleanup.

* review tweaks.

* Update manifest.json

* catch more specific errors

* catch more errors.

* impprt specific exceptions

* import specific exceptions

* Update __init__.py

* Update config_flow.py

* Update media_player.py

* Update remote.py

* Update media_player.py

* Update remote.py

* Update media_player.py

* Update remote.py

* Update config_flow.py

* Update config_flow.py

* Update media_player.py

* Update __init__.py

* Update __init__.py

* Update config_flow.py

* Update test_config_flow.py

* Update config_flow.py

* Update __init__.py

* Update test_config_flow.py

* Update remote.py

* Update test_init.py

* Update test_init.py

* Update media_player.py

* Update media_player.py

* Update media_player.py
This commit is contained in:
Chris Talkington authored and GitHub committed 2020-03-15 21:13:04 -07:00
1 parent 6e95b90f42
commit cf8dfdae47
16 files changed
+716 -125

No files matched your search

+46 -16
View File
@@ -1,34 +1,48 @@
"""Support for the Roku remote."""
import requests.exceptions
from roku import Roku
from typing import Callable, List
from homeassistant.components import remote
from homeassistant.const import CONF_HOST
from requests.exceptions import (
ConnectionError as RequestsConnectionError,
ReadTimeout as RequestsReadTimeout,
)
from roku import RokuException
from homeassistant.components.remote import RemoteDevice
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
from .const import DATA_CLIENT, DEFAULT_MANUFACTURER, DOMAIN
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Roku remote platform."""
if not discovery_info:
return
host = discovery_info[CONF_HOST]
async_add_entities([RokuRemote(host)], True)
async def async_setup_entry(
hass: HomeAssistantType,
entry: ConfigEntry,
async_add_entities: Callable[[List, bool], None],
) -> bool:
"""Load Roku remote based on a config entry."""
roku = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
async_add_entities([RokuRemote(roku)], True)
class RokuRemote(remote.RemoteDevice):
class RokuRemote(RemoteDevice):
"""Device that sends commands to an Roku."""
def __init__(self, host):
def __init__(self, roku):
"""Initialize the Roku device."""
self.roku = Roku(host)
self.roku = roku
self._available = False
self._device_info = {}
def update(self):
"""Retrieve latest state."""
if not self.enabled:
return
try:
self._device_info = self.roku.device_info
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout):
self._available = True
except (RequestsConnectionError, RequestsReadTimeout, RokuException):
self._available = False
pass
@property
@@ -38,11 +52,27 @@ class RokuRemote(remote.RemoteDevice):
return self._device_info.user_device_name
return f"Roku {self._device_info.serial_num}"
@property
def available(self):
"""Return if able to retrieve information from device or not."""
return self._available
@property
def unique_id(self):
"""Return a unique ID."""
return self._device_info.serial_num
@property
def device_info(self):
"""Return device specific attributes."""
return {
"name": self.name,
"identifiers": {(DOMAIN, self.unique_id)},
"manufacturer": DEFAULT_MANUFACTURER,
"model": self._device_info.model_num,
"sw_version": self._device_info.software_version,
}
@property
def is_on(self):
"""Return true if device is on."""