mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
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
This commit is contained in:
parent
e1647fb6ac
commit
e031b8078f
@ -68,12 +68,19 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
casts = []
|
casts = []
|
||||||
|
|
||||||
|
# get_chromecasts() returns Chromecast objects
|
||||||
|
# with the correct friendly name for grouped devices
|
||||||
|
all_chromecasts = pychromecast.get_chromecasts()
|
||||||
|
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
try:
|
found = [device for device in all_chromecasts
|
||||||
casts.append(CastDevice(*host))
|
if (device.host, device.port) == host]
|
||||||
KNOWN_HOSTS.append(host)
|
if found:
|
||||||
except pychromecast.ChromecastConnectionError:
|
try:
|
||||||
pass
|
casts.append(CastDevice(found[0]))
|
||||||
|
KNOWN_HOSTS.append(host)
|
||||||
|
except pychromecast.ChromecastConnectionError:
|
||||||
|
pass
|
||||||
|
|
||||||
add_devices(casts)
|
add_devices(casts)
|
||||||
|
|
||||||
@ -83,10 +90,9 @@ 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, port):
|
def __init__(self, chromecast):
|
||||||
"""Initialize the Cast device."""
|
"""Initialize the Cast device."""
|
||||||
import pychromecast
|
self.cast = chromecast
|
||||||
self.cast = pychromecast.Chromecast(host, port)
|
|
||||||
|
|
||||||
self.cast.socket_client.receiver_controller.register_status_listener(
|
self.cast.socket_client.receiver_controller.register_status_listener(
|
||||||
self)
|
self)
|
||||||
|
@ -6,12 +6,25 @@ from unittest.mock import patch
|
|||||||
from homeassistant.components.media_player import cast
|
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):
|
class TestCastMediaPlayer(unittest.TestCase):
|
||||||
"""Test the media_player module."""
|
"""Test the media_player module."""
|
||||||
|
|
||||||
@patch('homeassistant.components.media_player.cast.CastDevice')
|
@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."""
|
"""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, {
|
cast.setup_platform(None, {
|
||||||
'host': 'some_host'
|
'host': 'some_host'
|
||||||
}, lambda _: _)
|
}, lambda _: _)
|
||||||
@ -21,6 +34,7 @@ class TestCastMediaPlayer(unittest.TestCase):
|
|||||||
mock_device.reset_mock()
|
mock_device.reset_mock()
|
||||||
assert not mock_device.called
|
assert not mock_device.called
|
||||||
|
|
||||||
|
# Test chromecasts as if they were automatically discovered
|
||||||
cast.setup_platform(None, {}, lambda _: _, ('some_host',
|
cast.setup_platform(None, {}, lambda _: _, ('some_host',
|
||||||
cast.DEFAULT_PORT))
|
cast.DEFAULT_PORT))
|
||||||
assert not mock_device.called
|
assert not mock_device.called
|
||||||
|
Loading…
x
Reference in New Issue
Block a user