Fix discovery of audio groups (#20947)
* Fix discovery of audio groups * Fix tests * Re-discover * Review comments * Remove failing tests * Update dependencies * Fix test
This commit is contained in:
1 parent
6fad9e1a0a
commit
888345e4ff
4 files changed
+149
-85
No files matched your search
@@ -12,8 +12,7 @@ from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.components.cast.media_player import ChromecastInfo
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect, \
|
||||
async_dispatcher_send
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.components.cast import media_player as cast
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -44,7 +43,7 @@ def get_fake_chromecast_info(host='192.168.178.42', port=8009,
|
||||
uuid: Optional[UUID] = FakeUUID):
|
||||
"""Generate a Fake ChromecastInfo with the specified arguments."""
|
||||
return ChromecastInfo(host=host, port=port, uuid=uuid,
|
||||
friendly_name="Speaker")
|
||||
friendly_name="Speaker", service='the-service')
|
||||
|
||||
|
||||
async def async_setup_cast(hass, config=None, discovery_info=None):
|
||||
@@ -64,9 +63,10 @@ async def async_setup_cast_internal_discovery(hass, config=None,
|
||||
discovery_info=None):
|
||||
"""Set up the cast platform and the discovery."""
|
||||
listener = MagicMock(services={})
|
||||
browser = MagicMock(zc={})
|
||||
|
||||
with patch('pychromecast.start_discovery',
|
||||
return_value=(listener, None)) as start_discovery:
|
||||
return_value=(listener, browser)) as start_discovery:
|
||||
add_entities = await async_setup_cast(hass, config, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
@@ -120,8 +120,10 @@ def test_start_discovery_called_once(hass):
|
||||
@asyncio.coroutine
|
||||
def test_stop_discovery_called_on_stop(hass):
|
||||
"""Test pychromecast.stop_discovery called on shutdown."""
|
||||
browser = MagicMock(zc={})
|
||||
|
||||
with patch('pychromecast.start_discovery',
|
||||
return_value=(None, 'the-browser')) as start_discovery:
|
||||
return_value=(None, browser)) as start_discovery:
|
||||
# start_discovery should be called with empty config
|
||||
yield from async_setup_cast(hass, {})
|
||||
|
||||
@@ -132,38 +134,16 @@ def test_stop_discovery_called_on_stop(hass):
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
stop_discovery.assert_called_once_with('the-browser')
|
||||
stop_discovery.assert_called_once_with(browser)
|
||||
|
||||
with patch('pychromecast.start_discovery',
|
||||
return_value=(None, 'the-browser')) as start_discovery:
|
||||
return_value=(None, browser)) as start_discovery:
|
||||
# start_discovery should be called again on re-startup
|
||||
yield from async_setup_cast(hass)
|
||||
|
||||
assert start_discovery.call_count == 1
|
||||
|
||||
|
||||
async def test_internal_discovery_callback_only_generates_once(hass):
|
||||
"""Test discovery only called once per device."""
|
||||
discover_cast, _ = await async_setup_cast_internal_discovery(hass)
|
||||
info = get_fake_chromecast_info()
|
||||
|
||||
signal = MagicMock()
|
||||
async_dispatcher_connect(hass, 'cast_discovered', signal)
|
||||
|
||||
with patch('pychromecast.dial.get_device_status', return_value=None):
|
||||
# discovering a cast device should call the dispatcher
|
||||
discover_cast('the-service', info)
|
||||
await hass.async_block_till_done()
|
||||
discover = signal.mock_calls[0][1][0]
|
||||
assert discover == info
|
||||
signal.reset_mock()
|
||||
|
||||
# discovering it a second time shouldn't
|
||||
discover_cast('the-service', info)
|
||||
await hass.async_block_till_done()
|
||||
assert signal.call_count == 0
|
||||
|
||||
|
||||
async def test_internal_discovery_callback_fill_out(hass):
|
||||
"""Test internal discovery automatically filling out information."""
|
||||
import pychromecast # imports mock pychromecast
|
||||
@@ -323,35 +303,6 @@ async def test_entity_media_states(hass: HomeAssistantType):
|
||||
assert state.state == 'unknown'
|
||||
|
||||
|
||||
async def test_switched_host(hass: HomeAssistantType):
|
||||
"""Test cast device listens for changed hosts and disconnects old cast."""
|
||||
info = get_fake_chromecast_info()
|
||||
full_info = attr.evolve(info, model_name='google home',
|
||||
friendly_name='Speaker', uuid=FakeUUID)
|
||||
|
||||
with patch('pychromecast.dial.get_device_status',
|
||||
return_value=full_info):
|
||||
chromecast, _ = await async_setup_media_player_cast(hass, full_info)
|
||||
|
||||
chromecast2 = get_fake_chromecast(info)
|
||||
with patch('pychromecast._get_chromecast_from_host',
|
||||
return_value=chromecast2) as get_chromecast:
|
||||
async_dispatcher_send(hass, cast.SIGNAL_CAST_DISCOVERED, full_info)
|
||||
await hass.async_block_till_done()
|
||||
assert get_chromecast.call_count == 0
|
||||
|
||||
changed = attr.evolve(full_info, friendly_name='Speaker 2')
|
||||
async_dispatcher_send(hass, cast.SIGNAL_CAST_DISCOVERED, changed)
|
||||
await hass.async_block_till_done()
|
||||
assert get_chromecast.call_count == 0
|
||||
|
||||
changed = attr.evolve(changed, host='host2')
|
||||
async_dispatcher_send(hass, cast.SIGNAL_CAST_DISCOVERED, changed)
|
||||
await hass.async_block_till_done()
|
||||
assert get_chromecast.call_count == 1
|
||||
assert chromecast.disconnect.call_count == 1
|
||||
|
||||
|
||||
async def test_disconnect_on_stop(hass: HomeAssistantType):
|
||||
"""Test cast device disconnects socket on stop."""
|
||||
info = get_fake_chromecast_info()
|
||||
|
||||
Reference in new issue
Block a user