No longer ignore ports for Chromecasts
parent
41165695f0
commit
c8bfd27182
|
@ -28,48 +28,45 @@ SUPPORT_CAST = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
|||
SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE | SUPPORT_PLAY_MEDIA
|
||||
KNOWN_HOSTS = []
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
cast = None
|
||||
DEFAULT_PORT = 8009
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up the cast platform. """
|
||||
global cast
|
||||
import pychromecast
|
||||
cast = pychromecast
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# import CEC IGNORE attributes
|
||||
ignore_cec = config.get(CONF_IGNORE_CEC, [])
|
||||
if isinstance(ignore_cec, list):
|
||||
cast.IGNORE_CEC += ignore_cec
|
||||
pychromecast.IGNORE_CEC += ignore_cec
|
||||
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 = []
|
||||
|
||||
if discovery_info and discovery_info[0] not in KNOWN_HOSTS:
|
||||
hosts = [discovery_info[0]]
|
||||
if discovery_info and discovery_info in KNOWN_HOSTS:
|
||||
return
|
||||
|
||||
elif discovery_info:
|
||||
hosts = [discovery_info]
|
||||
|
||||
elif CONF_HOST in config:
|
||||
hosts = [config[CONF_HOST]]
|
||||
hosts = [(config[CONF_HOST], DEFAULT_PORT)]
|
||||
|
||||
else:
|
||||
hosts = (host_port[0] for host_port
|
||||
in cast.discover_chromecasts()
|
||||
if host_port[0] not in KNOWN_HOSTS)
|
||||
hosts = [host for host in pychromecast.discover_chromecasts()
|
||||
if host not in KNOWN_HOSTS]
|
||||
|
||||
casts = []
|
||||
|
||||
for host in hosts:
|
||||
try:
|
||||
casts.append(CastDevice(host))
|
||||
except cast.ChromecastConnectionError:
|
||||
pass
|
||||
else:
|
||||
casts.append(CastDevice(*host))
|
||||
KNOWN_HOSTS.append(host)
|
||||
except pychromecast.ChromecastConnectionError:
|
||||
pass
|
||||
|
||||
add_devices(casts)
|
||||
|
||||
|
@ -80,9 +77,10 @@ class CastDevice(MediaPlayerDevice):
|
|||
# pylint: disable=abstract-method
|
||||
# pylint: disable=too-many-public-methods
|
||||
|
||||
def __init__(self, host):
|
||||
def __init__(self, host, port):
|
||||
import pychromecast
|
||||
import pychromecast.controllers.youtube as youtube
|
||||
self.cast = cast.Chromecast(host)
|
||||
self.cast = pychromecast.Chromecast(host, port)
|
||||
self.youtube = youtube.YouTubeController()
|
||||
self.cast.register_handler(self.youtube)
|
||||
|
||||
|
@ -224,11 +222,13 @@ class CastDevice(MediaPlayerDevice):
|
|||
""" Turns on the ChromeCast. """
|
||||
# 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:
|
||||
import pychromecast
|
||||
|
||||
if self.cast.app_id:
|
||||
self.cast.quit_app()
|
||||
|
||||
self.cast.play_media(
|
||||
CAST_SPLASH, cast.STREAM_TYPE_BUFFERED)
|
||||
CAST_SPLASH, pychromecast.STREAM_TYPE_BUFFERED)
|
||||
|
||||
def turn_off(self):
|
||||
""" Turns Chromecast off. """
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue