Fixes an issue where Chromecast audio groups were not properly discovered (#3630)

* Fixes an issue where Chromecast audio groups were not properly discovered

* Forgot to commit the main fix

* Removes unused variable

* Doesn't use a protected API anymore

* PR remarks

* Fixes tests, adds comment

* Restores line as it was in the original commit, rephrases comment

* Should fix lint issues

* Trailing whitespace

* Some more lint
pull/3564/merge
Vittorio Monaco 2016-10-13 17:51:43 +02:00 committed by Paulus Schoutsen
parent e1647fb6ac
commit e031b8078f
2 changed files with 29 additions and 9 deletions

View File

@ -68,12 +68,19 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
casts = []
# get_chromecasts() returns Chromecast objects
# with the correct friendly name for grouped devices
all_chromecasts = pychromecast.get_chromecasts()
for host in hosts:
try:
casts.append(CastDevice(*host))
KNOWN_HOSTS.append(host)
except pychromecast.ChromecastConnectionError:
pass
found = [device for device in all_chromecasts
if (device.host, device.port) == host]
if found:
try:
casts.append(CastDevice(found[0]))
KNOWN_HOSTS.append(host)
except pychromecast.ChromecastConnectionError:
pass
add_devices(casts)
@ -83,10 +90,9 @@ class CastDevice(MediaPlayerDevice):
# pylint: disable=abstract-method
# pylint: disable=too-many-public-methods
def __init__(self, host, port):
def __init__(self, chromecast):
"""Initialize the Cast device."""
import pychromecast
self.cast = pychromecast.Chromecast(host, port)
self.cast = chromecast
self.cast.socket_client.receiver_controller.register_status_listener(
self)

View File

@ -6,12 +6,25 @@ from unittest.mock import patch
from homeassistant.components.media_player import cast
class FakeChromeCast(object):
def __init__(self, host, port):
self.host = host
self.port = port
class TestCastMediaPlayer(unittest.TestCase):
"""Test the media_player module."""
@patch('homeassistant.components.media_player.cast.CastDevice')
def test_filter_duplicates(self, mock_device):
@patch('pychromecast.get_chromecasts')
def test_filter_duplicates(self, mock_get_chromecasts, mock_device):
"""Test filtering of duplicates."""
mock_get_chromecasts.return_value = [
FakeChromeCast('some_host', cast.DEFAULT_PORT)
]
# Test chromecasts as if they were hardcoded in configuration.yaml
cast.setup_platform(None, {
'host': 'some_host'
}, lambda _: _)
@ -21,6 +34,7 @@ class TestCastMediaPlayer(unittest.TestCase):
mock_device.reset_mock()
assert not mock_device.called
# Test chromecasts as if they were automatically discovered
cast.setup_platform(None, {}, lambda _: _, ('some_host',
cast.DEFAULT_PORT))
assert not mock_device.called