No longer ignore ports for Chromecasts

This commit is contained in:
Paulus Schoutsen 2016-02-01 21:07:33 -08:00
parent 41165695f0
commit c8bfd27182
2 changed files with 50 additions and 20 deletions

View File

@ -28,48 +28,45 @@ SUPPORT_CAST = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE | SUPPORT_PLAY_MEDIA SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE | SUPPORT_PLAY_MEDIA
KNOWN_HOSTS = [] KNOWN_HOSTS = []
# pylint: disable=invalid-name DEFAULT_PORT = 8009
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 cast
import pychromecast import pychromecast
cast = 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):
cast.IGNORE_CEC += ignore_cec pychromecast.IGNORE_CEC += ignore_cec
else: else:
logger.error('Chromecast conig, %s must be a list.', CONF_IGNORE_CEC) logger.error('CEC config "%s" must be a list.', CONF_IGNORE_CEC)
hosts = [] hosts = []
if discovery_info and discovery_info[0] not in KNOWN_HOSTS: if discovery_info and discovery_info in KNOWN_HOSTS:
hosts = [discovery_info[0]] return
elif discovery_info:
hosts = [discovery_info]
elif CONF_HOST in config: elif CONF_HOST in config:
hosts = [config[CONF_HOST]] hosts = [(config[CONF_HOST], DEFAULT_PORT)]
else: else:
hosts = (host_port[0] for host_port hosts = [host for host in pychromecast.discover_chromecasts()
in cast.discover_chromecasts() if host not in KNOWN_HOSTS]
if host_port[0] not in KNOWN_HOSTS)
casts = [] casts = []
for host in hosts: for host in hosts:
try: try:
casts.append(CastDevice(host)) casts.append(CastDevice(*host))
except cast.ChromecastConnectionError:
pass
else:
KNOWN_HOSTS.append(host) KNOWN_HOSTS.append(host)
except pychromecast.ChromecastConnectionError:
pass
add_devices(casts) add_devices(casts)
@ -80,9 +77,10 @@ class CastDevice(MediaPlayerDevice):
# pylint: disable=abstract-method # pylint: disable=abstract-method
# pylint: disable=too-many-public-methods # pylint: disable=too-many-public-methods
def __init__(self, host): def __init__(self, host, port):
import pychromecast
import pychromecast.controllers.youtube as youtube import pychromecast.controllers.youtube as youtube
self.cast = cast.Chromecast(host) self.cast = pychromecast.Chromecast(host, port)
self.youtube = youtube.YouTubeController() self.youtube = youtube.YouTubeController()
self.cast.register_handler(self.youtube) self.cast.register_handler(self.youtube)
@ -224,11 +222,13 @@ class CastDevice(MediaPlayerDevice):
""" Turns on the ChromeCast. """ """ Turns on the ChromeCast. """
# The only way we can turn the Chromecast is on is by launching an app # The only way we can turn the Chromecast is on is by launching an app
if not self.cast.status or not self.cast.status.is_active_input: if not self.cast.status or not self.cast.status.is_active_input:
import pychromecast
if self.cast.app_id: if self.cast.app_id:
self.cast.quit_app() self.cast.quit_app()
self.cast.play_media( self.cast.play_media(
CAST_SPLASH, cast.STREAM_TYPE_BUFFERED) CAST_SPLASH, pychromecast.STREAM_TYPE_BUFFERED)
def turn_off(self): def turn_off(self):
""" Turns Chromecast off. """ """ Turns Chromecast off. """

View File

@ -0,0 +1,30 @@
"""
tests.component.media_player.test_cast
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests cast media_player component.
"""
# pylint: disable=too-many-public-methods,protected-access
import unittest
from unittest.mock import patch
from homeassistant.components.media_player import cast
class TestCastMediaPlayer(unittest.TestCase):
""" Test the media_player module. """
@patch('homeassistant.components.media_player.cast.CastDevice')
def test_filter_duplicates(self, mock_device):
cast.setup_platform(None, {
'host': 'some_host'
}, lambda _: _)
assert mock_device.called
mock_device.reset_mock()
assert not mock_device.called
cast.setup_platform(None, {}, lambda _: _, ('some_host',
cast.DEFAULT_PORT))
assert not mock_device.called