Configure Cast platform host via config

This commit is contained in:
Paulus Schoutsen 2015-08-17 22:37:01 -07:00
parent ebfec2d1d3
commit bf9d067a7d

View File

@ -8,14 +8,9 @@ WARNING: This platform is currently not working due to a changed Cast API
""" """
import logging import logging
try:
import pychromecast
except ImportError:
pychromecast = None
from homeassistant.const import ( from homeassistant.const import (
STATE_PLAYING, STATE_PAUSED, STATE_IDLE, STATE_OFF, STATE_PLAYING, STATE_PAUSED, STATE_IDLE, STATE_OFF,
STATE_UNKNOWN) STATE_UNKNOWN, CONF_HOST)
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
MediaPlayerDevice, MediaPlayerDevice,
@ -32,21 +27,23 @@ SUPPORT_CAST = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE
KNOWN_HOSTS = [] KNOWN_HOSTS = []
# pylint: disable=invalid-name
cast = None
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the cast platform. """ """ Sets up the cast platform. """
global pychromecast # pylint: disable=invalid-name global cast
if pychromecast is None: import pychromecast
import pychromecast as pychromecast_ cast = pychromecast
pychromecast = pychromecast_
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# import CEC IGNORE attributes # import CEC IGNORE attributes
ignore_cec = config.get(CONF_IGNORE_CEC, []) ignore_cec = config.get(CONF_IGNORE_CEC, [])
if isinstance(ignore_cec, list): if isinstance(ignore_cec, list):
pychromecast.IGNORE_CEC += ignore_cec cast.IGNORE_CEC += ignore_cec
else: else:
logger.error('Chromecast conig, %s must be a list.', CONF_IGNORE_CEC) logger.error('Chromecast conig, %s must be a list.', CONF_IGNORE_CEC)
@ -55,9 +52,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info and discovery_info[0] not in KNOWN_HOSTS: if discovery_info and discovery_info[0] not in KNOWN_HOSTS:
hosts = [discovery_info[0]] hosts = [discovery_info[0]]
elif CONF_HOST in config:
hosts = [config[CONF_HOST]]
else: else:
hosts = (host_port[0] for host_port hosts = (host_port[0] for host_port
in pychromecast.discover_chromecasts() in cast.discover_chromecasts()
if host_port[0] not in KNOWN_HOSTS) if host_port[0] not in KNOWN_HOSTS)
casts = [] casts = []
@ -65,7 +65,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for host in hosts: for host in hosts:
try: try:
casts.append(CastDevice(host)) casts.append(CastDevice(host))
except pychromecast.ChromecastConnectionError: except cast.ChromecastConnectionError:
pass pass
else: else:
KNOWN_HOSTS.append(host) KNOWN_HOSTS.append(host)
@ -80,7 +80,7 @@ class CastDevice(MediaPlayerDevice):
def __init__(self, host): def __init__(self, host):
import pychromecast.controllers.youtube as youtube import pychromecast.controllers.youtube as youtube
self.cast = pychromecast.Chromecast(host) self.cast = cast.Chromecast(host)
self.youtube = youtube.YouTubeController() self.youtube = youtube.YouTubeController()
self.cast.register_handler(self.youtube) self.cast.register_handler(self.youtube)
@ -226,7 +226,7 @@ class CastDevice(MediaPlayerDevice):
self.cast.quit_app() self.cast.quit_app()
self.cast.play_media( self.cast.play_media(
CAST_SPLASH, pychromecast.STREAM_TYPE_BUFFERED) CAST_SPLASH, cast.STREAM_TYPE_BUFFERED)
def turn_off(self): def turn_off(self):
""" Turns Chromecast off. """ """ Turns Chromecast off. """